2009-05-11 22:06:48 +00:00
|
|
|
/*****************************************************************************
|
2011-11-14 03:37:50 +00:00
|
|
|
*
|
2009-05-11 22:06:48 +00:00
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
|
|
|
*
|
2011-10-22 00:27:06 +00:00
|
|
|
* Copyright (C) 2011 Artem Pavlenko
|
2009-05-11 22:06:48 +00: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 12:29:08 +00:00
|
|
|
// mapnik
|
2009-05-11 22:06:48 +00:00
|
|
|
#include <mapnik/global.hpp>
|
2012-04-08 00:20:56 +00:00
|
|
|
#include <mapnik/debug.hpp>
|
2009-12-16 20:02:06 +00:00
|
|
|
#include <mapnik/box2d.hpp>
|
2009-05-11 22:06:48 +00:00
|
|
|
#include <mapnik/geometry.hpp>
|
|
|
|
#include <mapnik/feature.hpp>
|
|
|
|
#include <mapnik/feature_layer_desc.hpp>
|
|
|
|
#include <mapnik/wkb.hpp>
|
|
|
|
#include <mapnik/unicode.hpp>
|
|
|
|
|
|
|
|
// ogr
|
|
|
|
#include "ogr_converter.hpp"
|
|
|
|
|
|
|
|
using mapnik::feature_ptr;
|
|
|
|
using mapnik::geometry_utils;
|
2010-11-03 13:19:15 +00:00
|
|
|
using mapnik::geometry_type;
|
2009-05-11 22:06:48 +00:00
|
|
|
|
2011-12-21 20:22:05 +00:00
|
|
|
void ogr_converter::convert_geometry(OGRGeometry* geom, feature_ptr feature)
|
2009-05-11 22:06:48 +00:00
|
|
|
{
|
2011-12-21 20:22:05 +00:00
|
|
|
// NOTE: wkbFlatten macro in ogr flattens 2.5d types into base 2d type
|
2011-10-22 00:27:06 +00:00
|
|
|
switch (wkbFlatten(geom->getGeometryType()))
|
|
|
|
{
|
|
|
|
case wkbPoint:
|
|
|
|
convert_point(static_cast<OGRPoint*>(geom), feature);
|
|
|
|
break;
|
2011-12-21 20:22:05 +00:00
|
|
|
case wkbMultiPoint:
|
|
|
|
convert_multipoint(static_cast<OGRMultiPoint*>(geom), feature);
|
|
|
|
break;
|
|
|
|
case wkbLinearRing:
|
|
|
|
convert_linestring(static_cast<OGRLinearRing*>(geom), feature);
|
|
|
|
break;
|
2011-10-22 00:27:06 +00:00
|
|
|
case wkbLineString:
|
|
|
|
convert_linestring(static_cast<OGRLineString*>(geom), feature);
|
|
|
|
break;
|
2011-12-21 20:22:05 +00:00
|
|
|
case wkbMultiLineString:
|
|
|
|
convert_multilinestring(static_cast<OGRMultiLineString*>(geom), feature);
|
|
|
|
break;
|
2011-10-22 00:27:06 +00:00
|
|
|
case wkbPolygon:
|
|
|
|
convert_polygon(static_cast<OGRPolygon*>(geom), feature);
|
|
|
|
break;
|
|
|
|
case wkbMultiPolygon:
|
2011-12-21 20:22:05 +00:00
|
|
|
convert_multipolygon(static_cast<OGRMultiPolygon*>(geom), feature);
|
2011-10-22 00:27:06 +00:00
|
|
|
break;
|
|
|
|
case wkbGeometryCollection:
|
2011-12-21 20:22:05 +00:00
|
|
|
convert_collection(static_cast<OGRGeometryCollection*>(geom), feature);
|
2011-10-22 00:27:06 +00:00
|
|
|
break;
|
|
|
|
case wkbNone:
|
|
|
|
case wkbUnknown:
|
|
|
|
default:
|
2012-04-08 00:20:56 +00:00
|
|
|
{
|
2012-04-09 01:00:51 +00:00
|
|
|
MAPNIK_LOG_WARN(ogr) << "ogr_converter: unknown <ogr> geometry_type="
|
|
|
|
<< wkbFlatten(geom->getGeometryType());
|
2012-04-08 00:20:56 +00:00
|
|
|
}
|
2011-10-22 00:27:06 +00:00
|
|
|
break;
|
|
|
|
}
|
2009-05-11 22:06:48 +00:00
|
|
|
}
|
|
|
|
|
2011-10-22 00:27:06 +00:00
|
|
|
void ogr_converter::convert_point(OGRPoint* geom, feature_ptr feature)
|
2009-05-11 22:06:48 +00:00
|
|
|
{
|
2013-09-20 03:19:01 +00:00
|
|
|
std::unique_ptr<geometry_type> point(new geometry_type(mapnik::geometry_type::types::Point));
|
2011-10-22 00:27:06 +00:00
|
|
|
point->move_to(geom->getX(), geom->getY());
|
2013-09-20 03:19:01 +00:00
|
|
|
feature->paths().push_back(point.release());
|
2009-05-11 22:06:48 +00:00
|
|
|
}
|
|
|
|
|
2011-10-22 00:27:06 +00:00
|
|
|
void ogr_converter::convert_linestring(OGRLineString* geom, feature_ptr feature)
|
2009-05-11 22:06:48 +00:00
|
|
|
{
|
2011-10-22 00:27:06 +00:00
|
|
|
int num_points = geom->getNumPoints();
|
2013-09-20 03:19:01 +00:00
|
|
|
std::unique_ptr<geometry_type> line(new geometry_type(mapnik::geometry_type::types::LineString));
|
2011-10-22 00:27:06 +00:00
|
|
|
line->move_to(geom->getX(0), geom->getY(0));
|
|
|
|
for (int i = 1; i < num_points; ++i)
|
2009-05-11 22:06:48 +00:00
|
|
|
{
|
2011-10-22 00:27:06 +00:00
|
|
|
line->line_to (geom->getX(i), geom->getY(i));
|
2009-05-11 22:06:48 +00:00
|
|
|
}
|
2013-09-20 03:19:01 +00:00
|
|
|
feature->paths().push_back(line.release());
|
2009-05-11 22:06:48 +00:00
|
|
|
}
|
|
|
|
|
2011-10-22 00:27:06 +00:00
|
|
|
void ogr_converter::convert_polygon(OGRPolygon* geom, feature_ptr feature)
|
2009-05-11 22:06:48 +00:00
|
|
|
{
|
2011-10-22 00:27:06 +00:00
|
|
|
OGRLinearRing* exterior = geom->getExteriorRing();
|
|
|
|
int num_points = exterior->getNumPoints();
|
|
|
|
int num_interior = geom->getNumInteriorRings();
|
2009-05-11 22:06:48 +00:00
|
|
|
int capacity = 0;
|
2012-07-26 14:51:10 +00:00
|
|
|
for (int r = 0; r < num_interior; ++r)
|
2009-05-11 22:06:48 +00:00
|
|
|
{
|
2011-10-22 00:27:06 +00:00
|
|
|
OGRLinearRing* interior = geom->getInteriorRing(r);
|
|
|
|
capacity += interior->getNumPoints();
|
2011-11-14 03:37:50 +00:00
|
|
|
}
|
2012-07-26 14:51:10 +00:00
|
|
|
|
2013-09-20 03:19:01 +00:00
|
|
|
std::unique_ptr<geometry_type> poly(new geometry_type(mapnik::geometry_type::types::Polygon));
|
2012-07-26 14:51:10 +00:00
|
|
|
|
2011-10-22 00:27:06 +00:00
|
|
|
poly->move_to(exterior->getX(0), exterior->getY(0));
|
2013-03-01 16:06:44 +00:00
|
|
|
for (int i = 1; i < num_points; ++i)
|
2009-05-11 22:06:48 +00:00
|
|
|
{
|
2011-10-22 00:27:06 +00:00
|
|
|
poly->line_to(exterior->getX(i), exterior->getY(i));
|
2009-05-11 22:06:48 +00:00
|
|
|
}
|
2013-03-01 16:06:44 +00:00
|
|
|
poly->close_path();
|
2012-07-26 14:51:10 +00:00
|
|
|
for (int r = 0; r < num_interior; ++r)
|
2009-05-11 22:06:48 +00:00
|
|
|
{
|
2011-10-22 00:27:06 +00:00
|
|
|
OGRLinearRing* interior = geom->getInteriorRing(r);
|
|
|
|
num_points = interior->getNumPoints();
|
|
|
|
poly->move_to(interior->getX(0), interior->getY(0));
|
2013-03-01 16:06:44 +00:00
|
|
|
for (int i = 1; i < num_points; ++i)
|
2009-05-11 22:06:48 +00:00
|
|
|
{
|
2011-10-22 00:27:06 +00:00
|
|
|
poly->line_to(interior->getX(i), interior->getY(i));
|
2009-05-11 22:06:48 +00:00
|
|
|
}
|
2013-03-01 16:06:44 +00:00
|
|
|
poly->close_path();
|
2009-05-11 22:06:48 +00:00
|
|
|
}
|
2013-09-20 03:19:01 +00:00
|
|
|
feature->paths().push_back(poly.release());
|
2009-05-11 22:06:48 +00:00
|
|
|
}
|
|
|
|
|
2011-10-22 00:27:06 +00:00
|
|
|
void ogr_converter::convert_multipoint(OGRMultiPoint* geom, feature_ptr feature)
|
2009-05-11 22:06:48 +00:00
|
|
|
{
|
2011-10-22 00:27:06 +00:00
|
|
|
int num_geometries = geom->getNumGeometries();
|
2012-07-26 14:51:10 +00:00
|
|
|
for (int i = 0; i < num_geometries; ++i)
|
2009-05-11 22:06:48 +00:00
|
|
|
{
|
2011-10-22 00:27:06 +00:00
|
|
|
convert_point(static_cast<OGRPoint*>(geom->getGeometryRef(i)), feature);
|
2009-05-11 22:06:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-22 00:27:06 +00:00
|
|
|
void ogr_converter::convert_multilinestring(OGRMultiLineString* geom, feature_ptr feature)
|
2009-05-11 22:06:48 +00:00
|
|
|
{
|
2011-10-22 00:27:06 +00:00
|
|
|
int num_geometries = geom->getNumGeometries();
|
2012-07-26 14:51:10 +00:00
|
|
|
for (int i = 0; i < num_geometries; ++i)
|
2009-05-11 22:06:48 +00:00
|
|
|
{
|
2011-10-22 00:27:06 +00:00
|
|
|
convert_linestring(static_cast<OGRLineString*>(geom->getGeometryRef(i)), feature);
|
2009-05-11 22:06:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-22 00:27:06 +00:00
|
|
|
void ogr_converter::convert_multipolygon(OGRMultiPolygon* geom, feature_ptr feature)
|
2009-05-11 22:06:48 +00:00
|
|
|
{
|
2011-10-22 00:27:06 +00:00
|
|
|
int num_geometries = geom->getNumGeometries();
|
2012-07-26 14:51:10 +00:00
|
|
|
for (int i = 0; i < num_geometries; ++i)
|
2009-05-11 22:06:48 +00:00
|
|
|
{
|
2011-10-22 00:27:06 +00:00
|
|
|
convert_polygon(static_cast<OGRPolygon*>(geom->getGeometryRef(i)), feature);
|
2009-05-11 22:06:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-21 20:22:05 +00:00
|
|
|
void ogr_converter::convert_collection(OGRGeometryCollection* geom, feature_ptr feature)
|
2009-05-11 22:06:48 +00:00
|
|
|
{
|
2011-10-22 00:27:06 +00:00
|
|
|
int num_geometries = geom->getNumGeometries();
|
2012-07-26 14:51:10 +00:00
|
|
|
for (int i = 0; i < num_geometries; ++i)
|
2009-05-11 22:06:48 +00:00
|
|
|
{
|
2011-10-22 00:27:06 +00:00
|
|
|
OGRGeometry* g = geom->getGeometryRef(i);
|
2009-05-11 22:06:48 +00:00
|
|
|
if (g != NULL)
|
|
|
|
{
|
2011-12-21 20:22:05 +00:00
|
|
|
convert_geometry(g, feature);
|
2009-05-11 22:06:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|