forked from MaZderMind/osm-history-splitter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
geometryreader.hpp
321 lines (263 loc) · 11.9 KB
/
geometryreader.hpp
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
#ifndef OSMIUMEX_GEOMBUILDER_HPP
#define OSMIUMEX_GEOMBUILDER_HPP
#include <cstring>
#include <geos/geom/Coordinate.h>
#include <geos/geom/CoordinateSequence.h>
#include <geos/geom/CoordinateSequenceFactory.h>
#include <geos/geom/GeometryFactory.h>
#include <geos/geom/LinearRing.h>
#include <geos/geom/MultiPolygon.h>
#include <geos/geom/Point.h>
#include <geos/geom/Polygon.h>
#include <geos/geom/PrecisionModel.h>
#include <geos/util/GEOSException.h>
#include <osmium/handler.hpp>
#include <osmium/handler/node_locations_for_ways.hpp>
#include <osmium/index/map/dense_mem_array.hpp>
#include <osmium/io/file.hpp>
#include <osmium/io/reader.hpp>
#include <osmium/visitor.hpp>
typedef osmium::index::map::DenseMemArray<osmium::unsigned_object_id_type, osmium::Location> storage_array_t;
typedef osmium::handler::NodeLocationsForWays<storage_array_t, storage_array_t> cfw_handler_t;
namespace OsmiumExtension {
geos::geom::GeometryFactory* geos_factory() {
static std::unique_ptr<const geos::geom::PrecisionModel> precision_model{new geos::geom::PrecisionModel};
static std::unique_ptr<geos::geom::GeometryFactory> factory{new geos::geom::GeometryFactory(precision_model.get(), -1)};
return factory.get();
}
class OsmGeometryReader : public osmium::handler::Handler {
std::vector<geos::geom::Geometry*> outer;
storage_array_t store_pos;
storage_array_t store_neg;
cfw_handler_t* handler_cfw;
private:
geos::geom::Geometry *polygonFromWay(const osmium::Way& way) const {
if (!way.is_closed()) {
std::cerr << "can't build way polygon geometry of unclosed way, leave it as nullptr\n";
return nullptr;
}
try {
std::vector<geos::geom::Coordinate> *c = new std::vector<geos::geom::Coordinate>;
for (const auto& node_ref : way.nodes()) {
c->emplace_back(node_ref.location().lon(), node_ref.location().lat());
}
geos::geom::CoordinateSequence *cs = geos_factory()->getCoordinateSequenceFactory()->create(c);
geos::geom::LinearRing *ring = geos_factory()->createLinearRing(cs);
return static_cast<geos::geom::Geometry *>(geos_factory()->createPolygon(ring, nullptr));
} catch (const geos::util::GEOSException& exc) {
std::cerr << "error building way geometry, leave it as nullptr\n";
return nullptr;
}
}
public:
OsmGeometryReader() :
Handler(),
store_pos(5000),
store_neg(1000) {
handler_cfw = new cfw_handler_t(store_pos, store_neg);
}
virtual ~OsmGeometryReader() {
delete handler_cfw;
for (uint32_t i=0; i < outer.size(); i++) {
geos_factory()->destroyGeometry(outer[i]);
}
outer.clear();
}
void node(const osmium::Node& node) {
handler_cfw->node(node);
}
void way(osmium::Way& way) {
handler_cfw->way(way);
if (!way.is_closed()) {
std::cerr << "open way " << way.id() << " in osm-input\n";
return;
}
geos::geom::Geometry *geom = polygonFromWay(way);
if (!geom) {
std::cerr << "error creating polygon from way\n";
return;
}
outer.push_back(geom);
}
geos::geom::Geometry *buildGeom() const {
geos::geom::MultiPolygon *outerPoly;
try {
outerPoly = geos_factory()->createMultiPolygon(outer);
} catch(geos::util::GEOSException e) {
std::cerr << "error creating multipolygon: " << e.what() << "\n";
return nullptr;
}
return outerPoly;
}
};
class GeometryReader {
/// maximum length of a line in a .poly file
static const int polyfile_linelen = 2048;
public:
/**
* read a .poly file and generate a geos Geometry from it.
*
* .poly-files are simple:
* - a title
* - 1..n polygons
* - a polygon number, possibly with a ! in front so signalize holes
* - 1..n lines with floating point latitudes and longitudes
* - END token
* - END token
*
* usually the Geometry read from .poly files are used together with an
* geos::algorithm::locate::IndexedPointInAreaLocator to check for nodes
* being located inside the polygon.
*
* this method returns nullptr if the .poly file can't be read.
*/
static geos::geom::Geometry *fromPolyFile(const std::string &file) {
// pointer to coordinate vector
std::vector<geos::geom::Coordinate> *c = nullptr;
// vectors of outer and inner polygons
std::vector<geos::geom::Geometry*> *outer = new std::vector<geos::geom::Geometry*>();
std::vector<geos::geom::Geometry*> *inner = new std::vector<geos::geom::Geometry*>();
// file pointer to .poly file
FILE *fp = fopen(file.c_str(), "r");
if (!fp) {
std::cerr << "unable to open polygon file " << file << "\n";
return nullptr;
}
// line buffer
char line[polyfile_linelen];
// read title line
if (!fgets(line, polyfile_linelen-1, fp)) {
std::cerr << "unable to read title line from polygon file " << file << "\n";
return nullptr;
}
line[polyfile_linelen-1] = '\0';
// is this polygon an inner polygon
bool isinner = false;
// are we currently inside parsing one polygon
bool ispoly = false;
// double x / y coords
double x = 0, y = 0;
// read through the file
while (!feof(fp)) {
// read a line
if (!fgets(line, polyfile_linelen-1, fp)) {
std::cerr << "unable to read line from polygon file " << file << "\n";
return nullptr;
}
line[polyfile_linelen-1] = '\0';
// when we're currently outside a polygon
if (!ispoly) {
// if this is an end-line
if (0 == strncmp(line, "END", 3)) {
// cancel parsing
break;
}
// this is considered a polygon-start line
// if it begins with ! it signales the start of an inner polygon
isinner = (line[0] == '!');
// remember we're inside a polygon
ispoly = true;
// create a new coordinate sequence
c = new std::vector<geos::geom::Coordinate>();
// when we're currently inside a polygon
} else {
// if this is an end-line
if (0 == strncmp(line, "END", 3)) {
if (!c) {
std::cerr << "empty polygon file\n";
return nullptr;
}
// check if the polygon is closed
if (c->front() != c->back()) {
std::cerr << "auto-closing unclosed polygon\n";
c->push_back(c->front());
}
// build a polygon from the coordinate vector
geos::geom::Geometry* poly;
try {
poly = geos_factory()->createPolygon(
geos_factory()->createLinearRing(
geos_factory()->getCoordinateSequenceFactory()->create(c)
),
nullptr
);
} catch(geos::util::GEOSException e) {
std::cerr << "error creating polygon: " << e.what() << "\n";
return nullptr;
}
// add it to the appropriate polygon vector
if (isinner) {
inner->push_back(poly);
} else {
outer->push_back(poly);
}
// remember we're now outside a polygon
ispoly = false;
// an ordinary line
} else {
// try to parse it using sscanf
if (2 != sscanf(line, " %lE %lE", &x, &y)) {
std::cerr << "unable to parse line from polygon file " << file << ": " << line;
return nullptr;
}
// push the parsed coordinate into the coordinate vector
c->push_back(geos::geom::Coordinate(x, y, DoubleNotANumber));
}
}
}
// check that the file ended with END
if (0 != strncmp(line, "END", 3)) {
std::cerr << "polygon file " << file << " does not end with END token\n";
return nullptr;
}
// close the file pointer
fclose(fp);
// build MultiPolygons from the vectors of outer and inner polygons
geos::geom::Geometry *poly;
try {
geos::geom::MultiPolygon *outerPoly = geos_factory()->createMultiPolygon(outer);
geos::geom::MultiPolygon *innerPoly = geos_factory()->createMultiPolygon(inner);
// generate a MultiPolygon containing the difference of those two
poly = outerPoly->difference(innerPoly);
// destroy the both MultiPolygons
geos_factory()->destroyGeometry(outerPoly);
geos_factory()->destroyGeometry(innerPoly);
} catch(geos::util::GEOSException e) {
std::cerr << "error creating differential multipolygon: " << e.what() << "\n";
return nullptr;
}
// and return their difference
return poly;
} // fromPolyFile
static geos::geom::Geometry *fromOsmFile(const std::string &file) {
osmium::io::File infile(file);
OsmiumExtension::OsmGeometryReader reader_handler;
osmium::io::Reader reader(infile);
osmium::apply(reader, reader_handler);
geos::geom::Geometry *geom = reader_handler.buildGeom();
return geom;
}
/**
* construct a geos Geometry from a BoundingBox string.
*
* this method returns nullptr if the string can't be read.
*/
static geos::geom::Geometry *fromBBox(const std::string &bbox) {
double minlon, minlat, maxlon, maxlat;
if (4 != sscanf(bbox.c_str(), "%lf,%lf,%lf,%lf", &minlon, &minlat, &maxlon, &maxlat)) {
std::cerr << "invalid BBox string: " << bbox << "\n";
return nullptr;
}
// build the Geometry from the coordinates
return fromBBox(minlon, minlat, maxlon, maxlat);
}
static geos::geom::Geometry *fromBBox(double minlon, double minlat, double maxlon, double maxlat) {
// create an Envelope and convert it to a polygon
geos::geom::Envelope *e = new geos::geom::Envelope(minlon, maxlon, minlat, maxlat);
geos::geom::Geometry *p = geos_factory()->toGeometry(e);
delete e;
return p;
}
}; // class GeomBuilder
} // namespace OsmiumExtension
#endif // OSMIUMEX_GEOMBUILDER_HPP