Project:
OpenStreetMap
Code Location:
http://svn.openstreetmap.org/applications/utils/relationbuilder//applications/utils/relationbuilder
fetcher.pl
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#! /usr/bin/perl -w use strict; use LWP::UserAgent; use LWP::Debug qw (+); use XML::Parser; use open "utf8"; # # # my $URLBASE="http://www.informationfreeway.org/api/0.5"; my $last_id = -1; my $place_name = "unknown"; my $place_type = "unknown"; my $place_is_in = "unknown"; my $node_line = ""; my @tags = (); # # # sub fetch_place_nodes { my ($datafile, $min_lat, $max_lat, $min_lon, $max_lon) = @_; my $URL=$URLBASE . "/node[place=*][bbox=$min_lon,$min_lat,$max_lon,$max_lat]"; print $URL . "\n"; # Primitive caching... Just so I can continue working without network access... if (! -r $datafile) { my $ua = LWP::UserAgent->new(keep_alive=>1); $ua->env_proxy(); $ua->agent("relationbuilder/0.1"); my $request = HTTP::Request->new(GET => $URL); my $response = $ua->request($request); if ($response->is_success) { open OUT, "> $datafile"; print OUT $response->content . "\n"; close (OUT); print $response->status_line . "\n"; } else { print $response->status_line . "\n"; } } } # # # sub create_is_in_hierarchy { my ($datafile) = @_; my $parser = new XML::Parser (Handlers => { Start => \&is_in_start_handler, End => \&is_in_end_handler } ); $parser->parsefile($datafile); # , ProtocolEncoding => 'UTF-16'); } sub is_in_start_handler { my $expat = shift; my $tag = shift; if ($tag eq "node") { for my $i ( 0 ... $#_) { # print STDERR " ----- $i : '" . $_[$i] . "' -----\n"; my $key = $_[$i]; if (($i % 2) == 0) { $node_line .= " " . $key . "="; if ($key eq "id") { $last_id = $_[$i + 1]; } } else { $node_line .= "'" . $key . "'"; } } } elsif ($tag eq "tag") { my $key = $_[1]; my $val = $_[3]; # print STDERR " --- key = '" . $key . "', val = '" . $val . "'\n"; if ($key eq "place") { $place_type = $val; } elsif ($key eq "name") { $place_name = $val; } elsif ($key eq "is_in") { $place_is_in = $val; } push (@tags, " <tag k='" . $key . "' v='" . $val . "'/>"); } else { print STDERR "START - @{$expat->{Context}} \\\\ '$tag' - (@_)\n"; } } sub is_in_end_handler { my $expat = shift; my $tag = shift; if ($tag eq "node") { utf8::downgrade($place_type); utf8::downgrade($place_name, 1); utf8::downgrade($place_is_in, 1); chomp($place_type); mkdir ("data", "0755"); mkdir ("data/" . $place_type, "0755"); my $filename = "data/" . $place_type . "/place-" . $place_type . "-" . $place_is_in . "-" . $place_name . "-" . $last_id . ".osm"; # my $filename = "data/" . "place-" . $place_type . "-" . $place_name . "-" . $last_id . ".osm"; open OUT, "> $filename" || die ("Can't open $filename to write: $!\n"); binmode(OUT, ":utf8"); print OUT "<?xml version='1.0' encoding='UTF-8'?>\n"; print OUT "<osm version='0.5' generator='relationbuilder'>\n"; print OUT " <node " . $node_line . ">\n"; foreach my $line (@tags) { print OUT $line . "\n"; } # print OUT " <tag k='is_in' v='Asia, Turkey' />\n"; # print OUT " <tag k='place' v='cityX' />\n"; # print OUT " <tag k='name' v='Erzurum' />\n"; print OUT " </node>\n"; print OUT "</osm>\n"; close (OUT); $last_id = -1; $node_line = ""; $place_name = "unknown"; $place_type = "unknown"; $place_is_in = "unknown"; @tags = (); } elsif ($tag eq "tag") { } else { print STDERR "END - @{$expat->{Context}} // $tag\n"; } } # Turkey fetch_place_nodes("turkey-places.xml", "35.8", "42.5", "26.0", "45.0"); create_is_in_hierarchy("turkey-places.xml"); # Cyprus
