2009-05-12 00:06:48 +02:00
|
|
|
/*****************************************************************************
|
2011-11-14 04:37:50 +01:00
|
|
|
*
|
2009-05-12 00:06:48 +02:00
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
|
|
|
*
|
2017-05-05 13:02:01 +02:00
|
|
|
* Copyright (C) 2017 Artem Pavlenko
|
2009-05-12 00:06:48 +02:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2011-10-22 14:29:08 +02:00
|
|
|
// mapnik
|
2012-04-08 02:20:56 +02:00
|
|
|
#include <mapnik/debug.hpp>
|
2015-03-24 13:32:05 +01:00
|
|
|
#include <mapnik/geometry.hpp>
|
2009-05-12 00:06:48 +02:00
|
|
|
|
|
|
|
// ogr
|
2015-12-03 16:22:02 +01:00
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#include <mapnik/warning_ignore.hpp>
|
2009-05-12 00:06:48 +02:00
|
|
|
#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
|
2009-05-12 00:06:48 +02:00
|
|
|
|
2015-04-09 22:22:51 +02:00
|
|
|
mapnik::geometry::geometry<double> ogr_converter::convert_geometry(OGRGeometry* ogr_geom)
|
2009-05-12 00:06:48 +02:00
|
|
|
{
|
2011-12-21 21:22:05 +01:00
|
|
|
// 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()))
|
2011-10-22 02:27:06 +02:00
|
|
|
{
|
|
|
|
case wkbPoint:
|
2015-03-22 04:08:07 +01:00
|
|
|
return convert_point(static_cast<OGRPoint*>(ogr_geom));
|
2011-10-22 02:27:06 +02:00
|
|
|
break;
|
2011-12-21 21:22:05 +01:00
|
|
|
case wkbMultiPoint:
|
2015-03-22 04:08:07 +01:00
|
|
|
return convert_multipoint(static_cast<OGRMultiPoint*>(ogr_geom));
|
2011-12-21 21:22:05 +01:00
|
|
|
break;
|
|
|
|
case wkbLinearRing:
|
2015-03-22 04:08:07 +01:00
|
|
|
return convert_linestring(static_cast<OGRLinearRing*>(ogr_geom));
|
2011-12-21 21:22:05 +01:00
|
|
|
break;
|
2011-10-22 02:27:06 +02:00
|
|
|
case wkbLineString:
|
2015-03-22 04:08:07 +01:00
|
|
|
return convert_linestring(static_cast<OGRLineString*>(ogr_geom));
|
2011-10-22 02:27:06 +02:00
|
|
|
break;
|
2011-12-21 21:22:05 +01:00
|
|
|
case wkbMultiLineString:
|
2015-03-22 04:08:07 +01:00
|
|
|
return convert_multilinestring(static_cast<OGRMultiLineString*>(ogr_geom));
|
2011-12-21 21:22:05 +01:00
|
|
|
break;
|
2011-10-22 02:27:06 +02:00
|
|
|
case wkbPolygon:
|
2015-03-22 04:08:07 +01:00
|
|
|
return convert_polygon(static_cast<OGRPolygon*>(ogr_geom));
|
2011-10-22 02:27:06 +02:00
|
|
|
break;
|
|
|
|
case wkbMultiPolygon:
|
2015-03-22 04:08:07 +01:00
|
|
|
return convert_multipolygon(static_cast<OGRMultiPolygon*>(ogr_geom));
|
2011-10-22 02:27:06 +02:00
|
|
|
break;
|
|
|
|
case wkbGeometryCollection:
|
2015-03-22 04:08:07 +01:00
|
|
|
return convert_collection(static_cast<OGRGeometryCollection*>(ogr_geom));
|
2011-10-22 02:27:06 +02:00
|
|
|
break;
|
|
|
|
case wkbNone:
|
|
|
|
case wkbUnknown:
|
|
|
|
default:
|
2012-04-08 02:20:56 +02:00
|
|
|
{
|
2012-04-09 03:00:51 +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>();
|
2011-10-22 02:27:06 +02:00
|
|
|
break;
|
|
|
|
}
|
2009-05-12 00:06:48 +02:00
|
|
|
}
|
|
|
|
|
2015-04-09 22:22:51 +02:00
|
|
|
mapnik::geometry::point<double> ogr_converter::convert_point(OGRPoint* ogr_geom)
|
2009-05-12 00:06:48 +02:00
|
|
|
{
|
2015-04-09 22:22:51 +02:00
|
|
|
return mapnik::geometry::point<double>(ogr_geom->getX(), ogr_geom->getY());
|
2009-05-12 00:06:48 +02:00
|
|
|
}
|
|
|
|
|
2015-04-09 22:22:51 +02:00
|
|
|
mapnik::geometry::multi_point<double> ogr_converter::convert_multipoint(OGRMultiPoint* ogr_geom)
|
2009-05-12 00:06:48 +02:00
|
|
|
{
|
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)
|
2009-05-12 00:06:48 +02:00
|
|
|
{
|
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());
|
2009-05-12 00:06:48 +02:00
|
|
|
}
|
2015-03-22 04:08:07 +01:00
|
|
|
return geom;
|
2009-05-12 00:06:48 +02:00
|
|
|
}
|
|
|
|
|
2015-04-09 22:22:51 +02:00
|
|
|
mapnik::geometry::line_string<double> ogr_converter::convert_linestring(OGRLineString* ogr_geom)
|
2009-05-12 00:06:48 +02:00
|
|
|
{
|
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)
|
2009-05-12 00:06:48 +02:00
|
|
|
{
|
2016-06-27 11:23:13 +02:00
|
|
|
geom.emplace_back(ogr_geom->getX(i), ogr_geom->getY(i));
|
2009-05-12 00:06:48 +02:00
|
|
|
}
|
2015-03-22 04:08:07 +01:00
|
|
|
return geom;
|
2009-05-12 00:06:48 +02:00
|
|
|
}
|
|
|
|
|
2015-04-09 22:22:51 +02:00
|
|
|
mapnik::geometry::multi_line_string<double> ogr_converter::convert_multilinestring(OGRMultiLineString* ogr_geom)
|
2009-05-12 00:06:48 +02:00
|
|
|
{
|
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)
|
2009-05-12 00:06:48 +02:00
|
|
|
{
|
2015-06-02 15:25:57 +02:00
|
|
|
geom.emplace_back(convert_linestring(static_cast<OGRLineString*>(ogr_geom->getGeometryRef(i))));
|
2009-05-12 00:06:48 +02:00
|
|
|
}
|
2015-03-22 04:08:07 +01:00
|
|
|
return geom;
|
2009-05-12 00:06:48 +02:00
|
|
|
}
|
|
|
|
|
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)
|
2009-05-12 00:06:48 +02:00
|
|
|
{
|
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));
|
|
|
|
}
|
2016-06-29 13:37:32 +02:00
|
|
|
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)
|
2009-05-12 00:06:48 +02:00
|
|
|
{
|
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));
|
|
|
|
}
|
2016-06-29 13:37:32 +02:00
|
|
|
geom.push_back(std::move(interior));
|
2009-05-12 00:06:48 +02:00
|
|
|
}
|
2015-03-22 04:08:07 +01:00
|
|
|
return geom;
|
2009-05-12 00:06:48 +02:00
|
|
|
}
|
|
|
|
|
2015-04-09 22:22:51 +02:00
|
|
|
mapnik::geometry::multi_polygon<double> ogr_converter::convert_multipolygon(OGRMultiPolygon* ogr_geom)
|
2009-05-12 00:06:48 +02:00
|
|
|
{
|
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)
|
2009-05-12 00:06:48 +02:00
|
|
|
{
|
2015-06-02 15:25:57 +02:00
|
|
|
geom.emplace_back(convert_polygon(static_cast<OGRPolygon*>(ogr_geom->getGeometryRef(i))));
|
2009-05-12 00:06:48 +02:00
|
|
|
}
|
2015-03-22 04:08:07 +01:00
|
|
|
return geom;
|
2009-05-12 00:06:48 +02:00
|
|
|
}
|
|
|
|
|
2015-04-09 22:22:51 +02:00
|
|
|
mapnik::geometry::geometry_collection<double> ogr_converter::convert_collection(OGRGeometryCollection* ogr_geom)
|
2009-05-12 00:06:48 +02:00
|
|
|
{
|
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)
|
2009-05-12 00:06:48 +02:00
|
|
|
{
|
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)
|
2009-05-12 00:06:48 +02:00
|
|
|
{
|
2015-06-02 15:25:57 +02:00
|
|
|
geom.emplace_back(convert_geometry(g));
|
2009-05-12 00:06:48 +02:00
|
|
|
}
|
|
|
|
}
|
2015-03-22 04:08:07 +01:00
|
|
|
return geom;
|
2009-05-12 00:06:48 +02:00
|
|
|
}
|