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
|
/*
#-----------------------------------------------------------------------------
# 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 <geos_c.h>
#if (GEOS_VERSION_MAJOR==3)
/* geos trunk (3.0.0rc) */
#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/io/WKTReader.h>
#include <geos/io/WKTWriter.h>
#include <geos/opLinemerge.h>
using namespace geos::geom;
using namespace geos::io;
using namespace geos::operation::linemerge;
#else
/* geos-2.2 */
#include <geos/geom.h>
#include <geos/io.h>
#include <geos/opLinemerge.h>
using namespace geos;
#endif
#include "build_geometry.h"
struct Segment
{
Segment(double x0_,double y0_,double x1_,double y1_)
:x0(x0_),y0(y0_),x1(x1_),y1(y1_) {}
double x0;
double y0;
double x1;
double y1;
};
static std::vector<Segment> segs;
static std::vector<std::string> wkts;
typedef std::auto_ptr<Geometry> geom_ptr;
int is_simple(const char* wkt)
{
GeometryFactory factory;
WKTReader reader(&factory);
geom_ptr geom(reader.read(wkt));
if (geom->isSimple()) return 1;
return 0;
}
void add_segment(double x0,double y0,double x1,double y1)
{
segs.push_back(Segment(x0,y0,x1,y1));
}
const char * get_wkt(size_t index)
{
return wkts[index].c_str();
}
void clear_wkts()
{
wkts.clear();
}
size_t build_geometry(int polygon)
{
size_t wkt_size = 0;
GeometryFactory factory;
geom_ptr segment(0);
std::auto_ptr<std::vector<Geometry*> > lines(new std::vector<Geometry*>);
std::vector<Segment>::const_iterator pos=segs.begin();
std::vector<Segment>::const_iterator end=segs.end();
bool first=true;
try {
while (pos != end)
{
if (pos->x0 != pos->x1 || pos->y0 != pos->y1)
{
std::auto_ptr<CoordinateSequence> coords(factory.getCoordinateSequenceFactory()->create(0,2));
coords->add(Coordinate(pos->x0,pos->y0));
coords->add(Coordinate(pos->x1,pos->y1));
geom_ptr linestring(factory.createLineString(coords.release()));
if (first)
{
segment = linestring;
first=false;
}
else
{
lines->push_back(linestring.release());
}
}
++pos;
}
segs.clear();
if (segment.get())
{
geom_ptr mline (factory.createMultiLineString(lines.release()));
geom_ptr noded (segment->Union(mline.get()));
LineMerger merger;
merger.add(noded.get());
std::auto_ptr<std::vector<LineString *> > merged(merger.getMergedLineStrings());
WKTWriter writer;
for (unsigned i=0 ;i < merged->size(); ++i)
{
std::auto_ptr<LineString> pline ((*merged ) [i]);
if (polygon == 1 && pline->getNumPoints() > 3 && pline->isClosed())
{
std::auto_ptr<LinearRing> ring(factory.createLinearRing(pline->getCoordinates()));
geom_ptr poly(factory.createPolygon(ring.release(),0));
std::string text = writer.write(poly.get());
wkts.push_back(text);
++wkt_size;
}
else
{
std::string text = writer.write(pline.get());
wkts.push_back(text);
++wkt_size;
}
}
}
}
catch (...)
{
std::cerr << "excepton caught \n";
wkt_size = 0;
}
return wkt_size;
}
|