1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
|
/*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2007 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
*
*****************************************************************************/
//$Id$
#include <mapnik/global.hpp>
#include <mapnik/box2d.hpp>
#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;
using mapnik::geometry_type;
void ogr_converter::convert_geometry (OGRGeometry* geom, feature_ptr feature, bool multiple_geometries)
{
switch (wkbFlatten (geom->getGeometryType()))
{
case wkbPoint:
convert_point (static_cast<OGRPoint*>(geom), feature);
break;
case wkbLineString:
convert_linestring (static_cast<OGRLineString*>(geom), feature);
break;
case wkbPolygon:
convert_polygon (static_cast<OGRPolygon*>(geom), feature);
break;
case wkbMultiPoint:
if (multiple_geometries)
convert_multipoint_2 (static_cast<OGRMultiPoint*>(geom), feature);
else
// Todo - using convert_multipoint_2 until we have proper multipoint handling in convert_multipoint
// http://trac.mapnik.org/ticket/458
//convert_multipoint (static_cast<OGRMultiPoint*>(geom), feature);
convert_multipoint_2 (static_cast<OGRMultiPoint*>(geom), feature);
break;
case wkbMultiLineString:
if (multiple_geometries)
convert_multilinestring_2 (static_cast<OGRMultiLineString*>(geom), feature);
else
convert_multilinestring (static_cast<OGRMultiLineString*>(geom), feature);
break;
case wkbMultiPolygon:
if (multiple_geometries)
convert_multipolygon_2 (static_cast<OGRMultiPolygon*>(geom), feature);
else
convert_multipolygon (static_cast<OGRMultiPolygon*>(geom), feature);
break;
case wkbGeometryCollection:
convert_collection (static_cast<OGRGeometryCollection*>(geom), feature, multiple_geometries);
break;
case wkbLinearRing:
convert_linestring (static_cast<OGRLinearRing*>(geom), feature);
break;
case wkbNone:
case wkbUnknown:
default:
#ifdef MAPNIK_DEBUG
std::clog << "OGR Plugin: unknown <ogr> geometry_type=" << wkbFlatten (geom->getGeometryType()) << std::endl;
#endif
break;
}
}
void ogr_converter::convert_point (OGRPoint* geom, feature_ptr feature)
{
geometry_type * point = new geometry_type(mapnik::Point);
point->move_to (geom->getX(), geom->getY());
feature->add_geometry (point);
}
void ogr_converter::convert_linestring (OGRLineString* geom, feature_ptr feature)
{
int num_points = geom->getNumPoints ();
geometry_type * line = new geometry_type(mapnik::LineString);
line->set_capacity (num_points);
line->move_to (geom->getX (0), geom->getY (0));
for (int i=1;i<num_points;++i)
{
line->line_to (geom->getX (i), geom->getY (i));
}
feature->add_geometry (line);
}
void ogr_converter::convert_polygon (OGRPolygon* geom, feature_ptr feature)
{
OGRLinearRing* exterior = geom->getExteriorRing ();
int num_points = exterior->getNumPoints ();
int num_interior = geom->getNumInteriorRings ();
int capacity = 0;
for (int r=0;r<num_interior;r++)
{
OGRLinearRing* interior = geom->getInteriorRing (r);
capacity += interior->getNumPoints ();
}
geometry_type * poly = new geometry_type(mapnik::Polygon);
poly->set_capacity (num_points + capacity);
poly->move_to (exterior->getX (0), exterior->getY (0));
for (int i=1;i<num_points;++i)
{
poly->line_to (exterior->getX (i), exterior->getY (i));
}
for (int r=0;r<num_interior;r++)
{
OGRLinearRing* interior = geom->getInteriorRing (r);
num_points = interior->getNumPoints ();
poly->move_to(interior->getX (0), interior->getY (0));
for (int i=1;i<num_points;++i)
{
poly->line_to(interior->getX (i), interior->getY (i));
}
}
feature->add_geometry (poly);
}
void ogr_converter::convert_multipoint (OGRMultiPoint* geom, feature_ptr feature)
{
int num_geometries = geom->getNumGeometries ();
geometry_type * point = new geometry_type(mapnik::Point);
for (int i=0;i<num_geometries;i++)
{
OGRPoint* ogrpoint = static_cast<OGRPoint*>(geom->getGeometryRef (i));
point->move_to (ogrpoint->getX(), ogrpoint->getY());
//Todo - need to accept multiple points per mapnik::Point
}
// Todo - this only gets last point
feature->add_geometry (point);
}
void ogr_converter::convert_multipoint_2 (OGRMultiPoint* geom, feature_ptr feature)
{
int num_geometries = geom->getNumGeometries ();
for (int i=0;i<num_geometries;i++)
{
convert_point (static_cast<OGRPoint*>(geom->getGeometryRef (i)), feature);
}
}
void ogr_converter::convert_multilinestring (OGRMultiLineString* geom, feature_ptr feature)
{
int num_geometries = geom->getNumGeometries ();
int num_points = 0;
for (int i=0;i<num_geometries;i++)
{
OGRLineString* ls = static_cast<OGRLineString*>(geom->getGeometryRef (i));
num_points += ls->getNumPoints ();
}
geometry_type * line = new geometry_type(mapnik::LineString);
line->set_capacity (num_points);
for (int i=0;i<num_geometries;i++)
{
OGRLineString* ls = static_cast<OGRLineString*>(geom->getGeometryRef (i));
num_points = ls->getNumPoints ();
line->move_to (ls->getX (0), ls->getY (0));
for (int i=1;i<num_points;++i)
{
line->line_to (ls->getX (i), ls->getY (i));
}
}
feature->add_geometry (line);
}
void ogr_converter::convert_multilinestring_2 (OGRMultiLineString* geom, feature_ptr feature)
{
int num_geometries = geom->getNumGeometries ();
for (int i=0;i<num_geometries;i++)
{
convert_linestring (static_cast<OGRLineString*>(geom->getGeometryRef (i)), feature);
}
}
void ogr_converter::convert_multipolygon (OGRMultiPolygon* geom, feature_ptr feature)
{
int num_geometries = geom->getNumGeometries ();
int capacity = 0;
for (int i=0;i<num_geometries;i++)
{
OGRPolygon* p = static_cast<OGRPolygon*>(geom->getGeometryRef (i));
OGRLinearRing* exterior = p->getExteriorRing ();
capacity += exterior->getNumPoints ();
for (int r=0;r<p->getNumInteriorRings ();r++)
{
OGRLinearRing* interior = p->getInteriorRing (r);
capacity += interior->getNumPoints ();
}
}
geometry_type * poly = new geometry_type(mapnik::Polygon);
poly->set_capacity (capacity);
for (int i=0;i<num_geometries;i++)
{
OGRPolygon* p = static_cast<OGRPolygon*>(geom->getGeometryRef (i));
OGRLinearRing* exterior = p->getExteriorRing ();
int num_points = exterior->getNumPoints ();
int num_interior = p->getNumInteriorRings ();
poly->move_to (exterior->getX (0), exterior->getY (0));
for (int i=1;i<num_points;++i)
{
poly->line_to (exterior->getX (i), exterior->getY (i));
}
for (int r=0;r<num_interior;r++)
{
OGRLinearRing* interior = p->getInteriorRing (r);
num_points = interior->getNumPoints ();
poly->move_to(interior->getX (0), interior->getY (0));
for (int i=1;i<num_points;++i)
{
poly->line_to(interior->getX (i), interior->getY (i));
}
}
}
feature->add_geometry (poly);
}
void ogr_converter::convert_multipolygon_2 (OGRMultiPolygon* geom, feature_ptr feature)
{
int num_geometries = geom->getNumGeometries ();
for (int i=0;i<num_geometries;i++)
{
convert_polygon (static_cast<OGRPolygon*>(geom->getGeometryRef (i)), feature);
}
}
void ogr_converter::convert_collection (OGRGeometryCollection* geom, feature_ptr feature, bool multiple_geometries)
{
int num_geometries = geom->getNumGeometries ();
for (int i=0;i<num_geometries;i++)
{
OGRGeometry* g = geom->getGeometryRef (i);
if (g != NULL)
{
convert_geometry (g, feature, multiple_geometries);
}
}
}
|