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 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
|
/*
#-----------------------------------------------------------------------------
# Part of osm2pgsql utility
#-----------------------------------------------------------------------------
# By Artem Pavlenko, Copyright 2007
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#-----------------------------------------------------------------------------
*/
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <exception>
/* Need to know which geos version we have to work out which headers to include */
#include <geos/version.h>
/* geos (3.0.0+) */
#if (GEOS_VERSION_MAJOR==3)
#if (GEOS_VERSION_MINOR>=1)
/* Prepared geometries are new in 3.1.0 */
#define HAS_PREPARED_GEOMETRIES
#include <geos/geom/prep/PreparedGeometryFactory.h>
#include <geos/geom/prep/PreparedPolygon.h>
#endif
#include <geos/geom/GeometryFactory.h>
#include <geos/geom/CoordinateSequenceFactory.h>
#include <geos/geom/Geometry.h>
#include <geos/geom/LineString.h>
#include <geos/geom/LinearRing.h>
#include <geos/geom/MultiLineString.h>
#include <geos/geom/Polygon.h>
#include <geos/geom/MultiPolygon.h>
#include <geos/geom/Point.h>
#include <geos/io/WKTReader.h>
#include <geos/io/WKTWriter.h>
#include <geos/util/GEOSException.h>
#include <geos/opLinemerge.h>
using namespace geos::geom;
using namespace geos::io;
using namespace geos::util;
using namespace geos::operation::linemerge;
#else
/* geos-2.2.3 */
#include <geos/geom.h>
#include <geos/io.h>
#include <geos/opLinemerge.h>
using namespace geos;
#endif
#include "build_geometry.h"
typedef std::auto_ptr<Geometry> geom_ptr;
static std::vector<std::string> wkts;
static std::vector<double> areas;
char *get_wkt_simple(osmNode *nodes, int count, int polygon) {
GeometryFactory gf;
std::auto_ptr<CoordinateSequence> coords(gf.getCoordinateSequenceFactory()->create((size_t)0, (size_t)2));
try
{
for (int i = 0; i < count ; i++) {
Coordinate c;
c.x = nodes[i].lon;
c.y = nodes[i].lat;
coords->add(c, 0);
}
geom_ptr geom;
if (polygon && (coords->getSize() >= 4) && (coords->getAt(coords->getSize() - 1).equals2D(coords->getAt(0)))) {
std::auto_ptr<LinearRing> shell(gf.createLinearRing(coords.release()));
geom = geom_ptr(gf.createPolygon(shell.release(), new std::vector<Geometry *>));
geom->normalize(); // Fix direction of ring
} else {
if (coords->getSize() < 2)
return NULL;
geom = geom_ptr(gf.createLineString(coords.release()));
}
WKTWriter wktw;
std::string wkt = wktw.write(geom.get());
return strdup(wkt.c_str());
}
catch (std::bad_alloc)
{
std::cerr << std::endl << "Exception caught processing way. You are likelly running out of memory." << std::endl;
std::cerr << "Try in slim mode, using -s parameter." << std::endl;
return NULL;
}
catch (...)
{
std::cerr << std::endl << "Exception caught processing way" << std::endl;
return NULL;
}
}
size_t get_wkt_split(osmNode *nodes, int count, int polygon, double split_at) {
GeometryFactory gf;
std::auto_ptr<CoordinateSequence> coords(gf.getCoordinateSequenceFactory()->create((size_t)0, (size_t)2));
double area;
WKTWriter wktw;
size_t wkt_size = 0;
try
{
for (int i = 0; i < count ; i++) {
Coordinate c;
c.x = nodes[i].lon;
c.y = nodes[i].lat;
coords->add(c, 0);
}
geom_ptr geom;
if (polygon && (coords->getSize() >= 4) && (coords->getAt(coords->getSize() - 1).equals2D(coords->getAt(0)))) {
std::auto_ptr<LinearRing> shell(gf.createLinearRing(coords.release()));
geom = geom_ptr(gf.createPolygon(shell.release(), new std::vector<Geometry *>));
geom->normalize(); // Fix direction of ring
area = geom->getArea();
std::string wkt = wktw.write(geom.get());
wkts.push_back(wkt);
areas.push_back(area);
wkt_size++;
} else {
if (coords->getSize() < 2)
return 0;
double distance = 0;
std::auto_ptr<CoordinateSequence> segment;
segment = std::auto_ptr<CoordinateSequence>(gf.getCoordinateSequenceFactory()->create((size_t)0, (size_t)2));
segment->add(coords->getAt(0));
for(unsigned i=1; i<coords->getSize(); i++) {
segment->add(coords->getAt(i));
distance += coords->getAt(i).distance(coords->getAt(i-1));
if ((distance >= split_at) || (i == coords->getSize()-1)) {
geom = geom_ptr(gf.createLineString(segment.release()));
std::string wkt = wktw.write(geom.get());
wkts.push_back(wkt);
areas.push_back(0);
wkt_size++;
distance=0;
segment = std::auto_ptr<CoordinateSequence>(gf.getCoordinateSequenceFactory()->create((size_t)0, (size_t)2));
segment->add(coords->getAt(i));
}
}
}
}
catch (std::bad_alloc)
{
std::cerr << std::endl << "Exception caught processing way. You are likelly running out of memory." << std::endl;
std::cerr << "Try in slim mode, using -s parameter." << std::endl;
wkt_size = 0;
}
catch (...)
{
std::cerr << std::endl << "Exception caught processing way" << std::endl;
wkt_size = 0;
}
return wkt_size;
}
char * get_wkt(size_t index)
{
// return wkts[index].c_str();
char *result;
result = (char*) std::malloc( wkts[index].length() + 1);
// At least give some idea of why we about to seg fault
if (!result) std::cerr << std::endl << "Unable to allocate memory: " << (wkts[index].length() + 1) << std::endl;
std::strcpy(result, wkts[index].c_str());
return result;
}
double get_area(size_t index)
{
return areas[index];
}
void clear_wkts()
{
wkts.clear();
areas.clear();
}
static int coords2nodes(CoordinateSequence * coords, struct osmNode ** nodes) {
size_t num_coords;
size_t i;
Coordinate coord;
num_coords = coords->getSize();
*nodes = (struct osmNode *) malloc(num_coords * sizeof(struct osmNode));
for (i = 0; i < num_coords; i++) {
coord = coords->getAt(i);
(*nodes)[i].lon = coord.x;
(*nodes)[i].lat = coord.y;
}
return num_coords;
}
int parse_wkt(const char * wkt, struct osmNode *** xnodes, int ** xcount, int * polygon) {
GeometryFactory gf;
WKTReader reader(&gf);
std::string wkt_string(wkt);
Geometry * geometry;
const Geometry * subgeometry;
GeometryCollection * gc;
CoordinateSequence * coords;
size_t num_geometries;
size_t i;
*polygon = 0;
try {
geometry = reader.read(wkt_string);
switch (geometry->getGeometryTypeId()) {
// Single geometries
case GEOS_POLYGON:
// Drop through
case GEOS_LINEARRING:
*polygon = 1;
// Drop through
case GEOS_POINT:
// Drop through
case GEOS_LINESTRING:
*xnodes = (struct osmNode **) malloc(2 * sizeof(struct osmNode *));
*xcount = (int *) malloc(sizeof(int));
coords = geometry->getCoordinates();
(*xcount)[0] = coords2nodes(coords, &((*xnodes)[0]));
(*xnodes)[1] = NULL;
delete coords;
break;
// Geometry collections
case GEOS_MULTIPOLYGON:
*polygon = 1;
// Drop through
case GEOS_MULTIPOINT:
// Drop through
case GEOS_MULTILINESTRING:
gc = dynamic_cast<GeometryCollection *>(geometry);;
num_geometries = gc->getNumGeometries();
*xnodes = (struct osmNode **) malloc((num_geometries + 1) * sizeof(struct osmNode *));
*xcount = (int *) malloc(num_geometries * sizeof(int));
for (i = 0; i < num_geometries; i++) {
subgeometry = gc->getGeometryN(i);
coords = subgeometry->getCoordinates();
(*xcount)[i] = coords2nodes(coords, &((*xnodes)[i]));
delete coords;
}
(*xnodes)[i] = NULL;
break;
default:
std::cerr << std::endl << "unexpected object type while processing PostGIS data" << std::endl;
delete geometry;
return -1;
}
delete geometry;
} catch (...) {
std::cerr << std::endl << "Exception caught parsing PostGIS data" << std::endl;
return -1;
}
return 0;
}
struct polygondata
{
Polygon* polygon;
LinearRing* ring;
double area;
int iscontained;
unsigned containedbyid;
};
static int polygondata_comparearea(const void* vp1, const void* vp2)
{
const polygondata* p1 = (const polygondata*)vp1;
const polygondata* p2 = (const polygondata*)vp2;
if (p1->area == p2->area) return 0;
if (p1->area > p2->area) return -1;
return 1;
}
size_t build_geometry(osmid_t osm_id, struct osmNode **xnodes, int *xcount, int make_polygon, int enable_multi, double split_at) {
size_t wkt_size = 0;
std::auto_ptr<std::vector<Geometry*> > lines(new std::vector<Geometry*>);
GeometryFactory gf;
geom_ptr geom;
#ifdef HAS_PREPARED_GEOMETRIES
geos::geom::prep::PreparedGeometryFactory pgf;
#endif
try
{
for (int c=0; xnodes[c]; c++) {
std::auto_ptr<CoordinateSequence> coords(gf.getCoordinateSequenceFactory()->create((size_t)0, (size_t)2));
for (int i = 0; i < xcount[c]; i++) {
struct osmNode *nodes = xnodes[c];
Coordinate c;
c.x = nodes[i].lon;
c.y = nodes[i].lat;
coords->add(c, 0);
}
if (coords->getSize() > 1) {
geom = geom_ptr(gf.createLineString(coords.release()));
lines->push_back(geom.release());
}
}
//geom_ptr segment(0);
geom_ptr mline (gf.createMultiLineString(lines.release()));
//geom_ptr noded (segment->Union(mline.get()));
LineMerger merger;
//merger.add(noded.get());
merger.add(mline.get());
std::auto_ptr<std::vector<LineString *> > merged(merger.getMergedLineStrings());
WKTWriter writer;
// Procces ways into lines or simple polygon list
polygondata* polys = new polygondata[merged->size()];
unsigned totalpolys = 0;
for (unsigned i=0 ;i < merged->size(); ++i)
{
std::auto_ptr<LineString> pline ((*merged ) [i]);
if (make_polygon && pline->getNumPoints() > 3 && pline->isClosed())
{
polys[totalpolys].polygon = gf.createPolygon(gf.createLinearRing(pline->getCoordinates()),0);
polys[totalpolys].ring = gf.createLinearRing(pline->getCoordinates());
polys[totalpolys].area = polys[totalpolys].polygon->getArea();
polys[totalpolys].iscontained = 0;
polys[totalpolys].containedbyid = 0;
if (polys[totalpolys].area > 0.0)
totalpolys++;
else {
delete(polys[totalpolys].polygon);
delete(polys[totalpolys].ring);
}
}
else
{
//std::cerr << "polygon(" << osm_id << ") is no good: points(" << pline->getNumPoints() << "), closed(" << pline->isClosed() << "). " << writer.write(pline.get()) << std::endl;
double distance = 0;
std::auto_ptr<CoordinateSequence> segment;
segment = std::auto_ptr<CoordinateSequence>(gf.getCoordinateSequenceFactory()->create((size_t)0, (size_t)2));
segment->add(pline->getCoordinateN(0));
for(unsigned i=1; i<pline->getNumPoints(); i++) {
segment->add(pline->getCoordinateN(i));
distance += pline->getCoordinateN(i).distance(pline->getCoordinateN(i-1));
if ((distance >= split_at) || (i == pline->getNumPoints()-1)) {
geom = geom_ptr(gf.createLineString(segment.release()));
std::string wkt = writer.write(geom.get());
wkts.push_back(wkt);
areas.push_back(0);
wkt_size++;
distance=0;
segment = std::auto_ptr<CoordinateSequence>(gf.getCoordinateSequenceFactory()->create((size_t)0, (size_t)2));
segment->add(pline->getCoordinateN(i));
}
}
//std::string text = writer.write(pline.get());
//wkts.push_back(text);
//areas.push_back(0.0);
//wkt_size++;
}
}
if (totalpolys)
{
qsort(polys, totalpolys, sizeof(polygondata), polygondata_comparearea);
unsigned toplevelpolygons = 0;
int istoplevelafterall;
for (unsigned i=0 ;i < totalpolys; ++i)
{
if (polys[i].iscontained != 0) continue;
toplevelpolygons++;
#ifdef HAS_PREPARED_GEOMETRIES
const geos::geom::prep::PreparedGeometry* preparedtoplevelpolygon = pgf.create(polys[i].polygon);
#endif
for (unsigned j=i+1; j < totalpolys; ++j)
{
#ifdef HAS_PREPARED_GEOMETRIES
// Does preparedtoplevelpolygon contain the smaller polygon[j]?
if (polys[j].containedbyid == 0 && preparedtoplevelpolygon->contains(polys[j].polygon))
#else
// Does polygon[i] contain the smaller polygon[j]?
if (polys[j].containedbyid == 0 && polys[i].polygon->contains(polys[j].polygon))
#endif
{
// are we in a [i] contains [k] contains [j] situation
// which would actually make j top level
istoplevelafterall = 0;
for (unsigned k=i+1; k < j; ++k)
{
if (polys[k].iscontained && polys[k].containedbyid == i && polys[k].polygon->contains(polys[j].polygon))
{
istoplevelafterall = 1;
break;
}
#if 0
else if (polys[k].polygon->intersects(polys[j].polygon) || polys[k].polygon->touches(polys[j].polygon))
{
// FIXME: This code does not work as intended
// It should be setting the polys[k].ring in order to update this object
// but the value of polys[k].polygon calculated is normally NULL
// Add polygon this polygon (j) to k since they intersect
// Mark ourselfs to be dropped (2), delete the original k
Geometry* polyunion = polys[k].polygon->Union(polys[j].polygon);
delete(polys[k].polygon);
polys[k].polygon = dynamic_cast<Polygon*>(polyunion);
polys[j].iscontained = 2; // Drop
istoplevelafterall = 2;
break;
}
#endif
}
if (istoplevelafterall == 0)
{
polys[j].iscontained = 1;
polys[j].containedbyid = i;
}
}
}
#ifdef HAS_PREPARED_GEOMETRIES
pgf.destroy(preparedtoplevelpolygon);
#endif
}
// polys now is a list of ploygons tagged with which ones are inside each other
// List of polygons for multipolygon
std::auto_ptr<std::vector<Geometry*> > polygons(new std::vector<Geometry*>);
// For each top level polygon create a new polygon including any holes
for (unsigned i=0 ;i < totalpolys; ++i)
{
if (polys[i].iscontained != 0) continue;
// List of holes for this top level polygon
std::auto_ptr<std::vector<Geometry*> > interior(new std::vector<Geometry*>);
for (unsigned j=i+1; j < totalpolys; ++j)
{
if (polys[j].iscontained == 1 && polys[j].containedbyid == i)
{
interior->push_back(polys[j].ring);
}
}
Polygon* poly(gf.createPolygon(polys[i].ring, interior.release()));
poly->normalize();
polygons->push_back(poly);
}
// Make a multipolygon if required
if ((toplevelpolygons > 1) && enable_multi)
{
std::auto_ptr<MultiPolygon> multipoly(gf.createMultiPolygon(polygons.release()));
std::string text = writer.write(multipoly.get());
wkts.push_back(text);
areas.push_back(multipoly->getArea());
wkt_size++;
}
else
{
for(unsigned i=0; i<toplevelpolygons; i++)
{
Polygon* poly = dynamic_cast<Polygon*>(polygons->at(i));;
std::string text = writer.write(poly);
wkts.push_back(text);
areas.push_back(poly->getArea());
wkt_size++;
delete(poly);
}
}
}
for (unsigned i=0; i < totalpolys; ++i)
{
delete(polys[i].polygon);
}
delete[](polys);
}
catch (std::exception& e)
{
std::cerr << std::endl << "Standard exception processing way_id "<< osm_id << ": " << e.what() << std::endl;
wkt_size = 0;
}
catch (...)
{
std::cerr << std::endl << "Exception caught processing way id=" << osm_id << std::endl;
wkt_size = 0;
}
return wkt_size;
}
|