script/downloadosmtiles.pl

filter: Statementstmt 97.2% 106 / 109 Branchbran 65.7% 25 / 38 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 16 / 16 Podpod n/a totaltotal 88.1% 148 / 168 CC 22 SCAR 16.2
CC
22
Cov
88%
selecttilescmdline
10.2
parseintopt
5.6
parserealopt
5.0
CRAP 5.0
↑ Summary lib/Geo/OSM/Tiles.pm »
1 #!/usr/local/bin/perl
2  
3 6 ▶use 5.006001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use Geo::OSM::Tiles qw( :all );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use YAML qw( DumpFile LoadFile );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use Cwd qw(cwd);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 6 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 6 our $linkrgoffs = 350.0;
16  
17 6 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 6 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 6 ▶die "$usage\n"
Branch: 1 branch
BranchTrueFalseTotal
unless &GetOptions(\%main::opt, "latitude=s", "longitude=s", "zoom=s", "link=s", "quiet", "baseurl=s", "destdir=s", "dumptilelist=s", "loadtilelist=s") and @ARGV == 0 0 6 6
Condition33%1/3
&GetOptions(\%main::opt, "latitude=s", "longitude=s", "zoom=s", "link=s", "quiet", "baseurl=s", "destdir=s", "dumptilelist=s", "loadtilelist=s") and @ARGV == 0
!ll&&!rl&&r
006
MC/DC0%0/2
&GetOptions(\%main::opt, "latitude=s", "longitude=s", "zoom=s", "link=s", "quiet", "baseurl=s", "destdir=s", "dumptilelist=s", "loadtilelist=s") and @ARGV == 0
&GetOptions(\%main::opt, "latitude=s", "longitude=s", "zoom=s", "link=s", "quiet", "baseurl=s", "destdir=s", "dumptilelist=s", "loadtilelist=s") @ARGV == 0
Truth table33%1/3
A and B
A= &GetOptions(\%main::opt, "latitude=s", "longitude=s", "zoom=s", "link=s", "quiet", "baseurl=s", "destdir=s", "dumptilelist=s", "loadtilelist=s")B= @ARGV == 0
ABresult
0X0
100
111
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 6 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 6 ▶if ($opt{loadtilelist}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($main::opt{'loadtilelist'}) { } 1 5 6
79 1 $tilelist = LoadFile($opt{loadtilelist});
80 }
81 else {
82 5 $tilelist = selecttilescmdline;
83 }
84  
85 6 our $baseurl = $opt{baseurl};
86 6 our $destdir = $opt{destdir};
87  
88 6 ▶if ($opt{dumptilelist}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($main::opt{'dumptilelist'}) { } 1 5 6
89 1 DumpFile($opt{dumptilelist}, $tilelist);
90 }
91 else {
92 5 downloadtiles($tilelist);
93 }
94  
95  
96  
97 # Select tiles from command line options
98 sub selecttilescmdline
99 5 ▶{
Subroutine selecttilescmdline: called CC=10 SCAR=23.2
100 5 ▶ if ($opt{link}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($main::opt{"link"}) 1 4 5
101 die "Invalid link: $opt{link}\n"
102 1 ▶ unless $opt{link} =~ /^http:\/\/.*\/\?lat=(-?\d+(?:\.\d+)?)\&lon=(-?\d+(?:\.\d+)?)\&zoom=(\d+)/;
Branch: 1 branch
BranchTrueFalseTotal
unless $main::opt{"link"} =~ m[^http://.*/\?lat=(-?\d+(?:\.\d+)?)\&lon=(-?\d+(?:\.\d+)?)\&zoom=(\d+)] 0 1 1
103 1 my $lat = $1;
104 1 my $lon = $2;
105 1 my $zoom = $3;
106 1 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 1 ▶ unless defined($opt{latitude});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $main::opt{"latitude"} 1 0 1
115 $opt{longitude} = [ $lon - $offs, $lon + $offs ]
116 1 ▶ unless defined($opt{longitude});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $main::opt{"longitude"} 1 0 1
117 $opt{zoom} = $zoom
118 1 ▶ unless defined($opt{zoom});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $main::opt{"zoom"} 0 1 1
119 }
120  
121 5 my ($latmin, $latmax) = checklatrange(parserealopt("latitude"));
122 5 my ($lonmin, $lonmax) = checklonrange(parserealopt("longitude"));
123 5 my ($zoommin, $zoommax) = parseintopt("zoom");
124  
125 5 my $tl = {};
126  
127 5 for my $zoom ($zoommin..$zoommax) {
128  
129 8 my $txmin = lon2tilex($lonmin, $zoom);
130 8 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 8 my $tymin = lat2tiley($latmax, $zoom);
134 8 my $tymax = lat2tiley($latmin, $zoom);
135  
136 8 my $ntx = $txmax - $txmin + 1;
137 8 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 8 ▶ unless $opt{quiet};
Branch: 1 branch
BranchTrueFalseTotal
unless $main::opt{"quiet"} 0 8 8
141 8 $tl->{$zoom} = [];
142  
143 8 for my $tx ($txmin..$txmax) {
144 22 for my $ty ($tymin..$tymax) {
145 114 push @{$tl->{$zoom}},
146 { xyz => [ $tx, $ty, $zoom ] };
147 }
148 }
149 }
150  
151 5 return $tl;
152 }
153  
154  
155 sub downloadtiles
156 5 ▶{
Subroutine downloadtiles: called CC=4 SCAR=14.0
157 5 my $tiles = shift;
158  
159 5 my $lwpua = LWP::UserAgent->new;
160 5 $lwpua->env_proxy;
161  
162 5 for my $zoom (sort {$a <=> $b} keys %$tiles) {
163  
164 printf "Download %d tiles for zoom level %d ...\n",
165 0 scalar(@{$tiles->{$zoom}}), $zoom
166 8 ▶ unless $opt{quiet};
Branch: 1 branch
BranchTrueFalseTotal
unless $main::opt{"quiet"} 0 8 8
167  
168 8 for my $t (@{$tiles->{$zoom}}) {
169 114 downloadtile($lwpua, @{$t->{xyz}});
170 }
171 }
172 }
173  
174  
175 sub parserealopt
176 10 ▶{
Subroutine parserealopt: called CC=5 SCAR=16.2
177 10 my ($optname) = @_;
178  
179 die "$optname must be specified in the command line.\n"
180 10 ▶ unless defined($opt{$optname});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $main::opt{$optname} 0 10 10
181  
182 10 ▶ if (ref($opt{$optname})) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $main::opt{$optname}) { } 2 8 10
183 2 return @{$opt{$optname}};
184 }
185 else {
186 die "Invalid $optname: $opt{$optname}\n"
187 8 ▶ unless $opt{$optname} =~ /^(-?\d+(?:\.\d+)?)(?::(-?\d+(?:\.\d+)?))?$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $main::opt{$optname} =~ /^(-?\d+(?:\.\d+)?)(?::(-?\d+(?:\.\d+)?))?$/ 0 8 8
188 8 my ($min, $max) = ($1, $2);
189 8 ▶ $max = $min unless defined($max);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $max 6 2 8
190  
191 8 return ($min, $max);
192 }
193 }
194  
195  
196 sub parseintopt
197 5 ▶{
Subroutine parseintopt: called CC=5 SCAR=17.3
198 5 my ($optname) = @_;
199  
200 die "$optname must be specified in the command line.\n"
201 5 ▶ unless defined($opt{$optname});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $main::opt{$optname} 0 5 5
202  
203 5 ▶ if (ref($opt{$optname})) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $main::opt{$optname}) { } 0 5 5
204 0 return @{$opt{$optname}};
205 }
206 else {
207 die "Invalid $optname: $opt{$optname}\n"
208 5 ▶ unless $opt{$optname} =~ /^(\d+)(?::(\d+))?$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $main::opt{$optname} =~ /^(\d+)(?::(\d+))?$/ 0 5 5
209 5 my ($min, $max) = ($1, $2);
210 5 ▶ $max = $min unless defined($max);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $max 3 2 5
211  
212 5 return ($min, $max);
213 }
214 }
215  
216  
217 sub downloadtile
218 114 ▶{
Subroutine downloadtile: called CC=2 SCAR=7.0
219 114 my ($lwpua, $tilex, $tiley, $zoom) = @_;
220 114 my $path = tile2path($tilex, $tiley, $zoom);
221 114 my $url = "$baseurl/$path";
222 114 my @pathcomp = split /\//, $path;
223 114 my $fname = File::Spec->catfile($destdir, @pathcomp);
224  
225 114 mkpath(dirname($fname));
226 114 my $res = $lwpua->get($url, ':content_file' => $fname);
227 114 ▶ die $res->status_line
Branch: 1 branch
BranchTrueFalseTotal
unless $res->is_success 0 114 114
228 unless $res->is_success;
229 }
230  
231  
232 __END__
↑ Summary lib/Geo/OSM/Tiles.pm »