-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathosm.cpp
More file actions
1490 lines (1358 loc) · 38.9 KB
/
osm.cpp
File metadata and controls
1490 lines (1358 loc) · 38.9 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
/*
Project: GAKLIB
Module: osm.cpp
Description: Open Street Map Importer
Author: Martin Gäckler
Address: Hofmannsthalweg 14, A-4030 Linz
Web: https://www.gaeckler.at/
Copyright: (c) 1988-2026 Martin Gäckler
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 3.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
THIS SOFTWARE IS PROVIDED BY Martin Gäckler, Linz, Austria ``AS IS''
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
*/
// --------------------------------------------------------------------- //
// ----- switches ------------------------------------------------------ //
// --------------------------------------------------------------------- //
// --------------------------------------------------------------------- //
// ----- includes ------------------------------------------------------ //
// --------------------------------------------------------------------- //
#include <gak/osm.h>
#include <gak/xmlParser.h>
#include <gak/types.h>
#include <gak/iostream.h>
#include <gak/stopWatch.h>
#include <gak/directory.h>
#include <gak/cmdlineParser.h>
// --------------------------------------------------------------------- //
// ----- imported datas ------------------------------------------------ //
// --------------------------------------------------------------------- //
// --------------------------------------------------------------------- //
// ----- module switches ----------------------------------------------- //
// --------------------------------------------------------------------- //
#ifdef __BORLANDC__
# pragma option -RT-
# pragma option -b
# pragma option -a4
# pragma option -pc
#endif
using namespace gak;
using namespace xml;
using namespace math;
// --------------------------------------------------------------------- //
// ----- constants ----------------------------------------------------- //
// --------------------------------------------------------------------- //
// element tags
static const STRING NODE = "node";
static const STRING WAY = "way";
static const STRING RELATION = "relation";
static const STRING ND = "nd";
static const STRING TAG = "tag";
static const STRING MEMBER = "member";
// attribute names
static const STRING ID = "id";
static const STRING K = "k";
static const STRING V = "v";
static const STRING TYPE = "type";
static const STRING ROLE = "role";
static const STRING REF = "ref";
// attribute values
static const STRING HIGHWAY = "highway";
static const STRING MOTORWAY = "motorway";
static const STRING MOTORWAY_LINK = "motorway_link";
static const STRING TRUNK = "trunk";
static const STRING TRUNK_LINK = "trunk_link";
static const STRING PRIMARY = "primary";
static const STRING PRIMARY_LINK = "primary_link";
static const STRING SECONDARY = "secondary";
static const STRING SECONDARY_LINK = "secondary_link";
static const STRING TERTIARY = "tertiary";
static const STRING TERTIARY_LINK = "tertiary_link";
static const STRING UNCLASSIFIED = "unclassified";
static const STRING RESIDENTIAL = "residential";
static const STRING SERVICE = "service";
static const STRING LIVING_STREET = "living_street";
static const STRING PEDESTRIAN = "pedestrian";
static const STRING TRACK = "track";
static const STRING BUS_GUIDEWAY = "bus_guideway";
static const STRING WT_ESCAPE = "escape";
static const STRING RACEWAY = "raceway";
static const STRING ROAD = "road";
static const STRING FOOTWAY = "footway";
static const STRING CYCLEWAY = "cycleway";
static const STRING BRIDLEWAY = "bridleway";
static const STRING STEPS = "steps";
static const STRING PATH = "path";
static const STRING WATERWAY = "waterway";
static const STRING RIVER = "river";
static const STRING RIVERBANK = "riverbank";
static const STRING CANAL = "canal";
static const STRING STREAM = "stream";
static const STRING NATURAL = "natural";
static const STRING WATER = "water";
static const STRING BAY = "bay";
static const STRING WETLAND = "wetland";
static const STRING WOOD = "wood";
static const STRING SCRUB = "scrub";
static const STRING GRASSLAND = "grassland";
static const STRING RAILWAY = "railway";
static const STRING RAIL = "rail";
static const STRING SUBWAY = "subway";
static const STRING TRAM = "tram";
static const STRING MAIN = "main";
static const STRING BRANCH = "branch";
static const STRING USAGE = "usage";
static const STRING LANDUSE = "landuse";
static const STRING FOREST = "forest";
static const STRING FARMLAND = "farmland";
static const STRING FARMYARD = "farmyard";
static const STRING MEADOW = "meadow";
static const STRING GRASS = "grass";
static const STRING ONEWAY = "oneway";
static const STRING YES = "yes";
static const STRING MINUS1 = "-1";
static const STRING OUTER = "outer";
static const STRING LAKE = "lake";
static const STRING PLACE = "place";
static const STRING NAME = "name";
static const STRING CONTINENT = "continent";
static const STRING COUNTRY = "country";
static const STRING STATE = "state";
static const STRING REGION = "region";
static const STRING DISTRICT = "district";
static const STRING COUNTY = "county";
static const STRING MUNICIPALITY = "municipality";
static const STRING CITY = "city";
static const STRING TOWN = "town";
static const STRING VILLAGE = "village";
static const STRING HAMLET = "hamlet";
static const STRING ALLOTMENTS = "allotments";
static const STRING ISOLATED_DWELING = "isolated_dweling";
static const STRING FARM = "farm";
static const STRING BOROUGH = "borough";
static const STRING QUARTER = "quarter";
static const STRING SUBURB = "suburb";
static const STRING NEIGHBOURHOOD = "neighbourhood";
static const STRING CITY_BLOCK = "city_block";
static const STRING PLOT = "plot";
static const STRING ISLAND = "island";
static const STRING ISLET = "islet";
static const STRING SQUARE = "square";
static const STRING LOCALITY = "locality";
static const char CHAR_TILES = 'T';
static const char CHAR_READ = 'R';
static const char CHAR_OSM_PATH = 'P';
static const char CHAR_MAP_FILE = 'M';
static const unsigned FLAG_TILES = 0x10;
static const unsigned FLAG_READ_MAP = 0x20;
static const unsigned OPT_OSM_PATH = 0x40;
static const unsigned OPT_MAP_FILE = 0x80;
const bool readMap = false;
static const gak::CommandLine::Options options[] =
{
{ CHAR_TILES, "buildTiles", 0, 1, FLAG_TILES, "save map data per tile" },
{ CHAR_READ, "readMap", 0, 1, FLAG_READ_MAP, "read existing map data" },
{ CHAR_OSM_PATH, "osmPath", 0, 1, OPT_OSM_PATH|CommandLine::needArg, "open street map path" },
{ CHAR_MAP_FILE, "mapFile", 0, 1, OPT_MAP_FILE|CommandLine::needArg, "binary map file" },
{ 0 },
};
// --------------------------------------------------------------------- //
// ----- macros -------------------------------------------------------- //
// --------------------------------------------------------------------- //
// --------------------------------------------------------------------- //
// ----- type definitions ---------------------------------------------- //
// --------------------------------------------------------------------- //
// --------------------------------------------------------------------- //
// ----- class definitions --------------------------------------------- //
// --------------------------------------------------------------------- //
struct XmlProcessor : public XmlNullProcessor
{
struct ProcessorNode : public OsmNode
{
OsmLink::Type maxWayType;
ProcessorNode()
{
maxWayType = OsmLink::Unkown;
}
};
struct ProcessorPlace : public OsmPlace
{
Type m_type;
};
typedef Array<OsmNodeKeyT> WayPoints;
enum OsmDirection
{
odBOTH, odFROM_START, odFROM_END
};
struct ProcessorWay : public OsmLink
{
OsmWayKeyT m_newWayID;
OsmKeyT m_relationID;
WayPoints m_wayPoints;
OsmDirection m_direction;
};
typedef PairMap<STRING, std::size_t> ValueCounter;
typedef Array<OsmWayKeyT> OuterWays;
typedef TreeMap<OsmWayKeyT, ProcessorWay> Ways;
typedef TreeMap<OsmNodeKeyT, ProcessorNode> Nodes;
typedef PairMap<tileid_t,OSMbuilder> MapData;
enum OsmElement
{
oeUNKOWN, oeNODE, oeWAY, oeRELATION
};
ValueCounter m_tagCounter,
m_attributeCounter,
m_wayTypeCounter,
m_waterWayCounter[3],
m_waterCounter[3],
m_naturalCounter[3],
m_railTypeCounter,
m_railUsageCounter,
m_railServiceCounter,
m_placeCounter,
m_landuseCounter[3];
bool m_buildTiles;
STRING m_tilesPath;
MapData m_map;
Nodes m_allNodes;
Ways m_ways;
OsmElement m_osmElement;
OsmLink::Type m_highway,
m_waterway,
m_water,
m_natural,
m_railway,
m_landuse;
// reading a place
ProcessorPlace m_newPlace;
// reading a way
ProcessorWay m_newWay;
// for railways
STRING m_usage, m_service;
// reading a relation
OsmKeyT m_relationID;
OuterWays m_outerWays;
static OsmLinkKeyT getNextLinkID( tileid_t tileID )
{
static uint32 baseID = 0;
return OsmLinkKeyT(tileID)<<32| ++baseID;
}
XmlProcessor( bool buildTiles, const STRING &tilesPath = nullptr)
: m_osmElement(oeUNKOWN), m_buildTiles(buildTiles), m_tilesPath(tilesPath)
{}
static void showCounter( std::size_t count, const STRING &value )
{
static StopWatch watch( true );
static StopWatch total( true );
if( watch.getMillis() > 5000 )
{
std::cout << value.padCopy( 16, STR_P_LEFT ) << ": "
<< formatNumber( count, 0, 0, ',' ) << ' '
<< total.get<Minutes<> >().toString() << " \r"
<< std::flush;
watch.start();
}
}
static void countValue( ValueCounter &counter, const STRING &value )
{
std::size_t &count = counter[value];
count += 1;
showCounter( count, value );
}
static void printCounter( std::ostream &out, const STRING name, const ValueCounter &counter )
{
out << name.padCopy( 32, STR_P_LEFT ) << "----------------------------------------\n";
for(
XmlProcessor::ValueCounter::const_iterator it = counter.cbegin(),
endIT = counter.cend();
it != endIT;
++it
)
{
out << it->getKey().padCopy( 32, STR_P_LEFT ) << ": " << it->getValue() << '\n';
}
}
STRING getTileFileName( tileid_t tileID ) const
{
return ::getTileFileName( m_tilesPath, tileID );
}
OSMbuilder &getMap( tileid_t tileID )
{
if( m_buildTiles )
{
if( !m_map.hasElement( tileID ) )
{
OSMbuilder &newMap = m_map[tileID];
STRING fileName = getTileFileName( tileID );
if( exists( fileName ) )
readFromBinaryFile( fileName, &newMap, OSM_MAGIC2, VERSION_MAGIC, false );
return newMap;
}
return m_map[tileID];
}
else
{
m_map.setChunkSize(1);
return m_map[0];
}
}
bool hasArea( OsmAreaKeyT id ) const
{
for(MapData::const_iterator it = m_map.cbegin(), endIT = m_map.cend(); it != endIT; ++it )
{
if( it->getValue().hasArea( id ) )
return true;
}
return false;
}
size_t getNumNodes() const
{
size_t numNodes = 0;
for(MapData::const_iterator it = m_map.cbegin(), endIT = m_map.cend(); it != endIT; ++it )
{
numNodes += it->getValue().getNumNodes();
}
return numNodes;
}
size_t getNumAreas() const
{
size_t numAreas = 0;
for(MapData::const_iterator it = m_map.cbegin(), endIT = m_map.cend(); it != endIT; ++it )
{
numAreas += it->getValue().getNumAreas();
}
return numAreas;
}
size_t getNumPlaces() const
{
size_t numPlaces = 0;
for(MapData::const_iterator it = m_map.cbegin(), endIT = m_map.cend(); it != endIT; ++it )
{
numPlaces += it->getValue().getNumPlaces();
}
return numPlaces;
}
size_t getNumLinks() const
{
size_t numLinks = 0;
for(MapData::const_iterator it = m_map.cbegin(), endIT = m_map.cend(); it != endIT; ++it )
{
numLinks += it->getValue().getNumLinks();
}
return numLinks;
}
void saveTiles()
{
for(MapData::const_iterator it = m_map.cbegin(), endIT = m_map.cend(); it != endIT; ++it )
{
writeToBinaryFile( getTileFileName(it->getKey()), it->getValue(), OSM_MAGIC2, VERSION_MAGIC, owmOverwrite );
}
}
void initItem()
{
m_highway = m_waterway = m_water = m_natural = m_railway = m_landuse = m_newWay.type = OsmLink::Unkown;
m_newPlace.m_type = OsmPlace::Unkown;
m_newPlace.name = NULL_STRING;
m_newWay.m_wayPoints.clear();
m_newWay.m_direction = odBOTH;
m_newWay.m_relationID = 0;
m_usage = NULL_STRING;
m_service = NULL_STRING;
m_outerWays.clear();
}
void processTag( const STRING &tag )
{
if( tag == NODE )
{
m_osmElement = oeNODE;
initItem();
}
else if( tag == WAY )
{
m_osmElement = oeWAY;
initItem();
}
else if( tag == RELATION )
{
m_osmElement = oeRELATION;
initItem();
}
countValue( m_tagCounter, tag );
}
void processAttribute(
const STRING &tag, const STRING &attribute, const STRING &value
)
{
countValue( m_attributeCounter, attribute );
}
void processNodeTagAttributes(
const PairMap<STRING,STRING> &attributes
)
{
if( attributes[K] == PLACE )
{
STRING placeType = attributes[V];
countValue( m_placeCounter, placeType );
if( placeType == CONTINENT )
{
m_newPlace.m_type = OsmPlace::continent;
}
else if( placeType == COUNTRY )
{
m_newPlace.m_type = OsmPlace::country;
}
else if( placeType == STATE )
{
m_newPlace.m_type = OsmPlace::state;
}
else if( placeType == REGION )
{
m_newPlace.m_type = OsmPlace::region;
}
else if( placeType == "province" )
{
m_newPlace.m_type = OsmPlace::province;
}
else if( placeType == DISTRICT )
{
m_newPlace.m_type = OsmPlace::district;
}
else if( placeType == COUNTY )
{
m_newPlace.m_type = OsmPlace::county;
}
else if( placeType == MUNICIPALITY )
{
m_newPlace.m_type = OsmPlace::municipality;
}
else if( placeType == CITY )
{
m_newPlace.m_type = OsmPlace::city;
}
else if( placeType == TOWN )
{
m_newPlace.m_type = OsmPlace::town;
}
else if( placeType == VILLAGE )
{
m_newPlace.m_type = OsmPlace::village;
}
else if( placeType == HAMLET )
{
m_newPlace.m_type = OsmPlace::hamlet;
}
else if( placeType == ALLOTMENTS )
{
m_newPlace.m_type = OsmPlace::allotments;
}
else if( placeType == ISOLATED_DWELING )
{
m_newPlace.m_type = OsmPlace::isolated_dweling;
}
else if( placeType == FARM )
{
m_newPlace.m_type = OsmPlace::farm;
}
else if( placeType == BOROUGH )
{
m_newPlace.m_type = OsmPlace::borough;
}
else if( placeType == QUARTER )
{
m_newPlace.m_type = OsmPlace::quarter;
}
else if( placeType == SUBURB )
{
m_newPlace.m_type = OsmPlace::suburb;
}
else if( placeType == NEIGHBOURHOOD )
{
m_newPlace.m_type = OsmPlace::neighbourhood;
}
else if( placeType == CITY_BLOCK )
{
m_newPlace.m_type = OsmPlace::city_block;
}
else if( placeType == PLOT )
{
m_newPlace.m_type = OsmPlace::plot;
}
else if( placeType == ISLAND )
{
m_newPlace.m_type = OsmPlace::island;
}
else if( placeType == ISLET )
{
m_newPlace.m_type = OsmPlace::islet;
}
else if( placeType == SQUARE )
{
m_newPlace.m_type = OsmPlace::square;
}
else if( placeType == LOCALITY )
{
m_newPlace.m_type = OsmPlace::locality;
}
}
else if( attributes[K] == NAME )
{
m_newPlace.name = attributes[V];
if( m_newPlace.name.getCharSet() == STR_UTF8 )
{
m_newPlace.name = m_newPlace.name.decodeUTF8();
}
}
}
static OsmLink::Type parseHighway( STRING wayType )
{
if( wayType == MOTORWAY || wayType == MOTORWAY_LINK )
{
return OsmLink::motorway;
}
else if( wayType == TRUNK || wayType == TRUNK_LINK )
{
return OsmLink::trunk;
}
else if( wayType == PRIMARY || wayType == PRIMARY_LINK )
{
return OsmLink::primary;
}
else if( wayType == SECONDARY || wayType == SECONDARY_LINK )
{
return OsmLink::secondary;
}
else if( wayType == TERTIARY || wayType == TERTIARY_LINK )
{
return OsmLink::tertiary;
}
else if( wayType == UNCLASSIFIED )
{
return OsmLink::unclassified;
}
else if( wayType == RESIDENTIAL )
{
return OsmLink::residential;
}
else if( wayType == SERVICE )
{
return OsmLink::service;
}
else if( wayType == LIVING_STREET )
{
return OsmLink::living_street;
}
else if( wayType == PEDESTRIAN )
{
return OsmLink::pedestrian;
}
else if( wayType == TRACK )
{
return OsmLink::track;
}
else if( wayType == BUS_GUIDEWAY )
{
return OsmLink::bus_guideway;
}
else if( wayType == WT_ESCAPE )
{
return OsmLink::escape;
}
else if( wayType == RACEWAY )
{
return OsmLink::raceway;
}
else if( wayType == ROAD )
{
return OsmLink::road;
}
else if( wayType == FOOTWAY )
{
return OsmLink::footway;
}
else if( wayType == CYCLEWAY )
{
return OsmLink::cycleway;
}
else if( wayType == BRIDLEWAY )
{
return OsmLink::bridleway;
}
else if( wayType == STEPS )
{
return OsmLink::steps;
}
else if( wayType == PATH )
{
return OsmLink::path;
}
return OsmLink::Unkown;
}
static OsmLink::Type parseWaterway( STRING waterType )
{
if( waterType == RIVER )
{
return OsmLink::river;
}
else if( waterType == RIVERBANK )
{
return OsmLink::riverbank;
}
else if( waterType == CANAL )
{
return OsmLink::canal;
}
else if( waterType == STREAM )
{
return OsmLink::stream;
}
return OsmLink::Unkown;
}
static OsmLink::Type parseWater( STRING waterType )
{
if( waterType == LAKE )
{
return OsmLink::lake;
}
return OsmLink::Unkown;
}
static OsmLink::Type parseNatural( STRING naturalType )
{
if( naturalType == WATER )
{
return OsmLink::water;
}
else if( naturalType == BAY )
{
return OsmLink::bay;
}
else if( naturalType == WETLAND )
{
return OsmLink::wetland;
}
else if( naturalType == WOOD )
{
return OsmLink::wood;
}
else if( naturalType == SCRUB )
{
return OsmLink::scrub;
}
else if( naturalType == GRASS )
{
return OsmLink::grass;
}
else if( naturalType == GRASSLAND )
{
return OsmLink::grassland;
}
return OsmLink::Unkown;
}
static OsmLink::Type parseLanduse( STRING usageType )
{
if( usageType == FOREST )
{
return OsmLink::forest;
}
else if( usageType == FARMLAND )
{
return OsmLink::farmland;
}
else if( usageType == FARMYARD )
{
return OsmLink::farmyard;
}
else if( usageType == MEADOW )
{
return OsmLink::meadow;
}
else if( usageType == GRASS )
{
return OsmLink::grass;
}
return OsmLink::Unkown;
}
static OsmLink::Type parseRailway( STRING railwayType )
{
if( railwayType == RAIL )
{
return OsmLink::secondRailway;
}
else if( railwayType == SUBWAY )
{
return OsmLink::subway;
}
else if( railwayType == TRAM )
{
return OsmLink::tramway;
}
return OsmLink::Unkown;
}
void processTagAttributes(
OsmElement element, const PairMap<STRING,STRING> &attributes
)
{
if( attributes[K] == HIGHWAY )
{
STRING wayType = attributes[V];
countValue( m_wayTypeCounter, wayType );
m_highway = parseHighway( wayType );
}
else if( attributes[V] == ONEWAY )
{
STRING direction = attributes[V];
if( direction == YES )
{
m_newWay.m_direction = odFROM_START;
}
else if( direction == MINUS1 )
{
m_newWay.m_direction = odFROM_END;
}
}
else if( attributes[K] == RAILWAY )
{
STRING railwayType = attributes[V];
countValue( m_railTypeCounter, railwayType );
m_railway = parseRailway( railwayType );
}
else if( attributes[K] == USAGE )
{
m_usage = attributes[V];
countValue( m_railUsageCounter, m_usage );
}
else if( attributes[K] == SERVICE )
{
m_service = attributes[V];
countValue( m_railServiceCounter, m_service );
}
else if( attributes[K] == WATERWAY )
{
STRING waterType = attributes[V];
countValue( m_waterWayCounter[element-1], waterType );
m_waterway = parseWaterway( waterType );
}
else if( attributes[K] == WATER )
{
STRING waterType = attributes[V];
countValue( m_waterCounter[element-1], waterType );
m_water = parseWater( waterType );
}
else if( attributes[K] == NATURAL )
{
STRING naturalType = attributes[V];
countValue( m_naturalCounter[element-1], naturalType );
m_natural = parseNatural( naturalType );
}
else if( attributes[K] == LANDUSE )
{
STRING landuse = attributes[V];
countValue( m_landuseCounter[element-1], landuse );
m_landuse = parseLanduse( landuse );
}
}
void processAttributes(
const STRING &tag, const PairMap<STRING,STRING> &attributes
)
{
if( m_osmElement == oeNODE )
{
if( tag == NODE )
{
ProcessorNode newNode;
OsmNodeKeyT id = attributes["id"].getValueE<OsmNodeKeyT>();
m_newPlace.pos.longitude = newNode.pos.longitude = attributes["lon"].getValueE<float>();
m_newPlace.pos.latitude = newNode.pos.latitude = attributes["lat"].getValueE<float>();
m_allNodes[id] = newNode;
}
else if( tag == TAG )
{
processNodeTagAttributes( attributes );
}
}
else if( m_osmElement == oeWAY )
{
if( tag == WAY )
{
m_newWay.m_newWayID = attributes[ID].getValueE<OsmWayKeyT>();
}
else if( tag == ND )
{
OsmNodeKeyT ref = attributes.cbegin()->getValue().getValueE<OsmNodeKeyT>();
m_newWay.m_wayPoints.addElement( ref );
}
else if( tag == TAG )
{
processTagAttributes( m_osmElement, attributes );
}
}
else if( m_osmElement == oeRELATION )
{
if( tag == RELATION )
{
m_relationID = attributes[ID].getValueE<OsmKeyT>();
}
else if( tag == MEMBER )
{
if( attributes[TYPE] == WAY && attributes[ROLE] == OUTER )
{
m_outerWays.addElement( attributes[REF].getValueE<OsmWayKeyT>() );
}
}
else if( tag == TAG )
{
processTagAttributes( m_osmElement, attributes );
}
}
}
short getPriority(
OsmLink::Type minWayType,
OsmLink::Type maxWayType,
OsmLink::Type wayType
)
{
return (maxWayType - wayType+1)*1000 / (maxWayType - minWayType-1);
}
short getPriority( OsmLink::Type wayType )
{
if( wayType >= OsmLink::minWayType && wayType <= OsmLink::maxWayType )
{
return getPriority(OsmLink::minWayType, OsmLink::maxWayType, wayType );
}
else if( wayType >= OsmLink::minWaterType && wayType <= OsmLink::maxWaterType )
{
return getPriority(OsmLink::minWaterType, OsmLink::maxWaterType, wayType );
}
else if( wayType >= OsmLink::minRailType && wayType <= OsmLink::maxRailType )
{
return getPriority(OsmLink::minRailType, OsmLink::maxRailType, wayType );
}
return 0;
}
const ProcessorNode &addWayPoint( OsmNodeKeyT nodeID, OsmLink::Type wayType )
{
ProcessorNode &node = m_allNodes[nodeID];
math::tileid_t tileID = node.getTileID();
OSMbuilder &map = getMap( tileID );
if( !map.hasNode( nodeID ) )
{
node.maxWayType = wayType;
map.addNode( node.maxWayType, nodeID, node );
}
else if( wayType != node.maxWayType && getPriority(wayType) > getPriority(node.maxWayType) )
{
map.moveNode( node.maxWayType, wayType, nodeID );
node.maxWayType = wayType;
}
return node;
}
void addWayPoints( ProcessorWay &way )
{
WayPoints::const_iterator it = way.m_wayPoints.cbegin(),
endIT = way.m_wayPoints.cend();
OsmNodeKeyT startNodeID = *it;
OsmNode startNode = addWayPoint( startNodeID, way.type );;
++it;
do
{
OsmNodeKeyT endNodeID = *it;
OsmNode endNode = addWayPoint( endNodeID, way.type );
float distance = float(getDistance( startNode.getPosition(), endNode.getPosition() ));
way.length = distance;
math::tileid_t startID = startNode.getTileID();
math::tileid_t endID = endNode.getTileID();
OsmLinkKeyT linkID = getNextLinkID( startID );
OSMbuilder &startMap = getMap( startID );
OSMbuilder &endMap = getMap( endID );
if( way.m_direction == odBOTH )
{
if( !startMap.hasLink(linkID) )
{
startMap.addLink(
linkID,
way,
startNodeID, endNodeID
);
}
if( !endMap.hasLink(-linkID) )
{
endMap.addLink(
-linkID,
way,
endNodeID, startNodeID
);
}
}
else if( way.m_direction == odFROM_START )
{
if( !startMap.hasLink(linkID) )
{
startMap.addLink(
linkID,
way,
startNodeID, endNodeID
);
}
}
else if( way.m_direction == odFROM_END )
{
if( !endMap.hasLink(linkID) )
{
endMap.addLink(