This file is indexed.

/usr/bin/downloadosmtiles is in libgeo-osm-tiles-perl 0.04-5.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
#!/usr/bin/perl

use 5.006001;
use strict;
use warnings;
use Geo::OSM::Tiles qw( :all );
use LWP::UserAgent;
use YAML qw( DumpFile LoadFile );
use File::Path;
use File::Basename;
use File::Spec;
use Cwd qw(cwd);
use Getopt::Long;

our $linkrgoffs = 350.0;

our $usage = qq{Usage: 
   $0 --latitude=d[:d] --longitude=d[:d] --zoom=z[:z] [--quiet] [--baseurl=url] [--destdir=dir] [--dumptilelist=filename]
   $0 --link=url [--latitude=d[:d]] [--longitude=d[:d]] [--zoom=z[:z]] [--quiet] [--baseurl=url] [--destdir=dir] [--dumptilelist=filename]
   $0 --loadtilelist=filename
};

our %opt = (
    latitude => undef,
    longitude => undef,
    zoom => undef,
    link => undef,
    quiet => undef,
    baseurl => "http://tile.openstreetmap.org",
    destdir => cwd,
    dumptilelist => undef,
    loadtilelist => undef,
);

die "$usage\n"
    unless GetOptions(\%opt,
		      "latitude=s", "longitude=s", "zoom=s", "link=s",
		      "quiet", "baseurl=s", "destdir=s", 
		      "dumptilelist=s", "loadtilelist=s") &&
	   @ARGV == 0;

sub selecttilescmdline;
sub downloadtiles;
sub parserealopt;
sub parseintopt;
sub downloadtile;


# List of tiles scheduled for download.
# Format:
# $tilelist = {
#     zoomlevel => [
# 	{
# 	    xyz => [ tx, ty, tz ],
# 	},
# 	{
# 	    xyz => [ tx, ty, tz ],
# 	},
# 	...
#     ],
#     zoomlevel => [
# 	{
# 	    xyz => [ tx, ty, tz ],
# 	},
# 	{
# 	    xyz => [ tx, ty, tz ],
# 	},
# 	...
#     ],
#     ...
# };
our $tilelist;

# In the current version we keep things simple, the tiles have to be
# selected either by the command line options --link, --lat, --lon,
# and --zoom or read from the file as given by --loadtilelist.  In a
# future version, it might be possible to combine both ways.
if ($opt{loadtilelist}) {
    $tilelist = LoadFile($opt{loadtilelist});
}
else {
    $tilelist = selecttilescmdline;
}

our $baseurl = $opt{baseurl};
our $destdir = $opt{destdir};

if ($opt{dumptilelist}) {
    DumpFile($opt{dumptilelist}, $tilelist);
}
else {
    downloadtiles($tilelist);
}



# Select tiles from command line options
sub selecttilescmdline
{
    if ($opt{link}) {
	# rewrite new style OSM links to old pattern
	if ($opt{link} =~ /^http:\/\/(.+)\/#map=(\d+)\/(-?[\d.]+)\/(-?[\d.]+)/) {
	    $opt{link} = "http://$1/?lat=$3&lon=$4&zoom=$2&layers=M";
        }
	die "Invalid link: $opt{link}\n"
	    unless $opt{link} =~ /^http:\/\/.*\/\?lat=(-?\d+(?:\.\d+)?)\&lon=(-?\d+(?:\.\d+)?)\&zoom=(\d+)/;
	my $lat = $1;
	my $lon = $2;
	my $zoom = $3;
	my $offs = $linkrgoffs / 2**$zoom;

	# Note that ($lat - $offs, $lat + $offs) or
	# ($lon - $offs, $lon + $offs) may get out of the acceptable
	# range of coordinates.  This will eventually get corrected by
	# checklatrange or checklonrange later on.

	$opt{latitude} = [ $lat - $offs, $lat + $offs ]
	    unless defined($opt{latitude});
	$opt{longitude} = [ $lon - $offs, $lon + $offs ]
	    unless defined($opt{longitude});
	$opt{zoom} = $zoom
	    unless defined($opt{zoom});
    }

    my ($latmin, $latmax) = checklatrange(parserealopt("latitude"));
    my ($lonmin, $lonmax) = checklonrange(parserealopt("longitude"));
    my ($zoommin, $zoommax) = parseintopt("zoom");

    my $tl = {};

    for my $zoom ($zoommin..$zoommax) {

	my $txmin = lon2tilex($lonmin, $zoom);
	my $txmax = lon2tilex($lonmax, $zoom);
	# Note that y=0 is near lat=+85.0511 and y=max is near
	# lat=-85.0511, so lat2tiley is monotonically decreasing.
	my $tymin = lat2tiley($latmax, $zoom);
	my $tymax = lat2tiley($latmin, $zoom);

	my $ntx = $txmax - $txmin + 1;
	my $nty = $tymax - $tymin + 1;
	printf "Schedule %d (%d x %d) tiles for zoom level %d for download ...\n",
	       $ntx*$nty, $ntx, $nty, $zoom
	    unless $opt{quiet};
	$tl->{$zoom} = [];    

	for my $tx ($txmin..$txmax) {
	    for my $ty ($tymin..$tymax) {
		push @{$tl->{$zoom}}, 
		    { xyz => [ $tx, $ty, $zoom ] };
	    }
	}
    }

    return $tl;
}


sub downloadtiles
{
    my $tiles = shift;

    my $lwpua = LWP::UserAgent->new;
    $lwpua->env_proxy;

    for my $zoom (sort {$a <=> $b} keys %$tiles) {

	printf "Download %d tiles for zoom level %d ...\n",
	       scalar(@{$tiles->{$zoom}}), $zoom
	    unless $opt{quiet};

	for my $t (@{$tiles->{$zoom}}) {
	    downloadtile($lwpua, @{$t->{xyz}});
	}
    }
}


sub parserealopt
{
    my ($optname) = @_;

    die "$optname must be specified in the command line.\n"
	unless defined($opt{$optname});

    if (ref($opt{$optname})) {
	return @{$opt{$optname}};
    }
    else {
	die "Invalid $optname: $opt{$optname}\n"
	    unless $opt{$optname} =~ /^(-?\d+(?:\.\d+)?)(?::(-?\d+(?:\.\d+)?))?$/;
	my ($min, $max) = ($1, $2);
	$max = $min unless defined($max);

	return ($min, $max);
    }
}


sub parseintopt
{
    my ($optname) = @_;

    die "$optname must be specified in the command line.\n"
	unless defined($opt{$optname});

    if (ref($opt{$optname})) {
	return @{$opt{$optname}};
    }
    else {
	die "Invalid $optname: $opt{$optname}\n"
	    unless $opt{$optname} =~ /^(\d+)(?::(\d+))?$/;
	my ($min, $max) = ($1, $2);
	$max = $min unless defined($max);

	return ($min, $max);
    }
}


sub downloadtile
{
    my ($lwpua, $tilex, $tiley, $zoom) = @_;
    my $path = tile2path($tilex, $tiley, $zoom);
    my $url = "$baseurl/$path";
    my @pathcomp = split /\//, $path;
    my $fname = File::Spec->catfile($destdir, @pathcomp);

    mkpath(dirname($fname));
    my $res = $lwpua->get($url, ':content_file' => $fname);
    die $res->status_line
	unless $res->is_success;
}


__END__
=head1 NAME

downloadosmtiles.pl - Download map tiles from OpenStreetMap

=head1 SYNOPSIS

  downloadosmtiles.pl --lat=49.5611:49.6282 --lon=10.951:11.0574 --zoom=13:14
  downloadosmtiles.pl --link='http://www.openstreetmap.org/?lat=-23.5872&lon=-46.6508&zoom=12&layers=B000FTF'
  downloadosmtiles.pl --loadtilelist=filename

=head1 DESCRIPTION

This script downloads all map tiles from an OpenStreetMap tile server
for some geographic region in a range of zoom levels.  The PNG images
of the tiles are stored in a directory tree that mirrors the paths
from the server.

A bounding box of geographic coordinates and a range of zoom levels
must be selected by command line options.

=head1 COMMAND LINE OPTIONS

Command line options may be abbreviated as long as they remain
unambiguous.

At least either C<--latitude>, C<--longitude>, and C<--zoom> or
C<--link> must be specified.

=head2 C<--latitude=latmin[:latmax]>

Selects the latitude of the bounding box of coordinates to download.
May be one single real value or two real values separated by a colon
in the range C<-85.0511..85.0511>.  If given only one value, just the
tile (or row of tiles) at this latitude will be downloaded.

Default: none

=head2 C<--longitude=lonmin[:lonmax]>

Selects the longitude of the bounding box of coordinates to download.
May be one single real value or two real values separated by a colon
in the range C<-180.0..180.0>.  If given only one value, just the tile
(or column of tiles) at this longitude will be downloaded.

Default: none

=head2 C<--zoom=zoommin[:zoommax]>

Selects the range of zoom levels to download the map tiles for.  May
be one single integer value or two integer values separated by a
colon.  OpenStreetMap supports zoom levels in the range C<0..18>.
(This depends on the base URL and is not enforced by this script.)

Note that the number of tiles to download grows by a factor of up to
four with each zoom level.

Default: none

=head2 C<--link=url>

An URL selecting C<--latitude>, C<--longitude>, and C<--zoom> in one
argument.  The idea is to select the current view of OSM's slippy map
by its permalink.

The argument to C<--link> must be an URL containing the HTTP options
C<?lat=s&lon=s&zoom=s>.  (Actually, the base URL will be ignored.)
The script chooses a box around the latitude and longitude options.
The size of the box depends on the zoom option.

If combined with C<--latitude>, C<--longitude>, or C<--zoom>, these
explicitly specified values override the implicitly specified values
from C<--link>.

Default: none

=head2 C<--baseurl=url>

The base URL of the server to download the tiles from.

Default: L<http://tile.openstreetmap.org>
(This is the base URL for the Mapnik tiles.)

=head2 C<--destdir=dir>

The directory where the tiles will be stored.  The PNG files will be
stored as C<dir/zoom/x/y.png>.

Default: The current working directory.

=head2 C<--quiet>

Do not write any diagnostic messages.  Only fatal errors will be
reported.

=head2 C<--dumptilelist=filename>

Do not download any tiles at all, but write a list of tiles as
selected by other command line options to the file named C<filename>.
See L</TILE LISTS> below.

=head2 C<--loadtilelist=filename>

Read a list of tiles to download from the file C<filename>.  See
L</TILE LISTS> below.

=head1 EXAMPLE

Select the region of interest in OSM's slippy map and follow the
permalink in the lower left of the window.  Lets assume this permalink
to be
L<http://www.openstreetmap.org/?lat=49.5782&lon=11.0076&zoom=12&layers=B000FTF>.
Then

  downloadosmtiles.pl --link='http://www.openstreetmap.org/?lat=49.5782&lon=11.0076&zoom=12&layers=B000FTF' --zoom=5:18

will download all tiles from zoom level 5 to 18 for this region.

=head1 TILE LISTS

A list of tiles may be stored to and retrieved from external files
using the C<--dumptilelist> and C<--loadtilelist> command line
options.  A set of tiles may be selected using the command line
options C<--latitude>, C<--longitude>, C<--zoom>, and C<--link> and
written to a file specified with C<--dumptilelist>.  This list may be
read at a later date using the C<--loadtilelist> option.

This may be useful to postpone the download of the tiles, to edit the
list of tiles, or to use some external tool to generate this list.

The tile lists are read and written in L<YAML> format.  Please note
that this is an experimental feature in the current version.  The file
format is not considered stable yet.  There is no guarantee that a
list of tiles generated by one version of this script may be read in
by a future version.

=head1 ENVIRONMENT

=over

=item http_proxy

=item ftp_proxy

=item xxx_proxy

=item no_proxy

These environment variables can be set to enable communication through
a proxy server.  This is implemented by L<LWP::UserAgent>.

=back

=head1 BUGS

=over

=item *

Ranges in the command line options must always be increasing.  While
this is considered a feature for C<--latitude> and C<--zoom>, it means
that it is impossible for a range in the C<--longitude> argument to
cross the 180 degree line.  A command line option like
C<--longitude=179.5:-179.5> will not work as one should expect.

=item *

The bounding box selected by the C<--link> command line option does
not always correspond to the current view in the slippy map.  The
problem is that the permalink from the slippy map only contains one
position and not the bounds of the current view.  The actual view of
the slippy map depends on many factors, including the size of the
browser window.  Thus, there is not much that can be done about this
issue.

=back

=head1 SEE ALSO

L<http://wiki.openstreetmap.org/wiki/Slippy_Map>

=head1 AUTHOR

Rolf Krahl E<lt>rotkraut@cpan.orgE<gt>

=head1 COPYRIGHT AND LICENCE

Copyright (C) 2008-2010 by Rolf Krahl

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.8.8 or,
at your option, any later version of Perl 5 you may have available.

=cut