c16dc2158c4f7cef8eb31fdfd3d7b898304e8b42
[navit-package] / src / script / osm2navit
1 #!/usr/bin/perl
2 use Data::Dumper;
3 sub deg
4 {
5         my ($deg, $dir)=(@_);
6
7         my ($in,$degi);
8
9         $in=$deg;
10         $degi=int($deg);
11         $deg-=$degi;
12         $deg*=60;
13         return sprintf("%d%09.6f %s", $degi, $deg, $dir);
14 }
15 sub coord
16 {
17         my ($id)=(@_);
18         return deg($lat{$id}, "N") . " " . deg($lon{$id}, "E");
19 }
20
21 while (<>) {
22         $line = $_;
23         chomp($line);
24         #print "$line\n";
25         if ($line =~ /<node id="(.*?)" lat="(.*?)" lon="(.*?)"/) {
26                 $lat{ $1 } = $2;
27                 $lon{ $1 } = $3;
28                 %tag=();
29         } elsif ($line =~ /<segment id="(.*?)" from="(.*?)" to="(.*?)"/) {
30                 $from{ $1 } = $2;
31                 $to{ $1 } = $3;
32                 $reference{ $1 } =0;
33                 %tag=();
34         } elsif ($line =~ /<tag k="(.*?)" v="(.*?)"/) {
35                 if ($1 ne "created_by" && $1 ne "converted_by") {
36                         $tag{ $1 } = $2;
37                 }
38         } elsif ($line =~ /<way id="(.*?)"/) {
39                 %tag=();
40                 $coords="";
41                 $in_way=1;
42                 $way_id=$1;
43                 $start='';
44                 $end='';
45         } elsif ($line =~ /<seg id="(.*?)"/) {
46                 if ($in_way) {
47                         $from=coord($from{ $1 });
48                         $to=coord($to{ $1 });
49                         $reference{ $1 }++;
50                         if ($start eq '') {
51                                 $start=$from;
52                                 $end=$to;
53                                 $coords="$start\n$end\n";
54                                 $start="dummy";
55                                 $end="dummy";
56                         } else {
57                                 if ($start == $from) {
58                                         $coords="$to\n" . $coords;
59                                         $start=$to;
60                                 } elsif ($start == $to) {
61                                         $coords="$from\n" . $coords;
62                                         $start=$from;
63                                 } elsif ($end == $from) {
64                                         $coords.=$to . "\n";
65                                         $end=$to;
66                                 } elsif ($end == $to) {
67                                         $coords.=$from . "\n";
68                                         $end=$from;
69                                 } else {
70                                         push(@coords,$coords);
71                                         $start=$from;
72                                         $end=$to;
73                                         $coords="$start\n$end\n";
74                                         $start="dummy";
75                                         $end="dummy";
76                                         # print STDERR "not connected from=$from to=$to coords=$coords \n";
77                                 }
78                         }
79                 }
80         } elsif ($line =~ /<osm /) {
81         } elsif ($line =~ /<?xml /) {
82         } elsif ($line =~ /<\/node/) {
83                 if (%tag) {
84                         #print STDERR Dumper(\%tag);
85                 }
86         } elsif ($line =~ /<\/segment/) {
87                 if (%tag) {
88                         #print STDERR Dumper(\%tag);
89                 }
90         } elsif ($line =~ /<\/segment/) {
91         } elsif ($line =~ /<\/way/) {
92                 $type='street_unkn';
93                 if ($tag{'highway'} ne '') {
94                         for ($tag{'highway'}) {
95                                 if (/cycleway/ || /footway/ || /steps/ || /cyclepath/ || /track/ || /service/ || /pedestrian/) {
96                                         $type='street_nopass';
97                                 } elsif (/residential/ || /unclassified/) {
98                                         $type='street_1_city';
99                                 } elsif (/tertiary/) {
100                                         $type='street_2_city';
101                                 } elsif (/secondary/) {
102                                         $type='street_3_city';
103                                 } elsif (/primary/) {
104                                         $type='street_4_city';
105                                 } elsif (/motorway/) {
106                                         $type='highway_city';
107                                 } else {
108                                         print STDERR "Unknown highway $_\n";
109                                 }
110
111                         }
112                 }
113                 if ($tag{'natural'} ne '') {
114                         for ($tag{'natural'}) {
115                                 if (/water/) {
116                                         $type='water_poly';
117                                 } else {
118                                         print STDERR "Unknown natural $_\n";
119                                 }
120
121                         }
122                 }
123                 if ($tag{'railway'} ne '') {
124                         for ($tag{'railway'}) {
125                                 if (/rail/) {
126                                         $type='rail';
127                                 } else {
128                                         print STDERR "Unknown railway $_\n";
129                                 }
130
131                         }
132                 }
133                 if ($tag{'waterway'} ne '') {
134                         for ($tag{'waterway'}) {
135                                 if (/canal/) {
136                                         $type='water_line';
137                                 } else {
138                                         print STDERR "Unknown waterway $_\n";
139                                 }
140
141                         }
142                 }
143                 if ($tag{'amenity'} ne '') {
144                         for ($tag{'amenity'}) {
145                                 if (/hospital/) {
146                                         $type='hospital_poly';
147                                 } elsif (/parking/) {
148                                         $type='parking_lot_poly';
149                                 } else {
150                                         print STDERR "Unknown amenity $_\n";
151                                 }
152
153                         }
154                 }
155                 if ($tag{'landuse'} ne '') {
156                         for ($tag{'landuse'}) {
157                                 if (/cemetery/) {
158                                         $type='cemetery_poly';
159                                 } elsif (/forest/||/village_green/||/greenfield/||/farm/) {
160                                         $type='wood';
161                                 } elsif (/industrial/ || /residential/) {
162                                         $type='industry_poly';
163                                 } elsif (/parking/) {
164                                         $type='parking_lot_poly';
165                                 } elsif (/water/) {
166                                         $type='water_poly';
167                                 } else {
168                                         print STDERR "Unknown landuse $_\n";
169                                 }
170
171                         }
172                 }
173                 $oline="type=$type";
174                 if ($tag{'name'} ne '') {
175                         $oline.=' label="' . $tag{'name'} . '"';
176                 }
177                 $oline .= ' debug="way_id=' . $way_id . ' ';
178                 while (($key,$value)=each(%tag)) {
179                         $oline .= "$key='$value' ";
180                 }
181                 $oline .= "\"\n";
182                 push(@coords,$coords);
183                 foreach $coords (@coords) {
184                         print $oline;
185                         print $coords;
186                 }
187                 @coords=();
188         } elsif ($line =~ /<\/osm/) {
189         } else {
190                 print "$line\n";
191         }
192         if ($count++ > 5) {
193                 #exit(0);
194         }
195 }
196 while (($key,$value)=each(%reference)) {
197         if ($value != 1) {
198                 print STDERR "$key $value\n";
199         }
200 }
201