1 | # |
---|
2 | # |
---|
3 | # selfintersecting.pl |
---|
4 | # |
---|
5 | # |
---|
6 | # |
---|
7 | # |
---|
8 | # Copyright (C) 2009, Gerhard Schwanz |
---|
9 | # |
---|
10 | # 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 |
---|
11 | # Free Software Foundation; either version 3 of the License, or (at your option) any later version. |
---|
12 | # |
---|
13 | # This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
---|
14 | # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
---|
15 | # |
---|
16 | # You should have received a copy of the GNU General Public License along with this program; if not, see <http://www.gnu.org/licenses/> |
---|
17 | # |
---|
18 | # |
---|
19 | |
---|
20 | |
---|
21 | use strict ; |
---|
22 | use warnings ; |
---|
23 | |
---|
24 | |
---|
25 | use OSM::osm 5.0 ; |
---|
26 | |
---|
27 | my $programName = "selfintersecting.pl" ; |
---|
28 | my $usage = "selfintersecting.pl file.osm out.htm out.gpx" ; |
---|
29 | my $version = "1.4" ; |
---|
30 | |
---|
31 | |
---|
32 | |
---|
33 | my $wayId ; |
---|
34 | my $wayUser ; |
---|
35 | my @wayNodes ; |
---|
36 | my @wayTags ; |
---|
37 | my $nodeId ; |
---|
38 | my $nodeUser ; |
---|
39 | my $nodeLat ; |
---|
40 | my $nodeLon ; |
---|
41 | my @nodeTags ; |
---|
42 | my $aRef1 ; |
---|
43 | my $aRef2 ; |
---|
44 | |
---|
45 | my @problems = () ; |
---|
46 | |
---|
47 | my $wayCount = 0 ; |
---|
48 | my $problems = 0 ; |
---|
49 | my $APIcount = 0 ; |
---|
50 | my $APIerrors = 0 ; |
---|
51 | |
---|
52 | my $osmName ; |
---|
53 | my $gpxName ; |
---|
54 | my $htmName ; |
---|
55 | |
---|
56 | my %lon ; my %lat ; |
---|
57 | |
---|
58 | |
---|
59 | my $time0 ; |
---|
60 | |
---|
61 | # get parameter |
---|
62 | |
---|
63 | $osmName = shift||''; |
---|
64 | if (!$osmName) |
---|
65 | { |
---|
66 | die (print $usage, "\n"); |
---|
67 | } |
---|
68 | |
---|
69 | $htmName = shift||''; |
---|
70 | if (!$htmName) |
---|
71 | { |
---|
72 | die (print $usage, "\n"); |
---|
73 | } |
---|
74 | |
---|
75 | $gpxName = shift||''; |
---|
76 | if (!$gpxName) |
---|
77 | { |
---|
78 | die (print $usage, "\n"); |
---|
79 | } |
---|
80 | |
---|
81 | |
---|
82 | print "\n$programName $version for file $osmName\n" ; |
---|
83 | print "\n" ; |
---|
84 | print "ATTENTION!\n" ; |
---|
85 | print "----------\n" ; |
---|
86 | print "This program uses API calls.\n" ; |
---|
87 | print "Although generally running this program once on a relatively small planet-excerpt\n" ; |
---|
88 | print "file poses no threat to the API you might be able to disturb the API\n" ; |
---|
89 | print "by running it often or in parallel and on big files.\n" ; |
---|
90 | print "Calls are made at max. 1/sec. (can be coded)\n" ; |
---|
91 | print "\n" ; |
---|
92 | |
---|
93 | $time0 = time() ; |
---|
94 | |
---|
95 | print "read node data...\n" ; |
---|
96 | openOsmFile ($osmName) ; |
---|
97 | ($nodeId, $nodeLon, $nodeLat, $nodeUser, $aRef1) = getNode2 () ; |
---|
98 | if ($nodeId != -1) { |
---|
99 | #@nodeTags = @$aRef1 ; |
---|
100 | } |
---|
101 | |
---|
102 | while ($nodeId != -1) { |
---|
103 | |
---|
104 | $lon{$nodeId} = $nodeLon ; |
---|
105 | $lat{$nodeId} = $nodeLat ; |
---|
106 | |
---|
107 | # next |
---|
108 | ($nodeId, $nodeLon, $nodeLat, $nodeUser, $aRef1) = getNode2 () ; |
---|
109 | if ($nodeId != -1) { |
---|
110 | #@nodeTags = @$aRef1 ; |
---|
111 | } |
---|
112 | } |
---|
113 | |
---|
114 | print "done.\n" ; |
---|
115 | |
---|
116 | print "parsing and checking ways...\n" ; |
---|
117 | |
---|
118 | ($wayId, $wayUser, $aRef1, $aRef2) = getWay2 () ; |
---|
119 | if ($wayId != -1) { |
---|
120 | @wayNodes = @$aRef1 ; |
---|
121 | @wayTags = @$aRef2 ; |
---|
122 | } |
---|
123 | while ($wayId != -1) { |
---|
124 | if (scalar (@wayNodes) > 3) { |
---|
125 | $wayCount++ ; |
---|
126 | if ($wayCount % 1000000 == 0) { print "$wayCount ways processed...\n" ;} |
---|
127 | my ($tagText) = "" ; |
---|
128 | my ($highway) = 0 ; |
---|
129 | foreach my $t (@wayTags) { |
---|
130 | $tagText = $tagText . $t->[0] . ":" . $t->[1] . "<br>" ; |
---|
131 | if ($t->[0] eq "highway") { $highway = 1 ; } |
---|
132 | } |
---|
133 | |
---|
134 | $tagText = $tagText . "<br>Nodes: " . scalar (@wayNodes) . "<br><br>Nodes: @wayNodes<br>" ; |
---|
135 | |
---|
136 | my %count = () ; |
---|
137 | foreach my $n (@wayNodes) { $count{$n}++ ; } |
---|
138 | my ($double) = 0 ; my $doubleError = 0 ; |
---|
139 | foreach my $n (keys %count) { |
---|
140 | if ($count{$n}>1) { $double++ ; } |
---|
141 | } |
---|
142 | if ($double > 1) { $doubleError = 1 ; } |
---|
143 | if ( ($double == 1) and ($wayNodes[0] != $wayNodes[-1]) ) { $doubleError = 1 ; } |
---|
144 | |
---|
145 | # check highway exception and possibly reset $doubleError |
---|
146 | my ($doubleNodes) = 0 ; |
---|
147 | foreach my $n (@wayNodes) { |
---|
148 | if ($count{$n} == 2) { $doubleNodes++ ; } |
---|
149 | if ($count{$n} > 2) { $doubleNodes = 100 ; } # at least one node used more than twice -> error |
---|
150 | } |
---|
151 | if ($doubleNodes == 2) { |
---|
152 | if ( ($count{$wayNodes[0]} == 2) and ($count{$wayNodes[-1]} != 2) ) { $doubleError = 0 ; } |
---|
153 | if ( ($count{$wayNodes[-1]} == 2) and ($count{$wayNodes[0]} != 2) ) { $doubleError = 0 ; } |
---|
154 | } |
---|
155 | if ( ($doubleNodes == 4) and ($count{$wayNodes[0]} == 2) and ($count{$wayNodes[-1]} == 2) ) { |
---|
156 | $doubleError = 0 ; |
---|
157 | } |
---|
158 | |
---|
159 | if ($doubleError) { |
---|
160 | $problems++ ; |
---|
161 | my $node ; |
---|
162 | foreach my $n (@wayNodes[1..$#wayNodes-1]) { |
---|
163 | if ($count{$n}>1) { $node = $n ; } |
---|
164 | } |
---|
165 | push @problems, [$wayId, 0, 0, $node, "Node used twice", $lon{$node}, $lat{$node}, $tagText] ; |
---|
166 | } |
---|
167 | else { # check segments |
---|
168 | my $crossingFound = 0 ; |
---|
169 | my ($a, $b) ; |
---|
170 | my ($cLon, $cLat, $seg1, $seg2) ; |
---|
171 | # print $wayId, " ", $#wayNodes, "\n" ; |
---|
172 | for ($a=0; $a<$#wayNodes-1; $a++) { |
---|
173 | for ($b=$a + 2; $b<$#wayNodes; $b++) { |
---|
174 | my ($x, $y) = crossing ($lon{$wayNodes[$a]}, |
---|
175 | $lat{$wayNodes[$a]}, |
---|
176 | $lon{$wayNodes[$a+1]}, |
---|
177 | $lat{$wayNodes[$a+1]}, |
---|
178 | $lon{$wayNodes[$b]}, |
---|
179 | $lat{$wayNodes[$b]}, |
---|
180 | $lon{$wayNodes[$b+1]}, |
---|
181 | $lat{$wayNodes[$b+1]}) ; |
---|
182 | if (($x != 0) and ($y != 0)) { |
---|
183 | #print "\nFOUND $wayId $a $b $x $y\n" ; |
---|
184 | #print "number nodes: ", scalar (@wayNodes), "\n" ; |
---|
185 | #print "nodes list: @wayNodes\n" ; |
---|
186 | #print "$wayNodes[$a] $wayNodes[$a+1] $wayNodes[$b] $wayNodes[$b+1]\n" ; |
---|
187 | #print $lon{$wayNodes[$a]}, " ", $lat{$wayNodes[$a]}, " ", $lon{$wayNodes[$a+1]}, " ", $lat{$wayNodes[$a+1]}, "\n" ; |
---|
188 | #print $lon{$wayNodes[$b]}, " ", $lat{$wayNodes[$b]}, " ", $lon{$wayNodes[$b+1]}, " ", $lat{$wayNodes[$b+1]}, "\n" ; |
---|
189 | $crossingFound = 1 ; |
---|
190 | $seg1 = $a ; $seg2 = $b ; $cLon = $x ; $cLat = $y ; |
---|
191 | } # found |
---|
192 | } # for |
---|
193 | } # for |
---|
194 | |
---|
195 | |
---|
196 | if ($crossingFound == 1) { |
---|
197 | # check API way data |
---|
198 | print "request API data for way $wayId...\n" ; |
---|
199 | $APIcount++ ; |
---|
200 | sleep (1) ; # don't stress API |
---|
201 | my ($id, $u, @nds, @tags, $ndsRef, $tagRef) ; |
---|
202 | ($id, $u, $ndsRef, $tagRef) = APIgetWay ($wayId) ; |
---|
203 | print "API request finished.\n" ; |
---|
204 | @nds = @$ndsRef ; @tags = @$tagRef ; |
---|
205 | if ($id == 0) { $APIerrors++ ; } |
---|
206 | if ( ( scalar @wayNodes != scalar @nds) and ($wayId == $id) ) { |
---|
207 | $crossingFound = 0 ; |
---|
208 | print "WARNING: way $wayId segment crossing but API node count mismatch. Ignoring this way.\n" ; |
---|
209 | } |
---|
210 | if ($crossingFound == 1) { |
---|
211 | $problems++ ; |
---|
212 | push @problems, [$wayId, $seg1, $seg2, 0, "<p>Intersection at<br>" . $cLon . "<br>" . $cLat . "</p>\n", $cLon, $cLat, $tagText] ; |
---|
213 | } |
---|
214 | } |
---|
215 | } |
---|
216 | } |
---|
217 | |
---|
218 | ($wayId, $wayUser, $aRef1, $aRef2) = getWay2 () ; |
---|
219 | if ($wayId != -1) { |
---|
220 | @wayNodes = @$aRef1 ; |
---|
221 | @wayTags = @$aRef2 ; |
---|
222 | } |
---|
223 | } |
---|
224 | |
---|
225 | closeOsmFile() ; |
---|
226 | |
---|
227 | print "done.\n" ; |
---|
228 | print "$wayCount ways found.\n" ; |
---|
229 | print "$problems problems found.\n" ; |
---|
230 | print "$APIcount API calls.\n" ; |
---|
231 | print "$APIerrors API errors.\n\n" ; |
---|
232 | |
---|
233 | |
---|
234 | my $html ; my $gpx ; |
---|
235 | open ($html, ">", $htmName) || die ("Can't open html output file") ; |
---|
236 | open ($gpx, ">", $gpxName) || die ("Can't open gpx output file") ; |
---|
237 | |
---|
238 | |
---|
239 | printHTMLiFrameHeader ($html, "Self intersecting way Check by Gary68") ; |
---|
240 | printGPXHeader ($gpx) ; |
---|
241 | |
---|
242 | print $html "<H1>Self intersecting way Check by Gary68</H1>\n" ; |
---|
243 | print $html "<p>Version ", $version, "</p>\n" ; |
---|
244 | print $html "<H2>Statistics</H2>\n" ; |
---|
245 | print $html "<p>", stringFileInfo ($osmName), "<br>\n" ; |
---|
246 | print $html "number ways total: $wayCount<br>\n" ; |
---|
247 | print $html "number problems: $problems<br>\n" ; |
---|
248 | print $html "</p>\n" ; |
---|
249 | |
---|
250 | |
---|
251 | print $html "<H2>Data</H2>\n" ; |
---|
252 | print $html "<table border=\"1\">\n"; |
---|
253 | print $html "<tr>\n" ; |
---|
254 | print $html "<th>Line</th>\n" ; |
---|
255 | print $html "<th>Way Id (map/highlight)</th>\n" ; |
---|
256 | print $html "<th>Way tags</th>\n" ; |
---|
257 | print $html "<th>Segment 1</th>\n" ; |
---|
258 | print $html "<th>Segment 2</th>\n" ; |
---|
259 | print $html "<th>Node</th>\n" ; |
---|
260 | print $html "<th>Text</th>\n" ; |
---|
261 | print $html "<th>JOSM</th>\n" ; |
---|
262 | print $html "<th>OSM</th>\n" ; |
---|
263 | print $html "<th>OSB</th>\n" ; |
---|
264 | print $html "</tr>\n" ; |
---|
265 | |
---|
266 | my $line = 0 ; |
---|
267 | foreach my $problem (@problems) { |
---|
268 | $line++ ; |
---|
269 | my ($lo) = $problem->[5] ; |
---|
270 | my ($la) = $problem->[6] ; |
---|
271 | |
---|
272 | print $html "<tr>\n" ; |
---|
273 | print $html "<td>", $line , "</td>\n" ; |
---|
274 | print $html "<td>", historyLink ("way", $problem->[0]), "<br>", osmLinkMarkerWay ($lo, $la, 16, $problem->[0]), "</td>\n" ; |
---|
275 | print $html "<td><p>" . $problem->[7] . "</p></td>\n" ; |
---|
276 | print $html "<td>", $problem->[1], "</td>\n" ; |
---|
277 | print $html "<td>", $problem->[2], "</td>\n" ; |
---|
278 | print $html "<td>", historyLink ("node", $problem->[3]), "</td>\n" ; |
---|
279 | print $html "<td>", $problem->[4], "</td>\n" ; |
---|
280 | print $html "<td>", josmLinkSelectWays ($lo, $la, 0.01, $problem->[0]), "</td>\n" ; |
---|
281 | print $html "<td>", osmLink ($lo, $la, 16) , "<br>\n" ; |
---|
282 | print $html "<td>", osbLink ($lo, $la, 16) , "<br>\n" ; |
---|
283 | print $html "</tr>\n" ; |
---|
284 | |
---|
285 | my ($text) = "Self intersecting way - " . $problem->[0] ; |
---|
286 | printGPXWaypoint ($gpx, $lo, $la, $text) ; |
---|
287 | } |
---|
288 | |
---|
289 | print $html "</table>\n" ; |
---|
290 | print $html "<p>", stringTimeSpent (time()-$time0), "</p>\n" ; |
---|
291 | |
---|
292 | printHTMLFoot ($html) ; |
---|
293 | printGPXFoot ($gpx) ; |
---|
294 | |
---|
295 | close ($html) ; |
---|
296 | close ($gpx) ; |
---|
297 | |
---|
298 | |
---|
299 | print "\n$programName finished after ", stringTimeSpent (time()-$time0), "\n\n" ; |
---|
300 | |
---|
301 | |
---|
302 | |
---|
303 | |
---|