forked from systemed/tilemaker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathosm_store.cpp
More file actions
250 lines (208 loc) · 7.43 KB
/
osm_store.cpp
File metadata and controls
250 lines (208 loc) · 7.43 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
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
#include "osm_store.h"
#include <iostream>
#include <fstream>
#include <iterator>
#include <unordered_map>
#include <ciso646>
#include <boost/sort/sort.hpp>
#include "node_store.h"
#include "way_store.h"
using namespace std;
namespace bg = boost::geometry;
static inline bool isClosed(const std::vector<LatpLon>& way) {
return way.begin() == way.end();
}
UsedObjects::UsedObjects(Status status): status(status), mutex(256), ids(256 * 1024) {
}
bool UsedObjects::test(NodeID id) {
if (status == Status::Disabled)
return true;
const size_t chunk = id / 65536;
if (ids[chunk].size() == 0)
return false;
return ids[chunk][id % 65536];
}
void UsedObjects::enable() {
status = Status::Enabled;
}
bool UsedObjects::enabled() const {
return status == Status::Enabled;
}
void UsedObjects::set(NodeID id) {
const size_t chunk = id / 65536;
std::lock_guard<std::mutex> lock(mutex[chunk % mutex.size()]);
if (ids[chunk].size() == 0)
ids[chunk].resize(65536);
ids[chunk][id % 65536] = true;
}
void UsedObjects::clear() {
// This data is not needed after PbfProcessor's ReadPhase::Nodes has completed,
// and it takes up to ~1.5GB of RAM.
ids.clear();
}
void OSMStore::open(std::string const &osm_store_filename)
{
void_mmap_allocator::openMmapFile(osm_store_filename);
reopen();
}
MultiPolygon OSMStore::wayListMultiPolygon(WayVec::const_iterator outerBegin, WayVec::const_iterator outerEnd, WayVec::const_iterator innerBegin, WayVec::const_iterator innerEnd) const {
MultiPolygon mp;
if (outerBegin == outerEnd) { return mp; } // no outers so quit
std::vector<LatpLonDeque> outers;
std::vector<LatpLonDeque> inners;
std::map<WayID,bool> done; // true=this way has already been added to outers/inners, don't reconsider
// merge constituent ways together
mergeMultiPolygonWays(outers, done, outerBegin, outerEnd);
mergeMultiPolygonWays(inners, done, innerBegin, innerEnd);
// add all inners and outers to the multipolygon
std::vector<Ring> filledInners;
for (auto it = inners.begin(); it != inners.end(); ++it) {
Ring inner;
fillPoints(inner, it->begin(), it->end());
filledInners.emplace_back(inner);
}
bool onlyOneOuter = outers.size()==1;
for (auto ot = outers.begin(); ot != outers.end(); ot++) {
Polygon poly;
fillPoints(poly.outer(), ot->begin(), ot->end());
for (auto it = filledInners.begin(); it != filledInners.end(); ++it) {
if (onlyOneOuter || geom::within(*it, poly.outer())) { poly.inners().emplace_back(*it); }
}
mp.emplace_back(move(poly));
}
// fix winding
geom::correct(mp);
return mp;
}
MultiLinestring OSMStore::wayListMultiLinestring(WayVec::const_iterator outerBegin, WayVec::const_iterator outerEnd) const {
MultiLinestring mls;
if (outerBegin == outerEnd) { return mls; }
std::vector<LatpLonDeque> linestrings;
std::map<WayID,bool> done;
mergeMultiPolygonWays(linestrings, done, outerBegin, outerEnd);
for (auto ot = linestrings.begin(); ot != linestrings.end(); ot++) {
Linestring ls;
fillPoints(ls, ot->begin(), ot->end());
mls.emplace_back(move(ls));
}
return mls;
}
// Assemble multipolygon constituent ways
// - Any closed polygons are added as-is
// - Linestrings are joined to existing linestrings with which they share a start/end
// - If no matches can be found, then one linestring is added (to 'attract' others)
// - The process is rerun until no ways are left
// There's quite a lot of copying going on here - could potentially be addressed
void OSMStore::mergeMultiPolygonWays(std::vector<LatpLonDeque> &results, std::map<WayID,bool> &done, WayVec::const_iterator itBegin, WayVec::const_iterator itEnd) const {
// Create maps of start/end nodes
std::unordered_map<LatpLon, std::vector<WayID>> startNodes;
std::unordered_map<LatpLon, std::vector<WayID>> endNodes;
for (auto it = itBegin; it != itEnd; ++it) {
if (done[*it]) { continue; }
try {
auto const &way = ways.at(*it);
if (isClosed(way) || results.empty()) {
// if start==end, simply add it to the set
results.emplace_back(way.begin(), way.end());
done[*it] = true;
} else {
startNodes[way.front()].emplace_back(*it);
endNodes[way.back()].emplace_back(*it);
}
} catch (std::out_of_range &err) {
if (verbose) { cerr << "Missing way in relation: " << err.what() << endl; }
done[*it] = true;
}
}
auto deleteFromWayList = [&](LatpLon n, WayID w, bool which) {
auto &nodemap = which ? startNodes : endNodes;
std::vector<WayID> &waylist = nodemap.find(n)->second;
waylist.erase(std::remove(waylist.begin(), waylist.end(), w), waylist.end());
if (waylist.empty()) { nodemap.erase(nodemap.find(n)); }
};
auto removeWay = [&](WayID w) {
auto const &way = ways.at(w);
LatpLon first = way.front();
LatpLon last = way.back();
if (startNodes.find(first) != startNodes.end()) { deleteFromWayList(first, w, true ); }
if (startNodes.find(last) != startNodes.end()) { deleteFromWayList(last, w, true ); }
if (endNodes.find(first) != endNodes.end() ) { deleteFromWayList(first, w, false); }
if (endNodes.find(last) != endNodes.end() ) { deleteFromWayList(last, w, false); }
done[w] = true;
};
// Loop through, repeatedly adding start/end nodes if we can
int added;
do {
added = 0;
for (auto rt = results.begin(); rt != results.end(); rt++) {
bool working=true;
do {
LatpLon rFirst = rt->front();
LatpLon rLast = rt->back();
if (rFirst==rLast) break;
if (startNodes.find(rLast)!=startNodes.end()) {
// append to the result
auto match = startNodes.find(rLast)->second;
auto nodes = ways.at(match.back());
rt->insert(rt->end(), nodes.begin(), nodes.end());
removeWay(match.back());
added++;
} else if (endNodes.find(rLast)!=endNodes.end()) {
// append reversed to the original
auto match = endNodes.find(rLast)->second;
auto nodes = ways.at(match.back());
rt->insert(rt->end(),
std::make_reverse_iterator(nodes.end()),
std::make_reverse_iterator(nodes.begin()));
removeWay(match.back());
added++;
} else if (endNodes.find(rFirst)!=endNodes.end()) {
// prepend to the original
auto match = endNodes.find(rFirst)->second;
auto nodes = ways.at(match.back());
rt->insert(rt->begin(), nodes.begin(), nodes.end());
removeWay(match.back());
added++;
} else if (startNodes.find(rFirst)!=startNodes.end()) {
// prepend reversed to the original
auto match = startNodes.find(rFirst)->second;
auto nodes = ways.at(match.back());
rt->insert(rt->begin(),
std::make_reverse_iterator(nodes.end()),
std::make_reverse_iterator(nodes.begin()));
removeWay(match.back());
added++;
} else { working=false; }
} while (working);
}
// If nothing was added, then 'seed' it with a remaining unallocated way
for (int i=0; i<=1; i++) {
if (added>0) continue;
for (auto nt : (i==0 ? startNodes : endNodes)) {
WayID w = nt.second.back();
auto const &way = ways.at(w);
results.emplace_back(way.begin(), way.end());
added++;
removeWay(w);
break;
}
}
} while (added>0);
};
void OSMStore::reportSize() const {
std::cout << "Stored " << nodes.size() << " nodes, " << ways.size() << " ways, " << relations.size() << " relations" << std::endl;
}
void OSMStore::reopen() {
nodes.reopen();
ways.reopen();
relations.reopen();
}
void OSMStore::ensureUsedWaysInited() {
if (!used_ways.inited) used_ways.reserve(use_compact_nodes, nodes.size());
}
void OSMStore::clear() {
nodes.clear();
ways.clear();
relations.clear();
used_ways.clear();
}