summaryrefslogtreecommitdiff
path: root/script/osm/border_follower.pl
blob: 3a5b311671c1cfcfa42a9a3c67a18bc816e02a11 (plain)
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
#! /usr/bin/perl
# Do a
# svn co http://svn.openstreetmap.org/applications/utils/perl_lib/Geo
# and
# svn co http://svn.openstreetmap.org/applications/utils/perl_lib/Utils
# first
# Example usage:
# ./border_follower.pl 10359135 Germany 2 country state
use Geo::OSM::APIClientV5;
use Data::Dumper;
$direction='left';
if ($ARGV[0] eq '-r') {
	$direction='right';
	shift(@ARGV);
}
$first_wayid=$ARGV[0];
$name=$ARGV[1];
$required_admin_level=$ARGV[2];
$type=$ARGV[3];
$alt_type=$ARGV[4];
$revers=0;
$api=new Geo::OSM::APIClient(api=>'http://www.openstreetmap.org/api/0.5');
$wayid=$first_wayid;
$path="$first_wayid";
sub error
{
	my ($message)=@_;
	$node=$api->get_node($last);
	print "$message at Node $last $node->{lat} $node->{lon}\n";
	$lat=$node->{lat};
	$lon=$node->{lon};
	$latl=$node->{lat}-0.01;
	$lath=$node->{lat}+0.01;
	$lonl=$node->{lon}-0.01;
	$lonh=$node->{lon}+0.01;
	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");
	exit(1);
}

do {
	print "Following $wayid\n";
	$way=$api->get_way($wayid);
#	print Dumper($way->{'tags'});

	$nodes=$way->nodes;
	$tags = $way->tags;
	$reverse=0;
#	print Dumper($tags);
	while( my($k,$v) = splice @{$tags}, 0, 2 ) {
		if (($k eq "$direction:$type" || $k eq "$direction:$alt_type") && $v eq $name) {
			$reverse=1;
		}
	}
	if ($reverse) {
		$first=$nodes->[$#$nodes];
		$last=$nodes->[0];
	} else {
		$first=$nodes->[0];
		$last=$nodes->[$#$nodes];
	}
	$ways=$api->get_node_ways($last);

#	print "first=$first\n";
#	print "last=$last\n";

	print "$#$ways Ways\n";
	if ($#$ways <= 0) {
		error "End";
	}
	$lastid=$wayid;
	$count=0;
	foreach $way (@$ways) {
		$newid=$way->{'id'};
		$timestamp=$way->{'timestamp'};
		if ($newid == $lastid) {
			next;
		}
		$nodes=$way->nodes;
		print "way $newid ($#$nodes nodes) $timestamp\n";
		my $tags = $way->tags;
		$match=0;
		$boundary=0;
		$admin_level=0;
		while( my($k,$v) = splice @{$tags}, 0, 2 ) {
			print "tag: $k=$v\n";
			if (($k eq "left:$type" || $k eq "right:$type")) {
				if ($v eq $name) {
					$match=1;
				} else {
					$neighbors{$v}=$newid;
				}
			}
			if (($k eq "left:$alt_type" || $k eq "right:$alt_type") && $v eq $name) {
				print "Warning: $k in $newid\n";
				$match=1;
			}
			if ($k eq "boundary") {
				if ($boundary == 0 && $v eq "administrative") {
					$boundary=1;
				} else {
					$boundary=2;
				}
			}
			if ($k eq "admin_level") {
				if ($admin_level == 0 && $v eq $required_admin_level) {
					$admin_level=1;
				} else {
					$admin_level=2;
				}
			}
		}
		if ($match) {
			print "MATCH\n";
			if ($boundary != 1 ) {
				print "boundary $boundary wrong at $newid\n"
			}
			if ($admin_level != 1) {
				print "admin_level $admin_level wrong at $newid\n"
			}
			$wayid=$newid;
			$count++;
		}
	}
	if ($count == 0) {
		error "No connection" 
	} else { 
		if ($count > 1) {
			error "Multiple connections ($count)" 
		} else {
			$path="$path $wayid";
		}
	}
} while ($wayid != $first_wayid);
print "End reached\n";
print "Path $path\n";
while (($key,$value)=each(%neighbors)) {
	print "Neighbor $key $value\n";
}
#print Dumper($ways);