forked from MaZderMind/osm-history-splitter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cut.hpp
136 lines (103 loc) · 3.64 KB
/
cut.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
#ifndef SPLITTER_CUT_HPP
#define SPLITTER_CUT_HPP
#include <geos/io/WKTWriter.h>
#include <osmium/handler/progress.hpp>
#include <osmium/output.hpp>
#include "geometryreader.hpp"
#include "growing_bitset.hpp"
// information about a single extract
class ExtractInfo {
public:
enum ExtractMode {
LOCATOR = 1,
BOUNDS = 2
};
std::string name;
geos::algorithm::locate::IndexedPointInAreaLocator *locator;
Osmium::OSM::Bounds bounds;
Osmium::Output::Base *writer;
ExtractMode mode;
ExtractInfo(std::string name) : locator(NULL), writer(NULL) {
this->name = name;
}
~ExtractInfo() {
if(locator) delete locator;
if(writer) delete writer;
}
bool contains(const shared_ptr<Osmium::OSM::Node const>& node) {
if(mode == BOUNDS) {
return
(node->lon() > bounds.bottom_left().lon()) &&
(node->lat() > bounds.bottom_left().lat()) &&
(node->lon() < bounds.top_right().lon()) &&
(node->lat() < bounds.top_right().lat());
}
else if(mode == LOCATOR) {
// BOUNDARY 1
// EXTERIOR 2
// INTERIOR 0
geos::geom::Coordinate c = geos::geom::Coordinate(node->lon(), node->lat(), DoubleNotANumber);
return (0 == locator->locate(&c));
}
return false;
}
};
// information about the cutting algorithm
template <class TExtractInfo>
class CutInfo {
protected:
~CutInfo() {
for(int i=0, l = extracts.size(); i<l; i++) {
extracts[i]->writer->final();
delete extracts[i];
}
}
public:
std::vector<TExtractInfo*> extracts;
TExtractInfo *addExtract(std::string name, double minlon, double minlat, double maxlon, double maxlat) {
fprintf(stderr, "opening writer for %s\n", name.c_str());
Osmium::OSMFile outfile(name);
Osmium::Output::Base *writer = Osmium::Output::Factory::instance().create_output(outfile);
const Osmium::OSM::Position min(minlat, minlon);
const Osmium::OSM::Position max(maxlat, maxlon);
Osmium::OSM::Bounds bounds;
bounds.extend(min).extend(max);
Osmium::OSM::Meta meta(bounds);
writer->init(meta);
TExtractInfo *ex = new TExtractInfo(name);
ex->writer = writer;
ex->bounds = bounds;
ex->mode = ExtractInfo::BOUNDS;
extracts.push_back(ex);
return ex;
}
TExtractInfo *addExtract(std::string name, geos::geom::Geometry *poly) {
fprintf(stderr, "opening writer for %s\n", name.c_str());
Osmium::OSMFile outfile(name);
Osmium::Output::Base *writer = Osmium::Output::Factory::instance().create_output(outfile);
const geos::geom::Envelope *env = poly->getEnvelopeInternal();
const Osmium::OSM::Position min(env->getMinX(), env->getMinY());
const Osmium::OSM::Position max(env->getMaxX(), env->getMaxY());
Osmium::OSM::Bounds bounds;
bounds.extend(min).extend(max);
Osmium::OSM::Meta meta(bounds);
writer->init(meta);
TExtractInfo *ex = new TExtractInfo(name);
ex->writer = writer;
ex->locator = new geos::algorithm::locate::IndexedPointInAreaLocator(*poly);
ex->mode = ExtractInfo::LOCATOR;
Osmium::Geometry::geos_geometry_factory()->destroyGeometry(poly);
extracts.push_back(ex);
return ex;
}
};
template <class TCutInfo>
class Cut : public Osmium::Handler::Base {
protected:
Osmium::Handler::Progress pg;
TCutInfo *info;
public:
bool debug;
Cut(TCutInfo *info) : info(info) {}
};
#endif // SPLITTER_CUT_HPP