Repository navigation
Expand file tree
/
Copy pathosmmodel.cpp
More file actions
1019 lines (889 loc) · 37 KB
/
Copy pathosmmodel.cpp
File metadata and controls
1019 lines (889 loc) · 37 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
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "osmmodel.h"
#include "osmstyle.h"
#include "osmquadtree.h"
#include "osmevents.h"
#include "geocoordhash.h"
#include <QHash>
#include <QThreadPool>
#include <QRunnable>
#include <QElapsedTimer>
#include <QtConcurrent>
#include <QThread>
#include <QDebug>
#include <atomic>
OsmModel::OsmModel()
{
}
void OsmModel::clear()
{
QWriteLocker locker(&m_lock);
m_nodes.clear();
m_tags.clear();
m_nds.clear();
m_ways.clear();
m_relations.clear();
m_members.clear();
m_keyMap.clear();
m_keyIndexToName.clear();
m_nodeIdToIndex.clear();
m_wayIdToIndex.clear();
m_relIdToIndex.clear();
m_coordToNodeId.clear();
m_maxNodeId = 0;
m_fillDrawList.clear();
m_lineDrawList.clear();
m_pointDrawList.clear();
m_info = OsmBounds();
m_quadTree.reset();
}
int OsmModel::addNode(const OsmNode &node)
{
auto it = m_nodeIdToIndex.constFind(node.m_id);
if (it != m_nodeIdToIndex.constEnd()) {
int existingIdx = it.value();
const OsmNode &existing = m_nodes[existingIdx];
if (qAbs(existing.m_lat - node.m_lat) > 1e-7 ||
qAbs(existing.m_lon - node.m_lon) > 1e-7) {
qWarning() << "Duplicate node ID with different coords:" << node.m_id
<< "existing:" << existing.m_lat << existing.m_lon
<< "new:" << node.m_lat << node.m_lon;
}
return existingIdx;
}
int idx = m_nodes.size();
m_nodes.append(node);
m_nodeIdToIndex.insert(node.m_id, idx);
if (node.m_lat < m_info.latMin) m_info.latMin = node.m_lat;
if (node.m_lat > m_info.latMax) m_info.latMax = node.m_lat;
if (node.m_lon < m_info.lonMin) m_info.lonMin = node.m_lon;
if (node.m_lon > m_info.lonMax) m_info.lonMax = node.m_lon;
return idx;
}
int OsmModel::addWay(const OsmWay &way)
{
int idx = m_ways.size();
m_ways.append(way);
m_wayIdToIndex.insert(way.m_id, idx);
return idx;
}
int OsmModel::addRelation(const OsmRelation &rel)
{
int idx = m_relations.size();
m_relations.append(rel);
m_relIdToIndex.insert(rel.m_id, idx);
return idx;
}
int OsmModel::addTag(int keyIndex, const QString &value, int nextIndex)
{
m_tags.append(OsmTag(keyIndex, value, nextIndex));
return m_tags.size() - 1;
}
int OsmModel::addNd(qint64 ref, int nextIndex)
{
m_nds.append(OsmNd(ref, nextIndex));
return m_nds.size() - 1;
}
int OsmModel::addMember(OsmRelationMember::MemberType type, qint64 ref, const QString &role, int nextIndex)
{
m_members.append(OsmRelationMember(type, ref, role, nextIndex));
return m_members.size() - 1;
}
int OsmModel::findNodeIndex(qint64 id) const
{
auto it = m_nodeIdToIndex.constFind(id);
if (it != m_nodeIdToIndex.constEnd()) {
return it.value();
}
for (int i = 0; i < m_nodes.size(); ++i) {
if (m_nodes[i].m_id == id) {
m_nodeIdToIndex.insert(id, i);
return i;
}
}
return -1;
}
int OsmModel::findWayIndex(qint64 id) const
{
auto it = m_wayIdToIndex.constFind(id);
if (it != m_wayIdToIndex.constEnd()) {
return it.value();
}
for (int i = 0; i < m_ways.size(); ++i) {
if (m_ways[i].m_id == id) {
m_wayIdToIndex.insert(id, i);
return i;
}
}
return -1;
}
int OsmModel::registerKey(const QString &key)
{
auto it = m_keyMap.find(key);
if (it != m_keyMap.end()) {
return it.value();
}
int id = m_keyMap.size() + 1;
m_keyMap.insert(key, id);
m_keyIndexToName.insert(id, key);
return id;
}
QString OsmModel::keyName(int keyIndex) const
{
return m_keyIndexToName.value(keyIndex);
}
QString OsmModel::getTagValue(const OsmWay &way, const QString &key) const
{
int keyId = m_keyMap.value(key, -1);
if (keyId < 0) return QString();
int t = way.m_tagHead;
while (t >= 0 && t < m_tags.size()) {
if (m_tags[t].keyIndex == keyId) {
return m_tags[t].value;
}
t = m_tags[t].nextIndex;
}
return QString();
}
QString OsmModel::getTagValue(const OsmNode &node, const QString &key) const
{
int keyId = m_keyMap.value(key, -1);
if (keyId < 0) return QString();
int t = node.m_tagHead;
while (t >= 0 && t < m_tags.size()) {
if (m_tags[t].keyIndex == keyId) {
return m_tags[t].value;
}
t = m_tags[t].nextIndex;
}
return QString();
}
class BBoxComputeTask : public QRunnable {
public:
OsmModel *model;
int startWay;
int endWay;
int layerKey;
BBoxComputeTask(OsmModel *m, int s, int e, int lKey)
: model(m), startWay(s), endWay(e), layerKey(lKey) {
setAutoDelete(true);
}
void run() override {
QVector<OsmWay> &ways = model->ways();
const QHash<qint64, int> &nodeMap = model->nodeIdToIndex();
const QVector<OsmNd> &nds = model->nds();
const QVector<OsmNode> &nodes = model->nodes();
const QVector<OsmTag> &tags = model->tags();
for (int wi = startWay; wi < endWay; ++wi) {
OsmWay &way = ways[wi];
double minLat = 1e30, maxLat = -1e30;
double minLon = 1e30, maxLon = -1e30;
qint64 firstRef = -1, lastRef = -1;
int count = 0;
QVector<int> indices;
indices.reserve(16);
int t = way.ndHead();
while (t >= 0 && t < nds.size()) {
qint64 ref = nds[t].ref;
if (count == 0) firstRef = ref;
lastRef = ref;
auto it = nodeMap.constFind(ref);
if (it != nodeMap.constEnd()) {
int nodeIdx = it.value();
indices.append(nodeIdx);
const OsmNode &node = nodes[nodeIdx];
if (node.lat() < minLat) minLat = node.lat();
if (node.lat() > maxLat) maxLat = node.lat();
if (node.lon() < minLon) minLon = node.lon();
if (node.lon() > maxLon) maxLon = node.lon();
count++;
}
t = nds[t].nextIndex;
}
indices.squeeze();
way.nodeIndices() = indices;
way.m_isClosed = (count >= 4 && firstRef == lastRef);
if (minLat < 1e30) {
way.m_bbox = QRectF(minLon, minLat, maxLon - minLon, maxLat - minLat);
} else {
way.m_bbox = QRectF();
}
way.m_layer = 0;
if (layerKey >= 0) {
int tt = way.m_tagHead;
while (tt >= 0 && tt < tags.size()) {
if (tags[tt].keyIndex == layerKey) {
bool ok;
int l = tags[tt].value.toInt(&ok);
if (ok) way.m_layer = l;
break;
}
tt = tags[tt].nextIndex;
}
}
}
}
};
void OsmModel::computeWayBBoxes()
{
QElapsedTimer timer;
timer.start();
int numWays = m_ways.size();
if (numWays == 0) return;
int layerKey = m_keyMap.value(QStringLiteral("layer"), -1);
int idealThreads = QThread::idealThreadCount();
int threadCount = qMax(1, qMin(idealThreads, 8));
int waysPerThread = (numWays + threadCount - 1) / threadCount;
QThreadPool pool;
pool.setMaxThreadCount(threadCount);
qDebug() << "Computing bbox using" << threadCount << "threads for" << numWays << "ways";
for (int i = 0; i < threadCount; ++i) {
int s = i * waysPerThread;
int e = qMin(s + waysPerThread, numWays);
if (s >= numWays) break;
pool.start(new BBoxComputeTask(this, s, e, layerKey));
}
pool.waitForDone();
qDebug() << "BBox computation done in" << timer.elapsed() << "ms";
}
void OsmModel::finalizeAfterLoad()
{
m_nodes.squeeze();
m_tags.squeeze();
m_nds.squeeze();
m_ways.squeeze();
m_nodeIdToIndex.squeeze();
m_wayIdToIndex.squeeze();
computeWayBBoxes();
m_fillDrawList.clear();
m_lineDrawList.clear();
m_pointDrawList.clear();
m_fillDrawList.reserve(m_ways.size() / 2);
m_lineDrawList.reserve(m_ways.size());
m_pointDrawList.reserve(m_nodes.size() / 10); // POI nodes account for ~10% of total nodes
QHash<QString, int> keysNeeded;
keysNeeded[QStringLiteral("highway")] = registerKey(QStringLiteral("highway"));
keysNeeded[QStringLiteral("railway")] = registerKey(QStringLiteral("railway"));
keysNeeded[QStringLiteral("waterway")] = registerKey(QStringLiteral("waterway"));
keysNeeded[QStringLiteral("building")] = registerKey(QStringLiteral("building"));
keysNeeded[QStringLiteral("landuse")] = registerKey(QStringLiteral("landuse"));
keysNeeded[QStringLiteral("natural")] = registerKey(QStringLiteral("natural"));
keysNeeded[QStringLiteral("leisure")] = registerKey(QStringLiteral("leisure"));
keysNeeded[QStringLiteral("boundary")] = registerKey(QStringLiteral("boundary"));
keysNeeded[QStringLiteral("barrier")] = registerKey(QStringLiteral("barrier"));
keysNeeded[QStringLiteral("aeroway")] = registerKey(QStringLiteral("aeroway"));
keysNeeded[QStringLiteral("power")] = registerKey(QStringLiteral("power"));
keysNeeded[QStringLiteral("man_made")] = registerKey(QStringLiteral("man_made"));
keysNeeded[QStringLiteral("historic")] = registerKey(QStringLiteral("historic"));
keysNeeded[QStringLiteral("tourism")] = registerKey(QStringLiteral("tourism"));
keysNeeded[QStringLiteral("amenity")] = registerKey(QStringLiteral("amenity"));
keysNeeded[QStringLiteral("shop")] = registerKey(QStringLiteral("shop"));
keysNeeded[QStringLiteral("sport")] = registerKey(QStringLiteral("sport"));
keysNeeded[QStringLiteral("military")] = registerKey(QStringLiteral("military"));
keysNeeded[QStringLiteral("place")] = registerKey(QStringLiteral("place"));
keysNeeded[QStringLiteral("piste:type")] = registerKey(QStringLiteral("piste:type"));
keysNeeded[QStringLiteral("route")] = registerKey(QStringLiteral("route"));
keysNeeded[QStringLiteral("aerialway")] = registerKey(QStringLiteral("aerialway"));
keysNeeded[QStringLiteral("layer")] = registerKey(QStringLiteral("layer"));
int highwayKey = keysNeeded[QStringLiteral("highway")];
int railwayKey = keysNeeded[QStringLiteral("railway")];
int waterwayKey = keysNeeded[QStringLiteral("waterway")];
int buildingKey = keysNeeded[QStringLiteral("building")];
int landuseKey = keysNeeded[QStringLiteral("landuse")];
int naturalKey = keysNeeded[QStringLiteral("natural")];
int leisureKey = keysNeeded[QStringLiteral("leisure")];
int boundaryKey = keysNeeded[QStringLiteral("boundary")];
int barrierKey = keysNeeded[QStringLiteral("barrier")];
int aerowayKey = keysNeeded[QStringLiteral("aeroway")];
int powerKey = keysNeeded[QStringLiteral("power")];
int manMadeKey = keysNeeded[QStringLiteral("man_made")];
int historicKey = keysNeeded[QStringLiteral("historic")];
int tourismKey = keysNeeded[QStringLiteral("tourism")];
int amenityKey = keysNeeded[QStringLiteral("amenity")];
int shopKey = keysNeeded[QStringLiteral("shop")];
int sportKey = keysNeeded[QStringLiteral("sport")];
int militaryKey = keysNeeded[QStringLiteral("military")];
int placeKey = keysNeeded[QStringLiteral("place")];
int pisteKey = keysNeeded[QStringLiteral("piste:type")];
int routeKey = keysNeeded[QStringLiteral("route")];
int aerialwayKey = keysNeeded[QStringLiteral("aerialway")];
int layerKey = keysNeeded[QStringLiteral("layer")];
for (int wi = 0; wi < m_ways.size(); ++wi) {
OsmWay &way = m_ways[wi];
int tagType = TagEnum::UNKNOWN;
bool isArea = false;
int t = way.m_tagHead;
while (t >= 0 && t < m_tags.size()) {
const OsmTag &tag = m_tags[t];
int k = tag.keyIndex;
const QString &v = tag.value;
if (k == highwayKey) {
int ht = OsmStyle::highwayTagType(v);
if (ht != TagEnum::UNKNOWN) tagType = ht;
} else if (k == railwayKey) {
int rt = OsmStyle::railwayTagType(v);
if (rt != TagEnum::UNKNOWN) tagType = rt;
} else if (k == waterwayKey && v != QStringLiteral("")) {
tagType = OsmStyle::waterwayTagType(v);
isArea = way.isClosed() && OsmStyle::isWaterAreaTagType(tagType);
} else if (k == buildingKey) {
tagType = OsmStyle::buildingTagType(v);
isArea = way.isClosed();
} else if (k == landuseKey) {
tagType = OsmStyle::landuseTagType(v);
isArea = way.isClosed();
} else if (k == naturalKey) {
tagType = OsmStyle::naturalTagType(v);
isArea = way.isClosed();
} else if (k == leisureKey) {
tagType = OsmStyle::leisureTagType(v);
isArea = way.isClosed();
} else if (k == boundaryKey) {
tagType = OsmStyle::boundaryTagType(v);
} else if (k == barrierKey) {
int bt = OsmStyle::barrierTagType(v);
if (bt != TagEnum::UNKNOWN) tagType = bt;
} else if (k == aerowayKey) {
int at = OsmStyle::aerowayTagType(v);
if (at != TagEnum::UNKNOWN) {
tagType = at;
isArea = way.isClosed() && OsmStyle::isArea(at);
}
} else if (k == powerKey) {
int pt = OsmStyle::powerTagType(v);
if (pt != TagEnum::UNKNOWN) {
tagType = pt;
isArea = way.isClosed() && OsmStyle::isArea(pt);
}
} else if (k == manMadeKey) {
int mt = OsmStyle::manMadeTagType(v);
if (mt != TagEnum::UNKNOWN) tagType = mt;
} else if (k == historicKey) {
int ht = OsmStyle::historicTagType(v);
if (ht != TagEnum::UNKNOWN) {
tagType = ht;
isArea = way.isClosed() && OsmStyle::isArea(ht);
}
} else if (k == tourismKey) {
int tt = OsmStyle::tourismTagType(v);
if (tt != TagEnum::UNKNOWN) {
tagType = tt;
isArea = way.isClosed() && OsmStyle::isArea(tt);
}
} else if (k == amenityKey) {
int at = OsmStyle::amenityTagType(v);
if (at != TagEnum::UNKNOWN) {
tagType = at;
isArea = way.isClosed() && OsmStyle::isArea(at);
}
} else if (k == shopKey) {
int st = OsmStyle::shopTagType(v);
if (st != TagEnum::UNKNOWN) tagType = st;
} else if (k == sportKey) {
int st = OsmStyle::sportTagType(v);
if (st != TagEnum::UNKNOWN) {
tagType = st;
isArea = way.isClosed();
}
} else if (k == militaryKey) {
int mt = OsmStyle::militaryTagType(v);
if (mt != TagEnum::UNKNOWN) {
tagType = mt;
isArea = way.isClosed() && OsmStyle::isArea(mt);
}
} else if (k == placeKey) {
int pt = OsmStyle::placeTagType(v);
if (pt != TagEnum::UNKNOWN) {
tagType = pt;
isArea = way.isClosed();
}
} else if (k == pisteKey) {
int pt = OsmStyle::pisteTagType(v);
if (pt != TagEnum::UNKNOWN) tagType = pt;
} else if (k == routeKey) {
int rt = OsmStyle::routeTagType(v);
if (rt != TagEnum::UNKNOWN) tagType = rt;
} else if (k == aerialwayKey) {
int at = OsmStyle::aerialwayTagType(v);
if (at != TagEnum::UNKNOWN) tagType = at;
} else if (k == layerKey) {
bool ok = false;
int layer = v.toInt(&ok);
if (ok) way.setLayer(layer);
}
t = tag.nextIndex;
}
if (tagType == TagEnum::UNKNOWN) continue;
if (OsmStyle::isArea(tagType) && !way.isClosed()) {
isArea = false;
}
OsmDrawEntry entry;
entry.tagType = tagType;
entry.wayIndex = wi;
entry.lodMin = OsmStyle::defaultLodMin(tagType);
entry.lodMax = OsmStyle::defaultLodMax(tagType);
entry.isArea = isArea;
entry.bbox = way.bbox();
if (isArea) {
m_fillDrawList.append(entry);
} else {
m_lineDrawList.append(entry);
}
}
m_fillDrawList.squeeze();
m_lineDrawList.squeeze();
// Process nodes (POI nodes)
for (int ni = 0; ni < m_nodes.size(); ++ni) {
const OsmNode &node = m_nodes[ni];
int tagType = TagEnum::UNKNOWN;
int t = node.m_tagHead;
while (t >= 0 && t < m_tags.size()) {
const OsmTag &tag = m_tags[t];
int k = tag.keyIndex;
const QString &v = tag.value;
// First priority: POI-specific keys -- break immediately once identified
if (k == amenityKey) {
int at = OsmStyle::amenityTagType(v);
if (at != TagEnum::UNKNOWN) { tagType = at; break; }
} else if (k == shopKey) {
int st = OsmStyle::shopTagType(v);
if (st != TagEnum::UNKNOWN) { tagType = st; break; }
} else if (k == tourismKey) {
int tt = OsmStyle::tourismTagType(v);
if (tt != TagEnum::UNKNOWN) { tagType = tt; break; }
} else if (k == historicKey) {
int ht = OsmStyle::historicTagType(v);
if (ht != TagEnum::UNKNOWN) { tagType = ht; break; }
} else if (k == leisureKey) {
int lt = OsmStyle::leisureTagType(v);
if (lt != TagEnum::UNKNOWN) { tagType = lt; break; }
} else if (k == sportKey) {
int st = OsmStyle::sportTagType(v);
if (st != TagEnum::UNKNOWN) { tagType = st; break; }
}
// Second priority: infrastructure/geographic keys -- only set when tagType is still UNKNOWN
else if (tagType == TagEnum::UNKNOWN) {
if (k == highwayKey) {
int ht = OsmStyle::highwayTagType(v);
if (ht != TagEnum::UNKNOWN) tagType = ht;
} else if (k == railwayKey) {
int rt = OsmStyle::railwayTagType(v);
if (rt != TagEnum::UNKNOWN) tagType = rt;
} else if (k == waterwayKey) {
int wt = OsmStyle::waterwayTagType(v);
if (wt != TagEnum::UNKNOWN) tagType = wt;
} else if (k == buildingKey) {
int bt = OsmStyle::buildingTagType(v);
if (bt != TagEnum::UNKNOWN) tagType = bt;
} else if (k == landuseKey) {
tagType = OsmStyle::landuseTagType(v);
} else if (k == naturalKey) {
int nt = OsmStyle::naturalTagType(v);
if (nt != TagEnum::UNKNOWN) tagType = nt;
} else if (k == boundaryKey) {
int bt = OsmStyle::boundaryTagType(v);
if (bt != TagEnum::UNKNOWN) tagType = bt;
} else if (k == barrierKey) {
int bt = OsmStyle::barrierTagType(v);
if (bt != TagEnum::UNKNOWN) tagType = bt;
} else if (k == aerowayKey) {
int at = OsmStyle::aerowayTagType(v);
if (at != TagEnum::UNKNOWN) tagType = at;
} else if (k == powerKey) {
int pt = OsmStyle::powerTagType(v);
if (pt != TagEnum::UNKNOWN) tagType = pt;
} else if (k == manMadeKey) {
int mt = OsmStyle::manMadeTagType(v);
if (mt != TagEnum::UNKNOWN) tagType = mt;
} else if (k == militaryKey) {
int mt = OsmStyle::militaryTagType(v);
if (mt != TagEnum::UNKNOWN) tagType = mt;
} else if (k == placeKey) {
int pt = OsmStyle::placeTagType(v);
if (pt != TagEnum::UNKNOWN) tagType = pt;
} else if (k == pisteKey) {
int pt = OsmStyle::pisteTagType(v);
if (pt != TagEnum::UNKNOWN) tagType = pt;
} else if (k == routeKey) {
int rt = OsmStyle::routeTagType(v);
if (rt != TagEnum::UNKNOWN) tagType = rt;
} else if (k == aerialwayKey) {
int at = OsmStyle::aerialwayTagType(v);
if (at != TagEnum::UNKNOWN) tagType = at;
}
}
t = tag.nextIndex;
}
if (tagType == TagEnum::UNKNOWN) continue;
// Create draw entry for POI node
OsmDrawEntry entry;
entry.tagType = tagType;
entry.wayIndex = -1; // Mark as node
entry.nodeIndex = ni;
entry.lodMin = OsmStyle::defaultLodMin(tagType);
entry.lodMax = OsmStyle::defaultLodMax(tagType);
entry.isArea = false;
entry.bbox = QRectF(node.lon() - 0.0001, node.lat() - 0.0001, 0.0002, 0.0002);
m_pointDrawList.append(entry);
}
m_pointDrawList.squeeze();
qDebug() << "finalizeAfterLoad: ways=" << m_ways.size() << "nodes=" << m_nodes.size()
<< "fillDrawList=" << m_fillDrawList.size() << "lineDrawList=" << m_lineDrawList.size()
<< "pointDrawList=" << m_pointDrawList.size()
<< "bounds=" << m_info.lonMin << m_info.latMin << m_info.lonMax << m_info.latMax;
if (!m_lineDrawList.isEmpty()) {
const auto &first = m_lineDrawList.first();
qDebug() << " first line entry: tagType=" << first.tagType << "wayIndex=" << first.wayIndex
<< "bbox=" << first.bbox;
}
buildSpatialIndex();
}
void OsmModel::mergeFrom(const OsmModel &other)
{
if (other.m_nodes.isEmpty() && other.m_ways.isEmpty()) return;
int otherNodeCount = other.m_nodes.size();
int otherWayCount = other.m_ways.size();
int otherTagCount = other.m_tags.size();
int otherNdCount = other.m_nds.size();
reserveTags(m_tags.size() + otherTagCount);
reserveNds(m_nds.size() + otherNdCount);
reserveNodes(m_nodes.size() + otherNodeCount);
reserveWays(m_ways.size() + otherWayCount);
qint64 maxNodeId = m_maxNodeId;
qint64 originalMaxNodeId = maxNodeId;
int tagBase = m_tags.size();
int ndBase = m_nds.size();
int nodeBase = m_nodes.size();
int wayBase = m_ways.size();
QVector<int> keyRemap(other.m_keyIndexToName.size());
for (int i = 0; i < other.m_keyIndexToName.size(); i++) {
const QString &keyName = other.m_keyIndexToName[i];
keyRemap[i] = registerKey(keyName);
}
m_tags.resize(tagBase + otherTagCount);
for (int i = 0; i < otherTagCount; i++) {
const OsmTag &srcTag = other.m_tags[i];
OsmTag &dstTag = m_tags[tagBase + i];
dstTag.keyIndex = keyRemap[srcTag.keyIndex];
dstTag.value = srcTag.value;
dstTag.nextIndex = (srcTag.nextIndex >= 0) ? (tagBase + srcTag.nextIndex) : -1;
}
m_nds.resize(ndBase + otherNdCount);
for (int i = 0; i < otherNdCount; i++) {
const OsmNd &srcNd = other.m_nds[i];
OsmNd &dstNd = m_nds[ndBase + i];
dstNd.ref = srcNd.ref;
dstNd.nextIndex = (srcNd.nextIndex >= 0) ? (ndBase + srcNd.nextIndex) : -1;
}
QVector<qint64> nodeIdMap(otherNodeCount);
GeoCoord::CoordMap<qint64> coordToNewId;
coordToNewId.reserve(otherNodeCount);
QHash<qint64, qint64> oldIdToNewId;
oldIdToNewId.reserve(otherNodeCount);
int newNodeCount = 0;
int mergedDuplicates = 0;
for (int i = 0; i < otherNodeCount; i++) {
const OsmNode &srcNode = other.m_nodes[i];
double lat = srcNode.lat();
double lon = srcNode.lon();
qint64 existingId;
if (m_coordToNodeId.value(lon, lat, &existingId)) {
nodeIdMap[i] = existingId;
oldIdToNewId.insert(srcNode.id(), existingId);
mergedDuplicates++;
} else {
qint64 newId = ++maxNodeId;
nodeIdMap[i] = newId;
oldIdToNewId.insert(srcNode.id(), newId);
coordToNewId.insert(lon, lat, newId);
newNodeCount++;
}
}
m_nodes.resize(nodeBase + newNodeCount);
int nodeWriteIdx = 0;
for (int i = 0; i < otherNodeCount; i++) {
const OsmNode &srcNode = other.m_nodes[i];
qint64 newId = nodeIdMap[i];
if (newId <= originalMaxNodeId) {
if (srcNode.lat() < m_info.latMin) m_info.latMin = srcNode.lat();
if (srcNode.lat() > m_info.latMax) m_info.latMax = srcNode.lat();
if (srcNode.lon() < m_info.lonMin) m_info.lonMin = srcNode.lon();
if (srcNode.lon() > m_info.lonMax) m_info.lonMax = srcNode.lon();
continue;
}
int dstIdx = nodeBase + nodeWriteIdx;
OsmNode &dstNode = m_nodes[dstIdx];
dstNode.setId(newId);
dstNode.setLat(srcNode.lat());
dstNode.setLon(srcNode.lon());
int tagHead = srcNode.tagHead();
if (tagHead >= 0) tagHead += tagBase;
dstNode.setTagHead(tagHead);
m_nodeIdToIndex.insert(newId, dstIdx);
m_coordToNodeId.insert(srcNode.lon(), srcNode.lat(), newId);
if (srcNode.lat() < m_info.latMin) m_info.latMin = srcNode.lat();
if (srcNode.lat() > m_info.latMax) m_info.latMax = srcNode.lat();
if (srcNode.lon() < m_info.lonMin) m_info.lonMin = srcNode.lon();
if (srcNode.lon() > m_info.lonMax) m_info.lonMax = srcNode.lon();
nodeWriteIdx++;
}
for (int i = 0; i < otherNdCount; i++) {
qint64 oldRef = m_nds[ndBase + i].ref;
auto it = oldIdToNewId.constFind(oldRef);
if (it != oldIdToNewId.constEnd()) {
m_nds[ndBase + i].ref = it.value();
}
}
m_ways.resize(wayBase + otherWayCount);
for (int i = 0; i < otherWayCount; i++) {
const OsmWay &srcWay = other.m_ways[i];
OsmWay &dstWay = m_ways[wayBase + i];
dstWay.setId(wayBase + i);
int ndHead = srcWay.ndHead();
if (ndHead >= 0) ndHead += ndBase;
dstWay.setNdHead(ndHead);
int tagHead = srcWay.tagHead();
if (tagHead >= 0) tagHead += tagBase;
dstWay.setTagHead(tagHead);
dstWay.setLayer(srcWay.layer());
m_wayIdToIndex.insert(dstWay.id(), wayBase + i);
}
m_maxNodeId = maxNodeId;
const OsmBounds &otherBounds = other.bounds();
if (otherBounds.isValid()) {
if (otherBounds.lonMin < m_info.lonMin) m_info.lonMin = otherBounds.lonMin;
if (otherBounds.lonMax > m_info.lonMax) m_info.lonMax = otherBounds.lonMax;
if (otherBounds.latMin < m_info.latMin) m_info.latMin = otherBounds.latMin;
if (otherBounds.latMax > m_info.latMax) m_info.latMax = otherBounds.latMax;
}
qDebug() << "mergeFrom: added" << newNodeCount << "nodes (skipped" << mergedDuplicates << "duplicates),"
<< otherWayCount << "ways, total nodes=" << m_nodes.size() << "ways=" << m_ways.size();
}
void OsmModel::batchMergeFrom(const QVector<const OsmModel*> &models)
{
if (models.isEmpty()) return;
// Step 1: Pre-calculate total capacity
int totalNodeCount = 0, totalWayCount = 0, totalTagCount = 0, totalNdCount = 0;
for (const OsmModel *model : models) {
totalNodeCount += model->m_nodes.size();
totalWayCount += model->m_ways.size();
totalTagCount += model->m_tags.size();
totalNdCount += model->m_nds.size();
}
reserveNodes(m_nodes.size() + totalNodeCount);
reserveWays(m_ways.size() + totalWayCount);
reserveTags(m_tags.size() + totalTagCount);
reserveNds(m_nds.size() + totalNdCount);
qint64 maxNodeId = m_maxNodeId;
qint64 originalMaxNodeId = maxNodeId;
int tagBase = m_tags.size();
int ndBase = m_nds.size();
int nodeBase = m_nodes.size();
int wayBase = m_ways.size();
// Step 2: Pre-register all keys (batch processing)
QHash<QString, int> keyMap;
for (const OsmModel *model : models) {
for (const QString &keyName : model->m_keyIndexToName) {
if (!keyMap.contains(keyName)) {
keyMap.insert(keyName, registerKey(keyName));
}
}
}
// Step 3: Batch copy tags (all models processed at once)
m_tags.resize(tagBase + totalTagCount);
int tagOffset = 0;
for (const OsmModel *model : models) {
int modelTagCount = model->m_tags.size();
for (int i = 0; i < modelTagCount; i++) {
const OsmTag &srcTag = model->m_tags[i];
OsmTag &dstTag = m_tags[tagBase + tagOffset + i];
dstTag.keyIndex = keyMap.value(model->m_keyIndexToName[srcTag.keyIndex]);
dstTag.value = srcTag.value;
dstTag.nextIndex = (srcTag.nextIndex >= 0) ? (tagBase + tagOffset + srcTag.nextIndex) : -1;
}
tagOffset += modelTagCount;
}
// Step 4: Batch copy nds (refs not corrected yet)
m_nds.resize(ndBase + totalNdCount);
int ndOffset = 0;
for (const OsmModel *model : models) {
int modelNdCount = model->m_nds.size();
for (int i = 0; i < modelNdCount; i++) {
const OsmNd &srcNd = model->m_nds[i];
OsmNd &dstNd = m_nds[ndBase + ndOffset + i];
dstNd.ref = srcNd.ref;
dstNd.nextIndex = (srcNd.nextIndex >= 0) ? (ndBase + ndOffset + srcNd.nextIndex) : -1;
}
ndOffset += modelNdCount;
}
// Step 5: Node deduplication and ID mapping (use hash table for direct dedup, avoiding sort)
// Pre-build model-level node ID mappings: (modelIdx, nodeIdx) -> newId
QVector<QVector<qint64>> modelNodeMaps(models.size());
// Also build old ID to new ID direct mapping
QVector<QHash<qint64, qint64>> modelOldIdToNewId(models.size());
for (int modelIdx = 0; modelIdx < models.size(); modelIdx++) {
modelNodeMaps[modelIdx].resize(models[modelIdx]->m_nodes.size());
modelOldIdToNewId[modelIdx].reserve(models[modelIdx]->m_nodes.size());
}
// Use temporary hash table to store coordinate -> new ID, avoid repeated allocation
GeoCoord::CoordMap<qint64> coordToNewId;
coordToNewId.reserve(totalNodeCount);
// First pass: count new nodes, assign IDs
int newNodeCount = 0;
int mergedDuplicates = 0;
for (int modelIdx = 0; modelIdx < models.size(); modelIdx++) {
const OsmModel *model = models[modelIdx];
const auto &nodes = model->m_nodes;
for (int i = 0; i < nodes.size(); i++) {
const OsmNode &srcNode = nodes[i];
double lat = srcNode.lat();
double lon = srcNode.lon();
qint64 existingId;
if (m_coordToNodeId.value(lon, lat, &existingId)) {
// Already exists in the main model
modelNodeMaps[modelIdx][i] = existingId;
modelOldIdToNewId[modelIdx].insert(srcNode.id(), existingId);
mergedDuplicates++;
} else if (coordToNewId.value(lon, lat, &existingId)) {
// Already exists in other models
modelNodeMaps[modelIdx][i] = existingId;
modelOldIdToNewId[modelIdx].insert(srcNode.id(), existingId);
mergedDuplicates++;
} else {
// New node, assign ID
qint64 newId = ++maxNodeId;
modelNodeMaps[modelIdx][i] = newId;
modelOldIdToNewId[modelIdx].insert(srcNode.id(), newId);
coordToNewId.insert(lon, lat, newId);
newNodeCount++;
}
}
}
// Second pass: create new nodes
m_nodes.resize(nodeBase + newNodeCount);
int nodeWriteIdx = 0;
for (int modelIdx = 0; modelIdx < models.size(); modelIdx++) {
const OsmModel *model = models[modelIdx];
const auto &nodes = model->m_nodes;
for (int i = 0; i < nodes.size(); i++) {
const OsmNode &srcNode = nodes[i];
qint64 newId = modelNodeMaps[modelIdx][i];
// Check if this is a new node (greater than originalMaxNodeId)
if (newId <= originalMaxNodeId) {
// This is an existing node, no need to create
// But update bounds if not yet updated
if (srcNode.lat() < m_info.latMin) m_info.latMin = srcNode.lat();
if (srcNode.lat() > m_info.latMax) m_info.latMax = srcNode.lat();
if (srcNode.lon() < m_info.lonMin) m_info.lonMin = srcNode.lon();
if (srcNode.lon() > m_info.lonMax) m_info.lonMax = srcNode.lon();
continue;
}
int dstIdx = nodeBase + nodeWriteIdx;
OsmNode &dstNode = m_nodes[dstIdx];
dstNode.setId(newId);
dstNode.setLat(srcNode.lat());
dstNode.setLon(srcNode.lon());
int tagHead = srcNode.tagHead();
if (tagHead >= 0) {
int modelTagOffset = 0;
for (int j = 0; j < modelIdx; j++) {
modelTagOffset += models[j]->m_tags.size();
}
tagHead += tagBase + modelTagOffset;
}
dstNode.setTagHead(tagHead);
m_nodeIdToIndex.insert(newId, dstIdx);
m_coordToNodeId.insert(srcNode.lon(), srcNode.lat(), newId);
if (srcNode.lat() < m_info.latMin) m_info.latMin = srcNode.lat();
if (srcNode.lat() > m_info.latMax) m_info.latMax = srcNode.lat();
if (srcNode.lon() < m_info.lonMin) m_info.lonMin = srcNode.lon();
if (srcNode.lon() > m_info.lonMax) m_info.lonMax = srcNode.lon();
nodeWriteIdx++;
}
}
// Step 7: Correct nd.ref
ndOffset = 0;
for (int modelIdx = 0; modelIdx < models.size(); modelIdx++) {
const OsmModel *model = models[modelIdx];
int modelNdCount = model->m_nds.size();
const auto &oldIdToNewId = modelOldIdToNewId[modelIdx];
for (int i = 0; i < modelNdCount; i++) {
qint64 oldRef = m_nds[ndBase + ndOffset + i].ref;
auto it = oldIdToNewId.constFind(oldRef);
if (it != oldIdToNewId.constEnd()) {
m_nds[ndBase + ndOffset + i].ref = it.value();
}
}
ndOffset += modelNdCount;
}
// Step 8: Batch copy ways
m_ways.resize(wayBase + totalWayCount);
int wayOffset = 0;
tagOffset = 0;
ndOffset = 0;
for (int modelIdx = 0; modelIdx < models.size(); modelIdx++) {
const OsmModel *model = models[modelIdx];
int modelWayCount = model->m_ways.size();
for (int i = 0; i < modelWayCount; i++) {
const OsmWay &srcWay = model->m_ways[i];
OsmWay &dstWay = m_ways[wayBase + wayOffset + i];
dstWay.setId(wayBase + wayOffset + i);
int ndHead = srcWay.ndHead();
if (ndHead >= 0) ndHead += ndBase + ndOffset;
dstWay.setNdHead(ndHead);
int tagHead = srcWay.tagHead();
if (tagHead >= 0) tagHead += tagBase + tagOffset;
dstWay.setTagHead(tagHead);
dstWay.setLayer(srcWay.layer());
m_wayIdToIndex.insert(dstWay.id(), wayBase + wayOffset + i);
}
wayOffset += modelWayCount;
tagOffset += model->m_tags.size();
ndOffset += model->m_nds.size();
}
m_maxNodeId = maxNodeId;
// Update bounds
for (const OsmModel *model : models) {
const OsmBounds &b = model->bounds();
if (b.isValid()) {
if (b.lonMin < m_info.lonMin) m_info.lonMin = b.lonMin;
if (b.lonMax > m_info.lonMax) m_info.lonMax = b.lonMax;
if (b.latMin < m_info.latMin) m_info.latMin = b.latMin;
if (b.latMax > m_info.latMax) m_info.latMax = b.latMax;
}
}
qDebug() << "batchMergeFrom: merged" << models.size() << "models,"
<< "added" << newNodeCount << "nodes (skipped" << mergedDuplicates << "duplicates),"
<< totalWayCount << "ways, total nodes=" << m_nodes.size() << "ways=" << m_ways.size();
}
void OsmModel::buildSpatialIndex()
{
QWriteLocker locker(&m_lock);
if (!m_info.isValid()) return;
m_quadTree = std::make_unique<OsmQuadTree>(m_info.toRectF(), 8, 16);
for (int i = 0; i < m_ways.size(); ++i) {
const OsmWay &way = m_ways[i];
if (way.bbox().isValid()) {
m_quadTree->insert(i, way.bbox());
}
}
}
QList<int> OsmModel::queryWaysInRect(const QRectF &rect) const
{