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