forked from MaZderMind/osm-history-splitter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcut.hpp
More file actions
147 lines (111 loc) · 3.86 KB
/
cut.hpp
File metadata and controls
147 lines (111 loc) · 3.86 KB
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
#ifndef SPLITTER_CUT_HPP
#define SPLITTER_CUT_HPP
#include <osmium/io/any_output.hpp>
#include "geometryreader.hpp"
// information about a single extract
class ExtractInfo {
public:
enum ExtractMode {
LOCATOR = 1,
BOUNDS = 2
};
std::string name;
geos::algorithm::locate::IndexedPointInAreaLocator *locator;
osmium::Box bounds;
osmium::io::Writer writer;
ExtractMode mode;
osmium::memory::Buffer m_buffer;
ExtractInfo(const std::string& name, const osmium::io::File& file, const osmium::io::Header& header) :
locator(nullptr),
writer(file, header),
m_buffer(1024*1024, osmium::memory::Buffer::auto_grow::yes) {
this->name = name;
}
~ExtractInfo() {
flush();
writer.close();
if (locator) delete locator;
}
bool contains(const osmium::Node& node) {
if (mode == BOUNDS) {
return
(node.location().lon() > bounds.bottom_left().lon()) &&
(node.location().lat() > bounds.bottom_left().lat()) &&
(node.location().lon() < bounds.top_right().lon()) &&
(node.location().lat() < bounds.top_right().lat());
} else if (mode == LOCATOR) {
// BOUNDARY 1
// EXTERIOR 2
// INTERIOR 0
geos::geom::Coordinate c = geos::geom::Coordinate(node.location().lon(), node.location().lat(), DoubleNotANumber);
return (0 == locator->locate(&c));
}
return false;
}
void flush() {
osmium::memory::Buffer new_buffer(1024*1024, osmium::memory::Buffer::auto_grow::yes);
using std::swap;
swap(m_buffer, new_buffer);
writer(std::move(new_buffer));
}
void write(const osmium::OSMObject& object) {
m_buffer.add_item(object);
m_buffer.commit();
if (m_buffer.committed() > 900 * 1024) {
flush();
}
}
};
// information about the cutting algorithm
template <class TExtractInfo>
class CutInfo {
protected:
~CutInfo() {
for (auto& extract : extracts) {
delete extract;
}
}
public:
std::vector<TExtractInfo*> extracts;
TExtractInfo *addExtract(const std::string& name, double minlon, double minlat, double maxlon, double maxlat) {
std::cerr << "opening writer for " << name.c_str() << "\n";
osmium::io::File outfile(name);
const osmium::Location min(minlat, minlon);
const osmium::Location max(maxlat, maxlon);
osmium::Box bounds;
bounds.extend(min).extend(max);
osmium::io::Header header;
header.add_box(bounds);
TExtractInfo *ex = new TExtractInfo(name, outfile, header);
ex->bounds = bounds;
ex->mode = ExtractInfo::BOUNDS;
extracts.push_back(ex);
return ex;
}
TExtractInfo *addExtract(const std::string& name, geos::geom::Geometry *poly) {
std::cerr << "opening writer for " << name.c_str() << "\n";
osmium::io::File outfile(name);
const geos::geom::Envelope *env = poly->getEnvelopeInternal();
const osmium::Location min(env->getMinX(), env->getMinY());
const osmium::Location max(env->getMaxX(), env->getMaxY());
osmium::Box bounds;
bounds.extend(min).extend(max);
osmium::io::Header header;
header.add_box(bounds);
TExtractInfo *ex = new TExtractInfo(name, outfile, header);
ex->locator = new geos::algorithm::locate::IndexedPointInAreaLocator(*poly);
ex->mode = ExtractInfo::LOCATOR;
//XXX Osmium::Geometry::geos_geometry_factory()->destroyGeometry(poly);
extracts.push_back(ex);
return ex;
}
};
template <class TCutInfo>
class Cut : public osmium::handler::Handler {
protected:
TCutInfo *info;
public:
bool debug;
Cut(TCutInfo *info) : info(info) {}
};
#endif // SPLITTER_CUT_HPP