|
@@ -8,11 +8,13 @@
|
8
|
8
|
#include <sstream>
|
9
|
9
|
#include <string>
|
10
|
10
|
#include "osmfixer/index/StatIdx.h"
|
|
11
|
+#include "util/geo/BezierCurve.h"
|
11
|
12
|
|
12
|
13
|
using osmfixer::StatIdx;
|
13
|
14
|
using osmfixer::OsmAttrs;
|
14
|
15
|
using osmfixer::Station;
|
15
|
16
|
using osmfixer::Group;
|
|
17
|
+using osmfixer::Suggestion;
|
16
|
18
|
|
17
|
19
|
// _____________________________________________________________________________
|
18
|
20
|
void StatIdx::readFromFile(const std::string& path) {
|
|
@@ -90,6 +92,7 @@ void StatIdx::readFromFile(const std::string& path) {
|
90
|
92
|
}
|
91
|
93
|
|
92
|
94
|
initGroups();
|
|
95
|
+ initSuggestions();
|
93
|
96
|
initIndex();
|
94
|
97
|
}
|
95
|
98
|
|
|
@@ -126,7 +129,7 @@ void StatIdx::initGroups() {
|
126
|
129
|
assert(_stations[i].group < _groups.size());
|
127
|
130
|
if (_stations[i].group != _stations[i].origGroup &&
|
128
|
131
|
_groups[_stations[i].group].osmid == 1) {
|
129
|
|
- std::cout << "MURR" << std::endl;
|
|
132
|
+ std::cout << "MURR" << std::endl;
|
130
|
133
|
// only add non-orig groups to polygons of new (non-osm) groups
|
131
|
134
|
_groups[_stations[i].group].stations.push_back(i);
|
132
|
135
|
}
|
|
@@ -157,8 +160,33 @@ void StatIdx::initIndex() {
|
157
|
160
|
false);
|
158
|
161
|
_ggrid = util::geo::Grid<size_t, util::geo::Polygon, double>(5000, 5000,
|
159
|
162
|
_bbox, false);
|
|
163
|
+ _suggrid = util::geo::Grid<size_t, util::geo::Line, double>(5000, 5000, _bbox,
|
|
164
|
+ false);
|
160
|
165
|
for (size_t i = 0; i < _stations.size(); i++) _sgrid.add(_stations[i].pos, i);
|
161
|
166
|
for (size_t i = 0; i < _groups.size(); i++) _ggrid.add(_groups[i].poly, i);
|
|
167
|
+ for (size_t i = 0; i < _suggestions.size(); i++)
|
|
168
|
+ _suggrid.add(_suggestions[i].arrow, i);
|
|
169
|
+}
|
|
170
|
+
|
|
171
|
+// _____________________________________________________________________________
|
|
172
|
+std::vector<const Suggestion*> StatIdx::getSuggestions(
|
|
173
|
+ const util::geo::DBox bbox) const {
|
|
174
|
+ std::vector<const Suggestion*> ret;
|
|
175
|
+ auto ll = util::geo::latLngToWebMerc<double>(bbox.getLowerLeft().getX(),
|
|
176
|
+ bbox.getLowerLeft().getY());
|
|
177
|
+ auto ur = util::geo::latLngToWebMerc<double>(bbox.getUpperRight().getX(),
|
|
178
|
+ bbox.getUpperRight().getY());
|
|
179
|
+
|
|
180
|
+ std::set<size_t> tmp;
|
|
181
|
+ auto reqBox = util::geo::DBox(ll, ur);
|
|
182
|
+ _suggrid.get(reqBox, &tmp);
|
|
183
|
+
|
|
184
|
+ for (auto i : tmp) {
|
|
185
|
+ if (util::geo::intersects(_suggestions[i].arrow, reqBox))
|
|
186
|
+ ret.push_back(&_suggestions[i]);
|
|
187
|
+ }
|
|
188
|
+
|
|
189
|
+ return ret;
|
162
|
190
|
}
|
163
|
191
|
|
164
|
192
|
// _____________________________________________________________________________
|
|
@@ -209,7 +237,118 @@ const Station* StatIdx::getStation(size_t id) const {
|
209
|
237
|
}
|
210
|
238
|
|
211
|
239
|
// _____________________________________________________________________________
|
|
240
|
+void StatIdx::initSuggestions() {
|
|
241
|
+ for (size_t i = 0; i < _stations.size(); i++) {
|
|
242
|
+ auto& stat = _stations[i];
|
|
243
|
+ if (stat.group != stat.origGroup || getGroup(stat.group)->osmid == 1) {
|
|
244
|
+ Suggestion sug;
|
|
245
|
+ sug.station = i;
|
|
246
|
+ sug.arrow = util::geo::DLine{stat.pos, stat.pos};
|
|
247
|
+ if (getGroup(stat.origGroup)->osmid < 2) {
|
|
248
|
+ if (getGroup(stat.group)->osmid == 1) {
|
|
249
|
+ // move orphan into new group
|
|
250
|
+ sug.type = 1;
|
|
251
|
+ sug.target_gid = stat.group;
|
|
252
|
+
|
|
253
|
+ // sug.arrow = getGroupArrow(i, stat.group);
|
|
254
|
+
|
|
255
|
+ _suggestions.push_back(sug);
|
|
256
|
+ stat.suggestions.push_back(_suggestions.size() - 1);
|
|
257
|
+
|
|
258
|
+ } else if (getGroup(stat.group)->osmid > 1) {
|
|
259
|
+ // move orphan into existing group
|
|
260
|
+ sug.type = 2;
|
|
261
|
+ sug.target_gid = stat.group;
|
|
262
|
+ sug.target_osm_rel_id = getGroup(stat.group)->osmid;
|
|
263
|
+
|
|
264
|
+ sug.arrow = getGroupArrow(i, stat.group);
|
|
265
|
+
|
|
266
|
+ _suggestions.push_back(sug);
|
|
267
|
+ stat.suggestions.push_back(_suggestions.size() - 1);
|
|
268
|
+ }
|
|
269
|
+ } else {
|
|
270
|
+ if (getGroup(stat.group)->osmid == 1) {
|
|
271
|
+ // move station from relation into new group
|
|
272
|
+ sug.type = 3;
|
|
273
|
+ sug.orig_gid = stat.origGroup;
|
|
274
|
+ sug.orig_osm_rel_id = getGroup(stat.origGroup)->osmid;
|
|
275
|
+ sug.target_gid = stat.group;
|
|
276
|
+
|
|
277
|
+ sug.arrow = getGroupArrow(i, stat.group);
|
|
278
|
+
|
|
279
|
+ _suggestions.push_back(sug);
|
|
280
|
+ stat.suggestions.push_back(_suggestions.size() - 1);
|
|
281
|
+ } else if (getGroup(stat.group)->osmid > 1) {
|
|
282
|
+ // move station from relation into existing group
|
|
283
|
+ sug.type = 4;
|
|
284
|
+ sug.orig_gid = stat.origGroup;
|
|
285
|
+ sug.orig_osm_rel_id = getGroup(stat.origGroup)->osmid;
|
|
286
|
+ sug.target_gid = stat.group;
|
|
287
|
+ sug.target_osm_rel_id = getGroup(stat.group)->osmid;
|
|
288
|
+
|
|
289
|
+ _suggestions.push_back(sug);
|
|
290
|
+ stat.suggestions.push_back(_suggestions.size() - 1);
|
|
291
|
+ } else {
|
|
292
|
+ // move station out of relation
|
|
293
|
+ sug.type = 5;
|
|
294
|
+ sug.orig_gid = stat.origGroup;
|
|
295
|
+ sug.target_gid = stat.group;
|
|
296
|
+ sug.target_osm_rel_id = getGroup(stat.group)->osmid;
|
|
297
|
+
|
|
298
|
+ _suggestions.push_back(sug);
|
|
299
|
+ stat.suggestions.push_back(_suggestions.size() - 1);
|
|
300
|
+ }
|
|
301
|
+ }
|
|
302
|
+ }
|
|
303
|
+ }
|
|
304
|
+}
|
|
305
|
+
|
|
306
|
+// _____________________________________________________________________________
|
|
307
|
+util::geo::DLine StatIdx::getGroupArrow(size_t stat, size_t group) const {
|
|
308
|
+ auto a = getStation(stat)->pos;
|
|
309
|
+ auto b = util::geo::centroid(getGroup(group)->poly);
|
|
310
|
+
|
|
311
|
+ auto pl = util::geo::PolyLine<double>(a, b);
|
|
312
|
+ auto bb = pl.getPointAtDist(fmax(pl.getLength() / 2, pl.getLength() - 20)).p;
|
|
313
|
+
|
|
314
|
+ int side = rand() % 2;
|
|
315
|
+ if (!side) side = -1;
|
|
316
|
+
|
|
317
|
+
|
|
318
|
+ auto rot1 = util::geo::rotate(util::geo::DLine{a, bb}, 45 * side, a);
|
|
319
|
+ auto rot2 = util::geo::rotate(util::geo::DLine{a, bb}, -20 * side, bb);
|
|
320
|
+
|
|
321
|
+ auto rot3 = util::geo::rotate(util::geo::DLine{a, bb}, -120 * side, bb);
|
|
322
|
+
|
|
323
|
+ auto i =
|
|
324
|
+ util::geo::intersection(util::geo::LineSegment<double>{rot1[0], rot1[1]},
|
|
325
|
+ util::geo::LineSegment<double>{rot2[0], rot2[1]});
|
|
326
|
+
|
|
327
|
+ auto line = util::geo::BezierCurve<double>(a, i, rot3[0], bb).render(1).getLine();
|
|
328
|
+
|
|
329
|
+ // arrowhead
|
|
330
|
+ auto pl2 = util::geo::PolyLine<double>(line);
|
|
331
|
+ util::geo::LineSegment<double> headSeg(pl2.getPointAtDist(pl2.getLength() - 5).p, line.back());
|
|
332
|
+ auto arrHeadLeft = util::geo::rotate(headSeg, -45, line.back()).first;
|
|
333
|
+ auto arrHeadRight = util::geo::rotate(headSeg, 45, line.back()).first;
|
|
334
|
+
|
|
335
|
+ auto tmp = line.back();
|
|
336
|
+
|
|
337
|
+ line.push_back(arrHeadLeft);
|
|
338
|
+ line.push_back(tmp);
|
|
339
|
+ line.push_back(arrHeadRight);
|
|
340
|
+
|
|
341
|
+ return line;
|
|
342
|
+}
|
|
343
|
+
|
|
344
|
+// _____________________________________________________________________________
|
212
|
345
|
const Group* StatIdx::getGroup(size_t id) const {
|
213
|
346
|
if (id >= _groups.size()) return 0;
|
214
|
347
|
return &_groups[id];
|
215
|
348
|
}
|
|
349
|
+
|
|
350
|
+// _____________________________________________________________________________
|
|
351
|
+const Suggestion* StatIdx::getSuggestion(size_t id) const {
|
|
352
|
+ if (id >= _suggestions.size()) return 0;
|
|
353
|
+ return &_suggestions[id];
|
|
354
|
+}
|