1 |
#! /usr/bin/perl
|
2 |
# Do a
|
3 |
# svn co http://svn.openstreetmap.org/applications/utils/perl_lib/Geo
|
4 |
# and
|
5 |
# svn co http://svn.openstreetmap.org/applications/utils/perl_lib/Utils
|
6 |
# first
|
7 |
# Example usage:
|
8 |
# ./border_follower.pl 10359135 Germany 2 country state
|
9 |
use Geo::OSM::APIClientV5;
|
10 |
use Data::Dumper;
|
11 |
$direction='left';
|
12 |
if ($ARGV[0] eq '-r') {
|
13 |
$direction='right';
|
14 |
shift(@ARGV);
|
15 |
}
|
16 |
$first_wayid=$ARGV[0];
|
17 |
$name=$ARGV[1];
|
18 |
$required_admin_level=$ARGV[2];
|
19 |
$type=$ARGV[3];
|
20 |
$alt_type=$ARGV[4];
|
21 |
$revers=0;
|
22 |
$api=new Geo::OSM::APIClient(api=>'http://www.openstreetmap.org/api/0.5');
|
23 |
$wayid=$first_wayid;
|
24 |
$path="$first_wayid";
|
25 |
sub error
|
26 |
{
|
27 |
my ($message)=@_;
|
28 |
$node=$api->get_node($last);
|
29 |
print "$message at Node $last $node->{lat} $node->{lon}\n";
|
30 |
$lat=$node->{lat};
|
31 |
$lon=$node->{lon};
|
32 |
$latl=$node->{lat}-0.01;
|
33 |
$lath=$node->{lat}+0.01;
|
34 |
$lonl=$node->{lon}-0.01;
|
35 |
$lonh=$node->{lon}+0.01;
|
36 |
system("firefox 'http://www.informationfreeway.org/?lat=$lat&lon=$lon&zoom=12&layers=B000F000F' ; wget -O error.osm http://www.openstreetmap.org/api/0.5/map?bbox=$lonl,$latl,$lonh,$lath ; josm error.osm --selection=id:$last");
|
37 |
exit(1);
|
38 |
}
|
39 |
|
40 |
do {
|
41 |
print "Following $wayid\n";
|
42 |
$way=$api->get_way($wayid);
|
43 |
# print Dumper($way->{'tags'});
|
44 |
|
45 |
$nodes=$way->nodes;
|
46 |
$tags = $way->tags;
|
47 |
$reverse=0;
|
48 |
# print Dumper($tags);
|
49 |
while( my($k,$v) = splice @{$tags}, 0, 2 ) {
|
50 |
if (($k eq "$direction:$type" || $k eq "$direction:$alt_type") && $v eq $name) {
|
51 |
$reverse=1;
|
52 |
}
|
53 |
}
|
54 |
if ($reverse) {
|
55 |
$first=$nodes->[$#$nodes];
|
56 |
$last=$nodes->[0];
|
57 |
} else {
|
58 |
$first=$nodes->[0];
|
59 |
$last=$nodes->[$#$nodes];
|
60 |
}
|
61 |
$ways=$api->get_node_ways($last);
|
62 |
|
63 |
# print "first=$first\n";
|
64 |
# print "last=$last\n";
|
65 |
|
66 |
print "$#$ways Ways\n";
|
67 |
if ($#$ways <= 0) {
|
68 |
error "End";
|
69 |
}
|
70 |
$lastid=$wayid;
|
71 |
$count=0;
|
72 |
foreach $way (@$ways) {
|
73 |
$newid=$way->{'id'};
|
74 |
$timestamp=$way->{'timestamp'};
|
75 |
if ($newid == $lastid) {
|
76 |
next;
|
77 |
}
|
78 |
$nodes=$way->nodes;
|
79 |
print "way $newid ($#$nodes nodes) $timestamp\n";
|
80 |
my $tags = $way->tags;
|
81 |
$match=0;
|
82 |
$boundary=0;
|
83 |
$admin_level=0;
|
84 |
while( my($k,$v) = splice @{$tags}, 0, 2 ) {
|
85 |
print "tag: $k=$v\n";
|
86 |
if (($k eq "left:$type" || $k eq "right:$type")) {
|
87 |
if ($v eq $name) {
|
88 |
$match=1;
|
89 |
} else {
|
90 |
$neighbors{$v}=$newid;
|
91 |
}
|
92 |
}
|
93 |
if (($k eq "left:$alt_type" || $k eq "right:$alt_type") && $v eq $name) {
|
94 |
print "Warning: $k in $newid\n";
|
95 |
$match=1;
|
96 |
}
|
97 |
if ($k eq "boundary") {
|
98 |
if ($boundary == 0 && $v eq "administrative") {
|
99 |
$boundary=1;
|
100 |
} else {
|
101 |
$boundary=2;
|
102 |
}
|
103 |
}
|
104 |
if ($k eq "admin_level") {
|
105 |
if ($admin_level == 0 && $v eq $required_admin_level) {
|
106 |
$admin_level=1;
|
107 |
} else {
|
108 |
$admin_level=2;
|
109 |
}
|
110 |
}
|
111 |
}
|
112 |
if ($match) {
|
113 |
print "MATCH\n";
|
114 |
if ($boundary != 1 ) {
|
115 |
print "boundary $boundary wrong at $newid\n"
|
116 |
}
|
117 |
if ($admin_level != 1) {
|
118 |
print "admin_level $admin_level wrong at $newid\n"
|
119 |
}
|
120 |
$wayid=$newid;
|
121 |
$count++;
|
122 |
}
|
123 |
}
|
124 |
if ($count == 0) {
|
125 |
error "No connection"
|
126 |
} else {
|
127 |
if ($count > 1) {
|
128 |
error "Multiple connections ($count)"
|
129 |
} else {
|
130 |
$path="$path $wayid";
|
131 |
}
|
132 |
}
|
133 |
} while ($wayid != $first_wayid);
|
134 |
print "End reached\n";
|
135 |
print "Path $path\n";
|
136 |
while (($key,$value)=each(%neighbors)) {
|
137 |
print "Neighbor $key $value\n";
|
138 |
}
|
139 |
#print Dumper($ways);
|