Debian Codegraph @

downloadosmtiles.pl

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 #! perl
2
3 use 5.006001scip-perl cpan Geo-OSM-Tiles 0.04 `5.006001`/;
4 use strictscip-perl cpan Geo-OSM-Tiles 0.04 strict/;
5 use warningsscip-perl cpan Geo-OSM-Tiles 0.04 warnings/;
6 use Geo::OSM::Tilesscip-perl cpan Geo-OSM-Tiles 0.04 Geo/OSM/Tiles/ qw( :all );
7 use LWP::UserAgentscip-perl cpan Geo-OSM-Tiles 0.04 LWP/UserAgent/;
8 use YAMLscip-perl cpan Geo-OSM-Tiles 0.04 YAML/ qw( DumpFile LoadFile );
9 use File::Pathscip-perl cpan Geo-OSM-Tiles 0.04 File/Path/;
10 use File::Basenamescip-perl cpan Geo-OSM-Tiles 0.04 File/Basename/;
11 use File::Specscip-perl cpan Geo-OSM-Tiles 0.04 File/Spec/;
12 use Cwdscip-perl cpan Geo-OSM-Tiles 0.04 Cwd/ qw(cwd);
13 use Getopt::Longscip-perl cpan Geo-OSM-Tiles 0.04 Getopt/Long/;
14
15 our $linkrgoffsscip-perl cpan Geo-OSM-Tiles 0.04 main/$linkrgoffs. = 350.0;
16
17 our $usagescip-perl cpan Geo-OSM-Tiles 0.04 main/$usage. = qq{Usage:
18 $0 --latitude=d[:d] --longitude=d[:d] --zoom=z[:z] [--quiet] [--baseurl=url] [--destdir=dir] [--dumptilelist=filename]
19 $0 --link=url [--latitude=d[:d]] [--longitude=d[:d]] [--zoom=z[:z]] [--quiet] [--baseurl=url] [--destdir=dir] [--dumptilelist=filename]
20 $0 --loadtilelist=filename
21 };
22
23 our %optscip-perl cpan Geo-OSM-Tiles 0.04 main/`%opt`. = (
24 latitude => undef,
25 longitude => undef,
26 zoom => undef,
27 link => undef,
28 quiet => undef,
29 baseurl => "http://tile.openstreetmap.org",
30 destdir => cwd,
31 dumptilelist => undef,
32 loadtilelist => undef,
33 );
34
35 diescip-perl cpan Geo-OSM-Tiles 0.04 main/die(). "$usage\n"
36 unless GetOptionsscip-perl cpan Geo-OSM-Tiles 0.04 main/GetOptions().(\%optscip-perl cpan Geo-OSM-Tiles 0.04 main/`%opt`.,
37 "latitude=s", "longitude=s", "zoom=s", "link=s",
38 "quiet", "baseurl=s", "destdir=s",
39 "dumptilelist=s", "loadtilelist=s") &&
40 @ARGVscip-perl cpan Geo-OSM-Tiles 0.04 main/`@ARGV`. == 0;
41
42 sub selecttilescmdlinescip-perl cpan Geo-OSM-Tiles 0.04 main/selecttilescmdline().;
43 sub downloadtilesscip-perl cpan Geo-OSM-Tiles 0.04 main/downloadtiles().;
44 sub parserealoptscip-perl cpan Geo-OSM-Tiles 0.04 main/parserealopt().;
45 sub parseintoptscip-perl cpan Geo-OSM-Tiles 0.04 main/parseintopt().;
46 sub downloadtilescip-perl cpan Geo-OSM-Tiles 0.04 main/downloadtile().;
47
48
49 # List of tiles scheduled for download.
50 # Format:
51 # $tilelist = {
52 # zoomlevel => [
53 # {
54 # xyz => [ tx, ty, tz ],
55 # },
56 # {
57 # xyz => [ tx, ty, tz ],
58 # },
59 # ...
60 # ],
61 # zoomlevel => [
62 # {
63 # xyz => [ tx, ty, tz ],
64 # },
65 # {
66 # xyz => [ tx, ty, tz ],
67 # },
68 # ...
69 # ],
70 # ...
71 # };
72 our $tilelistscip-perl cpan Geo-OSM-Tiles 0.04 main/$tilelist.;
73
74 # In the current version we keep things simple, the tiles have to be
75 # selected either by the command line options --link, --lat, --lon,
76 # and --zoom or read from the file as given by --loadtilelist. In a
77 # future version, it might be possible to combine both ways.
78 if ($optscip-perl cpan Geo-OSM-Tiles 0.04 main/`%opt`.{loadtilelist}) {
79 $tilelistscip-perl cpan Geo-OSM-Tiles 0.04 main/$tilelist. = LoadFilescip-perl cpan Geo-OSM-Tiles 0.04 main/LoadFile().($optscip-perl cpan Geo-OSM-Tiles 0.04 main/`%opt`.{loadtilelist});
80 }
81 else {
82 $tilelistscip-perl cpan Geo-OSM-Tiles 0.04 main/$tilelist. = selecttilescmdline;
83 }
84
85 our $baseurlscip-perl cpan Geo-OSM-Tiles 0.04 main/$baseurl. = $optscip-perl cpan Geo-OSM-Tiles 0.04 main/`%opt`.{baseurl};
86 our $destdirscip-perl cpan Geo-OSM-Tiles 0.04 main/$destdir. = $optscip-perl cpan Geo-OSM-Tiles 0.04 main/`%opt`.{destdir};
87
88 if ($optscip-perl cpan Geo-OSM-Tiles 0.04 main/`%opt`.{dumptilelist}) {
89 DumpFilescip-perl cpan Geo-OSM-Tiles 0.04 main/DumpFile().($optscip-perl cpan Geo-OSM-Tiles 0.04 main/`%opt`.{dumptilelist}, $tilelistscip-perl cpan Geo-OSM-Tiles 0.04 main/$tilelist.);
90 }
91 else {
92 downloadtilesscip-perl cpan Geo-OSM-Tiles 0.04 main/downloadtiles().($tilelistscip-perl cpan Geo-OSM-Tiles 0.04 main/$tilelist.);
93 }
94
95
96
97 # Select tiles from command line options
98 sub selecttilescmdlinescip-perl cpan Geo-OSM-Tiles 0.04 main/selecttilescmdline().
99 {
100 if ($optscip-perl cpan Geo-OSM-Tiles 0.04 main/`%opt`.{linkscip-perl cpan Geo-OSM-Tiles 0.04 main/link().}) {
•101 # rewrite new style OSM links to old pattern
102 if ($optscip-perl cpan Geo-OSM-Tiles 0.04 main/`%opt`.{linkscip-perl cpan Geo-OSM-Tiles 0.04 main/link().} =~ /^http:\/\/(.+)\/#map=(\d+)\/(-?[\d.]+)\/(-?[\d.]+)/) {
103 $optscip-perl cpan Geo-OSM-Tiles 0.04 main/`%opt`.{linkscip-perl cpan Geo-OSM-Tiles 0.04 main/link().} = "http://$1/?lat=$3&lon=$4&zoom=$2&layers=M";
104 }
105 diescip-perl cpan Geo-OSM-Tiles 0.04 main/die(). "Invalid link: $opt{link}\n"
106 unless $optscip-perl cpan Geo-OSM-Tiles 0.04 main/`%opt`.{linkscip-perl cpan Geo-OSM-Tiles 0.04 main/link().} =~ /^http:\/\/.*\/\?lat=(-?\d+(?:\.\d+)?)\&lon=(-?\d+(?:\.\d+)?)\&zoom=(\d+)/;
107 my $lat = $1scip-perl cpan Geo-OSM-Tiles 0.04 main/$1.;
108 my $lon = $2scip-perl cpan Geo-OSM-Tiles 0.04 main/$2.;
109 my $zoom = $3scip-perl cpan Geo-OSM-Tiles 0.04 main/$3.;
110 my $offs = $linkrgoffsscip-perl cpan Geo-OSM-Tiles 0.04 main/$linkrgoffs. / 2**$zoom;
111
112 # Note that ($lat - $offs, $lat + $offs) or
113 # ($lon - $offs, $lon + $offs) may get out of the acceptable
114 # range of coordinates. This will eventually get corrected by
115 # checklatrange or checklonrange later on.
116
117 $optscip-perl cpan Geo-OSM-Tiles 0.04 main/`%opt`.{latitude} = [ $lat - $offs, $lat + $offs ]
118 unless definedscip-perl cpan Geo-OSM-Tiles 0.04 main/defined().($optscip-perl cpan Geo-OSM-Tiles 0.04 main/`%opt`.{latitude});
119 $optscip-perl cpan Geo-OSM-Tiles 0.04 main/`%opt`.{longitude} = [ $lon - $offs, $lon + $offs ]
120 unless definedscip-perl cpan Geo-OSM-Tiles 0.04 main/defined().($optscip-perl cpan Geo-OSM-Tiles 0.04 main/`%opt`.{longitude});
121 $optscip-perl cpan Geo-OSM-Tiles 0.04 main/`%opt`.{zoom} = $zoom
122 unless definedscip-perl cpan Geo-OSM-Tiles 0.04 main/defined().($optscip-perl cpan Geo-OSM-Tiles 0.04 main/`%opt`.{zoom});
123 }
124
125 my ($latmin, $latmax) = checklatrangescip-perl cpan Geo-OSM-Tiles 0.04 main/checklatrange().(parserealoptscip-perl cpan Geo-OSM-Tiles 0.04 main/parserealopt().("latitude"));
126 my ($lonmin, $lonmax) = checklonrangescip-perl cpan Geo-OSM-Tiles 0.04 main/checklonrange().(parserealoptscip-perl cpan Geo-OSM-Tiles 0.04 main/parserealopt().("longitude"));
127 my ($zoommin, $zoommax) = parseintoptscip-perl cpan Geo-OSM-Tiles 0.04 main/parseintopt().("zoom");
128
129 my $tl = {};
130
131 for my $zoom ($zoommin..$zoommax) {
132
133 my $txmin = lon2tilexscip-perl cpan Geo-OSM-Tiles 0.04 main/lon2tilex().($lonmin, $zoom);
134 my $txmax = lon2tilexscip-perl cpan Geo-OSM-Tiles 0.04 main/lon2tilex().($lonmax, $zoom);
135 # Note that y=0 is near lat=+85.0511 and y=max is near
136 # lat=-85.0511, so lat2tiley is monotonically decreasing.
137 my $tymin = lat2tileyscip-perl cpan Geo-OSM-Tiles 0.04 main/lat2tiley().($latmax, $zoom);
138 my $tymax = lat2tileyscip-perl cpan Geo-OSM-Tiles 0.04 main/lat2tiley().($latmin, $zoom);
139
140 my $ntx = $txmax - $txmin + 1;
141 my $nty = $tymax - $tymin + 1;
142 printfscip-perl cpan Geo-OSM-Tiles 0.04 main/printf(). "Schedule %d (%d x %d) tiles for zoom level %d for download ...\n",
143 $ntx*$nty, $ntx, $nty, $zoom
144 unless $optscip-perl cpan Geo-OSM-Tiles 0.04 main/`%opt`.{quiet};
145 $tl->{$zoom} = [];
146
147 for my $tx ($txmin..$txmax) {
148 for my $ty ($tymin..$tymax) {
149 pushscip-perl cpan Geo-OSM-Tiles 0.04 main/push(). @{$tl->{$zoom}},
150 { xyz => [ $tx, $ty, $zoom ] };
151 }
152 }
153 }
154
155 return $tl;
156 }
157
158
159 sub downloadtilesscip-perl cpan Geo-OSM-Tiles 0.04 main/downloadtiles().
160 {
161 my $tiles = shiftscip-perl cpan Geo-OSM-Tiles 0.04 main/shift().;
162
163 my $lwpua = LWP::UserAgent->newscip-perl cpan Geo-OSM-Tiles 0.04 LWP/UserAgent/new().;
164 $lwpua->env_proxyscip-perl cpan Geo-OSM-Tiles 0.04 LWP/UserAgent/env_proxy().;
165
166 for my $zoom (sortscip-perl cpan Geo-OSM-Tiles 0.04 main/sort(). {$ascip-perl cpan Geo-OSM-Tiles 0.04 main/$a. <=> $bscip-perl cpan Geo-OSM-Tiles 0.04 main/$b.} keysscip-perl cpan Geo-OSM-Tiles 0.04 main/keys(). %$tiles) {
167
168 printfscip-perl cpan Geo-OSM-Tiles 0.04 main/printf(). "Download %d tiles for zoom level %d ...\n",
169 scalarscip-perl cpan Geo-OSM-Tiles 0.04 main/scalar().(@{$tiles->{$zoom}}), $zoom
170 unless $optscip-perl cpan Geo-OSM-Tiles 0.04 main/`%opt`.{quiet};
171
172 for my $t (@{$tiles->{$zoom}}) {
173 downloadtilescip-perl cpan Geo-OSM-Tiles 0.04 main/downloadtile().($lwpua, @{$t->{xyz}});
174 }
175 }
176 }
177
178
179 sub parserealoptscip-perl cpan Geo-OSM-Tiles 0.04 main/parserealopt().
180 {
181 my ($optname) = @_scip-perl cpan Geo-OSM-Tiles 0.04 main/`@_`.;
182
183 diescip-perl cpan Geo-OSM-Tiles 0.04 main/die(). "$optname must be specified in the command line.\n"
184 unless definedscip-perl cpan Geo-OSM-Tiles 0.04 main/defined().($optscip-perl cpan Geo-OSM-Tiles 0.04 main/`%opt`.{$optname});
185
186 if (refscip-perl cpan Geo-OSM-Tiles 0.04 main/ref().($optscip-perl cpan Geo-OSM-Tiles 0.04 main/`%opt`.{$optname})) {
187 return @{$optscip-perl cpan Geo-OSM-Tiles 0.04 main/`%opt`.{$optname}};
188 }
189 else {
190 diescip-perl cpan Geo-OSM-Tiles 0.04 main/die(). "Invalid $optname: $opt{$optname}\n"
191 unless $optscip-perl cpan Geo-OSM-Tiles 0.04 main/`%opt`.{$optname} =~ /^(-?\d+(?:\.\d+)?)(?::(-?\d+(?:\.\d+)?))?$/;
192 my ($min, $max) = ($1scip-perl cpan Geo-OSM-Tiles 0.04 main/$1., $2scip-perl cpan Geo-OSM-Tiles 0.04 main/$2.);
193 $max = $min unless definedscip-perl cpan Geo-OSM-Tiles 0.04 main/defined().($max);
194
195 return ($min, $max);
196 }
197 }
198
199
200 sub parseintoptscip-perl cpan Geo-OSM-Tiles 0.04 main/parseintopt().
201 {
202 my ($optname) = @_scip-perl cpan Geo-OSM-Tiles 0.04 main/`@_`.;
203
204 diescip-perl cpan Geo-OSM-Tiles 0.04 main/die(). "$optname must be specified in the command line.\n"
205 unless definedscip-perl cpan Geo-OSM-Tiles 0.04 main/defined().($optscip-perl cpan Geo-OSM-Tiles 0.04 main/`%opt`.{$optname});
206
207 if (refscip-perl cpan Geo-OSM-Tiles 0.04 main/ref().($optscip-perl cpan Geo-OSM-Tiles 0.04 main/`%opt`.{$optname})) {
208 return @{$optscip-perl cpan Geo-OSM-Tiles 0.04 main/`%opt`.{$optname}};
209 }
210 else {
211 diescip-perl cpan Geo-OSM-Tiles 0.04 main/die(). "Invalid $optname: $opt{$optname}\n"
212 unless $optscip-perl cpan Geo-OSM-Tiles 0.04 main/`%opt`.{$optname} =~ /^(\d+)(?::(\d+))?$/;
213 my ($min, $max) = ($1scip-perl cpan Geo-OSM-Tiles 0.04 main/$1., $2scip-perl cpan Geo-OSM-Tiles 0.04 main/$2.);
214 $max = $min unless definedscip-perl cpan Geo-OSM-Tiles 0.04 main/defined().($max);
215
216 return ($min, $max);
217 }
218 }
219
220
221 sub downloadtilescip-perl cpan Geo-OSM-Tiles 0.04 main/downloadtile().
222 {
223 my ($lwpua, $tilex, $tiley, $zoom) = @_scip-perl cpan Geo-OSM-Tiles 0.04 main/`@_`.;
224 my $path = tile2pathscip-perl cpan Geo-OSM-Tiles 0.04 main/tile2path().($tilex, $tiley, $zoom);
225 my $url = "$baseurl/$path";
226 my @pathcomp = splitscip-perl cpan Geo-OSM-Tiles 0.04 main/split(). /\//, $path;
227 my $fname = File::Spec->catfilescip-perl cpan Geo-OSM-Tiles 0.04 File/Spec/catfile().($destdirscip-perl cpan Geo-OSM-Tiles 0.04 main/$destdir., @pathcomp);
228
229 mkpathscip-perl cpan Geo-OSM-Tiles 0.04 main/mkpath().(dirnamescip-perl cpan Geo-OSM-Tiles 0.04 main/dirname().($fname));
230 my $res = $lwpua->getscip-perl cpan Geo-OSM-Tiles 0.04 main/get().($url, ':content_file' => $fname);
231 diescip-perl cpan Geo-OSM-Tiles 0.04 main/die(). $res->status_linescip-perl cpan Geo-OSM-Tiles 0.04 main/status_line().
232 unless $res->is_successscip-perl cpan Geo-OSM-Tiles 0.04 main/is_success().;
233 }
234
235
236 __END__
237 =head1 NAME
238
239 downloadosmtiles.pl - Download map tiles from OpenStreetMap
240
241 =head1 SYNOPSIS
242
243 downloadosmtiles.pl --lat=49.5611:49.6282 --lon=10.951:11.0574 --zoom=13:14
244 downloadosmtiles.pl --link='http://www.openstreetmap.org/?lat=-23.5872&lon=-46.6508&zoom=12&layers=B000FTF'
245 downloadosmtiles.pl --loadtilelist=filename
246
247 =head1 DESCRIPTION
248
249 This script downloads all map tiles from an OpenStreetMap tile server
250 for some geographic region in a range of zoom levels. The PNG images
251 of the tiles are stored in a directory tree that mirrors the paths
252 from the server.
253
254 A bounding box of geographic coordinates and a range of zoom levels
255 must be selected by command line options.
256
257 =head1 COMMAND LINE OPTIONS
258
259 Command line options may be abbreviated as long as they remain
260 unambiguous.
261
262 At least either C<--latitude>, C<--longitude>, and C<--zoom> or
263 C<--link> must be specified.
264
265 =head2 C<--latitude=latmin[:latmax]>
266
267 Selects the latitude of the bounding box of coordinates to download.
268 May be one single real value or two real values separated by a colon
269 in the range C<-85.0511..85.0511>. If given only one value, just the
270 tile (or row of tiles) at this latitude will be downloaded.
271
272 Default: none
273
274 =head2 C<--longitude=lonmin[:lonmax]>
275
276 Selects the longitude of the bounding box of coordinates to download.
277 May be one single real value or two real values separated by a colon
278 in the range C<-180.0..180.0>. If given only one value, just the tile
279 (or column of tiles) at this longitude will be downloaded.
280
281 Default: none
282
283 =head2 C<--zoom=zoommin[:zoommax]>
284
285 Selects the range of zoom levels to download the map tiles for. May
286 be one single integer value or two integer values separated by a
287 colon. OpenStreetMap supports zoom levels in the range C<0..18>.
288 (This depends on the base URL and is not enforced by this script.)
289
290 Note that the number of tiles to download grows by a factor of up to
291 four with each zoom level.
292
293 Default: none
294
295 =head2 C<--link=url>
296
297 An URL selecting C<--latitude>, C<--longitude>, and C<--zoom> in one
298 argument. The idea is to select the current view of OSM's slippy map
299 by its permalink.
300
301 The argument to C<--link> must be an URL containing the HTTP options
302 C<?lat=s&lon=s&zoom=s>. (Actually, the base URL will be ignored.)
303 The script chooses a box around the latitude and longitude options.
304 The size of the box depends on the zoom option.
305
306 If combined with C<--latitude>, C<--longitude>, or C<--zoom>, these
307 explicitly specified values override the implicitly specified values
308 from C<--link>.
309
310 Default: none
311
312 =head2 C<--baseurl=url>
313
314 The base URL of the server to download the tiles from.
315
316 Default: L<http://tile.openstreetmap.org>
317 (This is the base URL for the Mapnik tiles.)
318
319 =head2 C<--destdir=dir>
320
321 The directory where the tiles will be stored. The PNG files will be
322 stored as C<dir/zoom/x/y.png>.
323
324 Default: The current working directory.
325
326 =head2 C<--quiet>
327
328 Do not write any diagnostic messages. Only fatal errors will be
329 reported.
330
331 =head2 C<--dumptilelist=filename>
332
333 Do not download any tiles at all, but write a list of tiles as
334 selected by other command line options to the file named C<filename>.
335 See L</TILE LISTS> below.
336
337 =head2 C<--loadtilelist=filename>
338
339 Read a list of tiles to download from the file C<filename>. See
340 L</TILE LISTS> below.
341
342 =head1 EXAMPLE
343
344 Select the region of interest in OSM's slippy map and follow the
345 permalink in the lower left of the window. Lets assume this permalink
346 to be
347 L<http://www.openstreetmap.org/?lat=49.5782&lon=11.0076&zoom=12&layers=B000FTF>.
348 Then
349
350 downloadosmtiles.pl --link='http://www.openstreetmap.org/?lat=49.5782&lon=11.0076&zoom=12&layers=B000FTF' --zoom=5:18
351
352 will download all tiles from zoom level 5 to 18 for this region.
353
354 =head1 TILE LISTS
355
356 A list of tiles may be stored to and retrieved from external files
357 using the C<--dumptilelist> and C<--loadtilelist> command line
358 options. A set of tiles may be selected using the command line
359 options C<--latitude>, C<--longitude>, C<--zoom>, and C<--link> and
360 written to a file specified with C<--dumptilelist>. This list may be
361 read at a later date using the C<--loadtilelist> option.
362
363 This may be useful to postpone the download of the tiles, to edit the
364 list of tiles, or to use some external tool to generate this list.
365
366 The tile lists are read and written in L<YAML> format. Please note
367 that this is an experimental feature in the current version. The file
368 format is not considered stable yet. There is no guarantee that a
369 list of tiles generated by one version of this script may be read in
370 by a future version.
371
372 =head1 ENVIRONMENT
373
374 =over
375
376 =item http_proxy
377
378 =item ftp_proxy
379
380 =item xxx_proxy
381
382 =item no_proxy
383
384 These environment variables can be set to enable communication through
385 a proxy server. This is implemented by L<LWP::UserAgent>.
386
387 =back
388
389 =head1 BUGS
390
391 =over
392
393 =item *
394
395 Ranges in the command line options must always be increasing. While
396 this is considered a feature for C<--latitude> and C<--zoom>, it means
397 that it is impossible for a range in the C<--longitude> argument to
398 cross the 180 degree line. A command line option like
399 C<--longitude=179.5:-179.5> will not work as one should expect.
400
401 =item *
402
403 The bounding box selected by the C<--link> command line option does
404 not always correspond to the current view in the slippy map. The
405 problem is that the permalink from the slippy map only contains one
406 position and not the bounds of the current view. The actual view of
407 the slippy map depends on many factors, including the size of the
408 browser window. Thus, there is not much that can be done about this
409 issue.
410
411 =back
412
413 =head1 SEE ALSO
414
415 L<http://wiki.openstreetmap.org/wiki/Slippy_Map>
416
417 =head1 AUTHOR
418
419 Rolf Krahl E<lt>[email protected]<gt>
420
421 =head1 COPYRIGHT AND LICENCE
422
423 Copyright (C) 2008-2010 by Rolf Krahl
424
425 This library is free software; you can redistribute it and/or modify
426 it under the same terms as Perl itself, either Perl version 5.8.8 or,
427 at your option, any later version of Perl 5 you may have available.
428
429 =cut