mapnik/plugins/input/ogr/ogr_converter.cpp

175 lines
5.9 KiB
C++
Raw Normal View History

/*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
2017-03-27 17:14:51 +02:00
* Copyright (C) 2016 Artem Pavlenko
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*****************************************************************************/
// mapnik
2012-04-08 02:20:56 +02:00
#include <mapnik/debug.hpp>
#include <mapnik/geometry.hpp>
// ogr
2015-12-03 16:22:02 +01:00
#pragma GCC diagnostic push
#include <mapnik/warning_ignore.hpp>
#include "ogr_converter.hpp"
2015-03-22 04:08:07 +01:00
#include <ogr_core.h>
#include <ogr_geometry.h>
2015-12-03 16:22:02 +01:00
#pragma GCC diagnostic pop
2015-04-09 22:22:51 +02:00
mapnik::geometry::geometry<double> ogr_converter::convert_geometry(OGRGeometry* ogr_geom)
{
// NOTE: wkbFlatten macro in ogr flattens 2.5d types into base 2d type
2015-03-22 04:08:07 +01:00
switch (wkbFlatten(ogr_geom->getGeometryType()))
{
case wkbPoint:
2015-03-22 04:08:07 +01:00
return convert_point(static_cast<OGRPoint*>(ogr_geom));
break;
case wkbMultiPoint:
2015-03-22 04:08:07 +01:00
return convert_multipoint(static_cast<OGRMultiPoint*>(ogr_geom));
break;
case wkbLinearRing:
2015-03-22 04:08:07 +01:00
return convert_linestring(static_cast<OGRLinearRing*>(ogr_geom));
break;
case wkbLineString:
2015-03-22 04:08:07 +01:00
return convert_linestring(static_cast<OGRLineString*>(ogr_geom));
break;
case wkbMultiLineString:
2015-03-22 04:08:07 +01:00
return convert_multilinestring(static_cast<OGRMultiLineString*>(ogr_geom));
break;
case wkbPolygon:
2015-03-22 04:08:07 +01:00
return convert_polygon(static_cast<OGRPolygon*>(ogr_geom));
break;
case wkbMultiPolygon:
2015-03-22 04:08:07 +01:00
return convert_multipolygon(static_cast<OGRMultiPolygon*>(ogr_geom));
break;
case wkbGeometryCollection:
2015-03-22 04:08:07 +01:00
return convert_collection(static_cast<OGRGeometryCollection*>(ogr_geom));
break;
case wkbNone:
case wkbUnknown:
default:
2012-04-08 02:20:56 +02:00
{
MAPNIK_LOG_WARN(ogr) << "ogr_converter: unknown <ogr> geometry_type="
2015-03-22 04:08:07 +01:00
<< wkbFlatten(ogr_geom->getGeometryType());
2012-04-08 02:20:56 +02:00
}
2015-04-09 22:22:51 +02:00
return mapnik::geometry::geometry<double>();
break;
}
}
2015-04-09 22:22:51 +02:00
mapnik::geometry::point<double> ogr_converter::convert_point(OGRPoint* ogr_geom)
{
2015-04-09 22:22:51 +02:00
return mapnik::geometry::point<double>(ogr_geom->getX(), ogr_geom->getY());
}
2015-04-09 22:22:51 +02:00
mapnik::geometry::multi_point<double> ogr_converter::convert_multipoint(OGRMultiPoint* ogr_geom)
{
2015-04-09 22:22:51 +02:00
mapnik::geometry::multi_point<double> geom;
2015-03-22 04:08:07 +01:00
int num_geometries = ogr_geom->getNumGeometries();
geom.reserve(num_geometries);
for (int i = 0; i < num_geometries; ++i)
{
2015-03-22 04:08:07 +01:00
OGRPoint const* pt = static_cast<OGRPoint*>(ogr_geom->getGeometryRef(i));
geom.emplace_back(pt->getX(), pt->getY());
}
2015-03-22 04:08:07 +01:00
return geom;
}
2015-04-09 22:22:51 +02:00
mapnik::geometry::line_string<double> ogr_converter::convert_linestring(OGRLineString* ogr_geom)
{
2015-04-09 22:22:51 +02:00
mapnik::geometry::line_string<double> geom;
2015-03-22 04:08:07 +01:00
int num_points = ogr_geom->getNumPoints();
geom.reserve(num_points);
for (int i = 0; i < num_points; ++i)
{
geom.emplace_back(ogr_geom->getX(i), ogr_geom->getY(i));
}
2015-03-22 04:08:07 +01:00
return geom;
}
2015-04-09 22:22:51 +02:00
mapnik::geometry::multi_line_string<double> ogr_converter::convert_multilinestring(OGRMultiLineString* ogr_geom)
{
2015-04-09 22:22:51 +02:00
mapnik::geometry::multi_line_string<double> geom;
2015-03-22 04:08:07 +01:00
int num_geometries = ogr_geom->getNumGeometries();
geom.reserve(num_geometries);
2012-07-26 16:51:10 +02:00
for (int i = 0; i < num_geometries; ++i)
{
geom.emplace_back(convert_linestring(static_cast<OGRLineString*>(ogr_geom->getGeometryRef(i))));
}
2015-03-22 04:08:07 +01:00
return geom;
}
2015-03-22 04:08:07 +01:00
2015-04-09 22:22:51 +02:00
mapnik::geometry::polygon<double> ogr_converter::convert_polygon(OGRPolygon* ogr_geom)
{
2015-04-09 22:22:51 +02:00
mapnik::geometry::polygon<double> geom;
mapnik::geometry::linear_ring<double> exterior;
2015-03-22 04:08:07 +01:00
OGRLinearRing* ogr_exterior = ogr_geom->getExteriorRing();
int num_points = ogr_exterior->getNumPoints();
exterior.reserve(num_points);
for (int i = 0; i < num_points; ++i)
{
exterior.emplace_back(ogr_exterior->getX(i), ogr_exterior->getY(i));
}
geom.push_back(std::move(exterior));
2015-03-22 04:08:07 +01:00
int num_interior = ogr_geom->getNumInteriorRings();
for (int r = 0; r < num_interior; ++r)
{
2015-03-22 04:08:07 +01:00
OGRLinearRing* ogr_interior = ogr_geom->getInteriorRing(r);
2015-04-09 22:22:51 +02:00
mapnik::geometry::linear_ring<double> interior;
2015-03-22 04:08:07 +01:00
int num_interior_points = ogr_interior->getNumPoints();
interior.reserve(num_interior_points);
for (int i = 0; i < num_interior_points; ++i)
{
interior.emplace_back(ogr_interior->getX(i), ogr_interior->getY(i));
}
geom.push_back(std::move(interior));
}
2015-03-22 04:08:07 +01:00
return geom;
}
2015-04-09 22:22:51 +02:00
mapnik::geometry::multi_polygon<double> ogr_converter::convert_multipolygon(OGRMultiPolygon* ogr_geom)
{
2015-04-09 22:22:51 +02:00
mapnik::geometry::multi_polygon<double> geom;
2015-03-22 04:08:07 +01:00
int num_geometries = ogr_geom->getNumGeometries();
geom.reserve(num_geometries);
2012-07-26 16:51:10 +02:00
for (int i = 0; i < num_geometries; ++i)
{
geom.emplace_back(convert_polygon(static_cast<OGRPolygon*>(ogr_geom->getGeometryRef(i))));
}
2015-03-22 04:08:07 +01:00
return geom;
}
2015-04-09 22:22:51 +02:00
mapnik::geometry::geometry_collection<double> ogr_converter::convert_collection(OGRGeometryCollection* ogr_geom)
{
2015-04-09 22:22:51 +02:00
mapnik::geometry::geometry_collection<double> geom;
2015-03-22 04:08:07 +01:00
int num_geometries = ogr_geom->getNumGeometries();
geom.reserve(num_geometries);
2012-07-26 16:51:10 +02:00
for (int i = 0; i < num_geometries; ++i)
{
2015-03-22 04:08:07 +01:00
OGRGeometry* g = ogr_geom->getGeometryRef(i);
2013-11-27 16:52:47 +01:00
if (g != nullptr)
{
geom.emplace_back(convert_geometry(g));
}
}
2015-03-22 04:08:07 +01:00
return geom;
}