source: subversion/applications/utils/gary68/areacheck.pl @ 17203

Last change on this file since 17203 was 17203, checked in by gary68, 10 years ago

areacheck new version with api tests, less false positives

  • Property svn:executable set to *
File size: 10.2 KB
Line 
1#
2# areacheck by gary68
3#
4#
5#
6#
7# Copyright (C) 2008, 2009, Gerhard Schwanz
8#
9# 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
10# Free Software Foundation; either version 3 of the License, or (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License along with this program; if not, see <http://www.gnu.org/licenses/>
16#
17#
18# 1.0 B 003
19# - added lots of tags
20#
21# 1.0 B 004
22#
23# 1.0 B 005
24# - hash get node info
25# - additional gpx output
26#
27# 1.1
28# - add more links to HTML
29#
30# 3.0
31# - online api support
32#
33
34
35use strict ;
36use warnings ;
37
38use OSM::osm 4.9 ;
39
40my @areas = qw (area:yes waterway:riverbank aeroway:terminal aeroway:apron man_made:surveillance building:yes leisure:park leisure:playground
41        amenity:bus_station amenity:college
42        amenity:ferry_terminal amenity:hospital amenity:parking amenity:school amenity:university tourism:attraction tourism:zoo tourism:museum
43        landuse:forest landuse:residential landuse:industrial landuse:cemetery natural:glacier natural:wood natural:water ) ;
44
45#my @areas = qw (area:yes waterway:riverbank waterway:dock railway:turntable landuse:railway aeroway:terminal aeroway:apron
46#       aerialway:station power:station power:sub_station man_made:reservoir_covered man_made:surveillance
47#       man_made:wastewater_plant man_made:watermill man_made:water_works building:yes
48#       leisure:golf_course leisure:sports_center leisure:stadium leisure:track leisure:pitch leisure:water_park leisure:marina
49#       leisure:fishing leisure:nature_reserve leisure:park leisure:playground leisure:garden leisure:common
50#       amenity:bicycle_parking amenity:bus_station amenity:car_rental amenity:car_sharing amenity:college
51#       amenity:ferry_terminal amenity:fountain amenity:hospital amenity:kindergarten amenity:parking amenity:place_of_worship
52#       amenity:public_building amenity:school amenity:taxi amenity:townhall amenity:university amenity:verterinary
53#       shop:kiosk shop:supermarket tourism:chalet tourism:camp_site tourism:caravan_site tourism:picnic_site tourism:theme_park
54#       tourism:attraction tourism:zoo tourism:museum historic:archeological_site historic:ruins historic:battlefield historic:wreck
55#       landuse:farm landuse:farm_yard landuse:quarry landuse:landfill landuse:basin landuse:reservoir landuse:forest
56#       landuse:allotments landuse:residential landuse:retail landuse:commercial landuse:industrial landuse:brownfield landuse:greenfield
57#       landuse:railway landuse:construction landuse:military landuse:cemetery landuse:meadow landuse:village_green
58#       landuse:recreation_ground military:airfield military:barracks military:danger_area military:range military:naval_base
59#       natural:glacier natural:scree natural:scrub natural:fell natural:heath natural:wood natural:marsh natural:wetland
60#       natural:water natural:mud natural:beach natural:bay natural:land natural:cave_entrance
61#       boundary:administrative boundary:civil boundary:political boundary:national_park
62#       place:region place:county place:city place:town place:village place:hamlet place:suburb place:locality place:island) ;
63
64
65my $program = "areacheck.pl" ;
66my $version = "3.0" ;
67my $usage = $program . " file.osm out.htm out.gpx" ;
68
69my $wayId ;
70my $wayId2 ;
71my $wayUser ;
72my @wayNodes ;
73my @wayTags ;
74my $nodeId ;
75my $nodeId2 ;
76my $nodeUser ;
77my $nodeLat ;
78my $nodeLon ;
79my @nodeTags ;
80my $aRef1 ;
81my $aRef2 ;
82my $wayCount = 0 ;
83my $areaCount = 0 ;
84my $areaOpenCount = 0 ;
85
86my $time0 = time() ; my $time1 ;
87my $i ;
88my $key ;
89my $num ;
90my $tag1 ; my $tag2 ;
91
92my $APIcount = 0 ;
93my $APIerrors = 0 ;
94my $APIrejected = 0 ;
95
96my $html ;
97my $gpx ;
98my $osmName ;
99my $htmlName ;
100my $gpxName ;
101
102
103my @open ;
104my @neededNodes ;
105my %neededNodesHash ;
106my %lon ;
107my %lat ;
108my %wayStart ;
109my %wayEnd ;
110my %openWayTags ;
111my %openWayNodes ;
112
113
114###############
115# get parameter
116###############
117$osmName = shift||'';
118if (!$osmName)
119{
120        die (print $usage, "\n");
121}
122
123$htmlName = shift||'';
124if (!$htmlName)
125{
126        die (print $usage, "\n");
127}
128
129$gpxName = shift||'';
130if (!$gpxName)
131{
132        die (print $usage, "\n");
133}
134
135print "\n$program $version for file $osmName\n\n" ;
136foreach (@areas) {
137        print $_, " " ;
138}
139print "\n\n" ;
140
141
142
143
144
145######################
146# skip all nodes first
147######################
148openOsmFile ($osmName) ;
149print "INFO: pass1: skipping nodes...\n" ;
150skipNodes () ;
151
152
153#####################
154# identify open areas
155#####################
156print "INFO: pass1: find open areas...\n" ;
157($wayId, $wayUser, $aRef1, $aRef2) = getWay () ;
158if ($wayId != -1) {
159        @wayNodes = @$aRef1 ;
160        @wayTags = @$aRef2 ;
161}
162while ($wayId != -1) { 
163        $wayCount++ ;
164
165        my $found = 0 ;
166        foreach $tag1 (@wayTags) {
167                foreach $tag2 (@areas) {
168                        if ($tag1 eq $tag2) { $found = 1 ; }
169                }
170        }
171
172        if ($found) { 
173                $areaCount++ ;
174                if ($wayNodes[0] != $wayNodes[-1]) {
175                        # check API way data
176                        #print "request API data for way $wayId...\n" ;
177                        sleep (1) ; # don't stress API
178                        $APIcount++ ;
179                        my ($id, $u, @nds, @tags, $ndsRef, $tagRef) ;
180                        ($id, $u, $ndsRef, $tagRef) = APIgetWay ($wayId) ;
181                        #print "API request finished.\n" ;
182                        @nds = @$ndsRef ; 
183                        @tags = @$tagRef ;
184
185                        if ($id == 0) { $APIerrors++ ; }
186
187                        #print "nodes: @nds\n" ;
188                        #print "WayId=$wayId Id=$id wayNodes:", scalar (@wayNodes), " nds:", scalar (@nds), "\n" ;
189
190                        if ( ( scalar (@wayNodes) != scalar (@nds)) and ($wayId == $id) ) { 
191                                print "WARNING: way $wayId has different node count in API call. Ignoring...\n" ;
192                                $APIrejected++ ;
193                        }
194                        else {
195                                $areaOpenCount ++ ;
196                                push @open, $wayId ;
197                                $wayStart{$wayId} = $wayNodes[0] ; 
198                                $wayEnd{$wayId} = $wayNodes[-1] ; 
199                                @{$openWayTags{$wayId}} = @wayTags ;
200                                @{$openWayNodes{$wayId}} = @wayNodes ;
201                        }
202                }
203        }
204
205        # next way
206        ($wayId, $wayUser, $aRef1, $aRef2) = getWay () ;
207        if ($wayId != -1) {
208                @wayNodes = @$aRef1 ;
209                @wayTags = @$aRef2 ;
210        }
211}
212
213closeOsmFile () ;
214
215print "INFO: number total ways: $wayCount\n" ;
216print "INFO: number areas: $areaCount\n" ;
217print "INFO: number open areas: $areaOpenCount\n" ;
218print "INFO: API calls: $APIcount\n" ;
219print "INFO: API errors: $APIerrors\n" ;
220print "INFO: API rejections: $APIrejected\n" ;
221
222
223
224######################
225# collect needed nodes
226######################
227print "INFO: pass2: collect needed nodes...\n" ;
228foreach $wayId (@open) {
229        push @neededNodes, $wayStart{$wayId} ;
230        push @neededNodes, $wayEnd{$wayId} ;
231}
232
233
234######################
235# get node information
236######################
237print "INFO: pass2: get node information...\n" ;
238openOsmFile ($osmName) ;
239
240foreach (@neededNodes) { $neededNodesHash{$_} = 1 ; }
241
242($nodeId, $nodeLon, $nodeLat, $nodeUser, $aRef1) = getNode () ;
243if ($nodeId != -1) {
244        #@nodeTags = @$aRef1 ;
245}
246
247while ($nodeId != -1) {
248
249        if (exists ($neededNodesHash{$nodeId}) ) { 
250                $lon{$nodeId} = $nodeLon ; $lat{$nodeId} = $nodeLat
251        }
252
253        # next
254        ($nodeId, $nodeLon, $nodeLat, $nodeUser, $aRef1) = getNode () ;
255        if ($nodeId != -1) {
256                #@nodeTags = @$aRef1 ;
257        }
258}
259
260closeOsmFile () ;
261
262$time1 = time () ;
263
264
265######################
266# PRINT HTML/GPX INFOS
267######################
268print "\nINFO: write HTML tables...\n" ;
269
270open ($html, ">", $htmlName) || die ("Can't open html output file") ;
271open ($gpx, ">", $gpxName) || die ("Can't open gpx output file") ;
272
273printHTMLHeader ($html, "$program by Gary68") ;
274printGPXHeader ($gpx) ;
275
276print $html "<H1>$program by Gary68</H1>\n" ;
277print $html "<p>Version ", $version, "</p>\n" ;
278
279print $html "<p>Check ways with following tags:</p>\n" ;
280print $html "<p>" ;
281foreach (@areas) {
282        print $html $_, " " ;
283}
284print $html "</p>" ;
285
286
287
288print $html "<H2>Statistics</H2>\n" ;
289print $html "<p>", stringFileInfo ($osmName), "<br>\n" ;
290print $html "number ways total: $wayCount<br>\n" ;
291print $html "number areas: $areaCount</p>\n" ;
292print $html "number open areas: $areaOpenCount</p>\n" ;
293print $html "number API errors: $APIerrors</p>\n" ;
294
295
296print $html "<H2>Open Areas</H2>\n" ;
297print $html "<p>These ways have to be closed areas according to map features but the first node is not the same as the last. So area is probably not closed or not properly so. It is possible that parts of the way are drawn doubly (thus closing the area in a way).</p>" ;
298print $html "<table border=\"1\" width=\"100%\">\n";
299print $html "<tr>\n" ;
300print $html "<th>Line</th>\n" ;
301print $html "<th>WayId</th>\n" ;
302print $html "<th>Tags</th>\n" ;
303print $html "<th>Nodes</th>\n" ;
304print $html "<th>Distance start/end</th>\n" ;
305print $html "<th>start/end node id</th>\n" ;
306print $html "<th>OSM start/end</th>\n" ;
307print $html "<th>OSB start/end</th>\n" ;
308print $html "<th>JOSM start/end</th>\n" ;
309print $html "</tr>\n" ;
310$i = 0 ;
311foreach $wayId (@open) {
312        $i++ ;
313
314        print $html "<tr>\n" ;
315        print $html "<td>", $i , "</td>\n" ;
316        print $html "<td>", historyLink ("way", $wayId) , "</td>\n" ;
317
318        print $html "<td>" ;
319        foreach (@{$openWayTags{$wayId}}) { print $html $_, " - " ; }
320        print $html "</td>\n" ;
321
322        print $html "<td>" ;
323        foreach (@{$openWayNodes{$wayId}}) { print $html $_, " - " ; }
324        print $html "</td>\n" ;
325
326        my $dist = distance ($lon{$wayStart{$wayId}},$lat{$wayStart{$wayId}},$lon{$wayEnd{$wayId}},$lat{$wayEnd{$wayId}}) * 1000 ;
327        printf $html "<td>%.0f m</td>\n", $dist ;
328
329        print $html "<td>", $wayStart{$wayId}, " / ", $wayEnd{$wayId}, "</td>\n" ;
330        print $html "<td>", osmLink ($lon{$wayStart{$wayId}}, $lat{$wayStart{$wayId}}, 16) , " ", osmLink ($lon{$wayEnd{$wayId}}, $lat{$wayEnd{$wayId}}, 16), "</td>\n" ;
331        print $html "<td>", osbLink ($lon{$wayStart{$wayId}}, $lat{$wayStart{$wayId}}, 16) , " ", osbLink ($lon{$wayEnd{$wayId}}, $lat{$wayEnd{$wayId}}, 16), "</td>\n" ;
332        print $html "<td>", josmLink ($lon{$wayStart{$wayId}}, $lat{$wayStart{$wayId}}, 0.01, $wayId), " ", josmLink ($lon{$wayEnd{$wayId}}, $lat{$wayEnd{$wayId}}, 0.01, $wayId), "</td>\n" ;
333
334        print $html "</tr>\n" ;
335
336        # GPX
337        my $text = $wayId . " - area way not closed or doubly drawn segments" ;
338        printGPXWaypoint ($gpx, $lon{$wayStart{$wayId}}, $lat{$wayStart{$wayId}}, $text) ;
339}
340
341print $html "</table>\n" ;
342print $html "<p>$i lines total</p>\n" ;
343
344
345
346########
347# FINISH
348########
349print $html "<p>", stringTimeSpent ($time1-$time0), "</p>\n" ;
350printHTMLFoot ($html) ;
351printGPXFoot ($gpx) ;
352
353close ($html) ;
354close ($gpx) ;
355
356print "\nINFO: finished after ", stringTimeSpent ($time1-$time0), "\n\n" ;
357
358
Note: See TracBrowser for help on using the repository browser.