blib/lib/Database/BI/Model/Boundary.pm

filter: Statementstmt 14.3% 46 / 320 Branchbran 0.6% 1 / 166 Conditioncond 0.7% 2 / 257 MC/DCmcdc 0.0% 0 / 132 Subroutinesub 25.5% 11 / 43 Podpod 100% 10 / 10 totaltotal 7.5% 70 / 928 CC 186 SCAR 52.7
CC
186
Cov
7%
_outline_id
552.0
outlines
506.0
_ask
342.0
CRAP 195.1
« blib/lib/Database/BI/Controller/Dashboard.pm ↑ Summary blib/lib/Database/BI/Model/DataSource.pm »
1 package Database::BI::Model::Boundary;
2  
3 # The outline of the area each map marker is in: its country, state or
4 # province, county, city, town or neighbourhood (the level).
5 #
6 # A map can draw the boundary of the area around each of its markers. The
7 # outlines come from OpenStreetMap's Nominatim, through its reverse lookup:
8 # given a point and a zoom, it names the administrative area there and, with
9 # polygon_geojson, sends its shape. Asking by point rather than by name means
10 # the same code serves coordinate columns and geocoded places, in any country,
11 # and needs no state or country column to tell one Richmond from another.
12 #
13 # Nominatim allows one request a second, so this module asks as little as it
14 # can:
15 #
16 # 1. Each point is rounded to 3 decimal places (about 110 m), which is also
17 # all of it that leaves this machine, and remembered with the outline it
18 # fell in (or with none, for a point at sea).
19 # 2. A point not remembered is first tested against the outlines already
20 # known, through an index of their bounding boxes, so ten thousand shops
21 # in one county cost one request, not ten thousand.
22 # 3. Only then is Nominatim asked, at most online_max times a call, and a
23 # point it does not answer is left pending, not remembered as nowhere.
24 #
25 # The outlines are simplified by Nominatim (polygon_threshold), about 7 KB each,
26 # because they are written into the page.
27 #
28 # What Nominatim has at each level differs by country, and was probed in
29 # November 2026 (the points and what came back are in the comments at %LEVELS).
30 # A level is therefore a plan: a list of tries, each a reverse zoom and the
31 # place ranks an answer may have, taken in order until one gives an outline.
32 #
33 # country zoom 3, rank 4.
34 # state zoom 5, rank 8: a US or Indian state, a Canadian province, a
35 # New Zealand region, England or Scotland.
36 # county below, one rank a country.
37 # city zoom 10, ranks 12 to 16, an administrative area or a place
38 # mapped as an area: Toronto, Manchester, Canterbury (the
39 # district), Bristol. Where zoom 10 gives only a point (York,
40 # Pune), zoom 8 at rank 12: the unitary authority or city
41 # district. A point in no city gets its rank 12 area
42 # too (a US county): zoom 10 answers with it. New Zealand's cities are its territorial
43 # authorities, rank 12 at zoom 8 (Hamilton City), or the region
44 # where that is one (Auckland).
45 # town zoom 12, ranks 13 to 18: Margate, a Toronto ward. Then zoom
46 # 14, ranks 19 and 20, administrative areas only: a town
47 # that is a civil parish (Westgate-on-Sea).
48 # neighbourhood zoom 14, ranks 17 to 20: a Toronto neighbourhood, an English
49 # civil parish (Westgate-on-Sea), a suburb mapped as an area
50 # (Manchester's City Centre). Most suburbs are mapped as a
51 # point (Cliftonville, Garlinge), and those have no outline.
52 #
53 # The larger the area, the more its outline is simplified (the level's detail),
54 # or a country would be megabytes. An outline is kept under its detail, so one
55 # relation drawn at two levels (Auckland, a state and a city) is kept twice.
56 #
57 # A "county" is the division of a country below its states. Which
58 # of Nominatim's place ranks that is depends on the country, so %LEVELS gives a
59 # rank for most (12: a US county, an English county, a German Landkreis, a
60 # French departement, a Pakistani district) and the countries that differ (10:
61 # an Indian or Bangladeshi district, a Chinese prefecture, a Spanish province;
62 # 8: a Kenyan county), and config map_bounds_ranks can add more. Each reply
63 # names its country, so a point is asked at the zoom of the last country seen,
64 # and again at its own country's zoom when that guess was wrong: one extra
65 # request at each border a map crosses. The rank an outline was accepted at is
66 # kept with it, so a changed table asks again rather than draw the old level.
67 # The other levels do the same with their plans: a point is asked the first try
68 # of the last country's plan, and its own country's plan from there.
69  
70 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
71 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
72  
73 1 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
74 1 ▶use JSON::PP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
75 1 ▶use List::Util qw(first);
Subroutine BEGIN: called CC=1 SCAR=0.0
76 1 ▶use Params::Get ();
Subroutine BEGIN: called CC=1 SCAR=0.0
77 1 ▶use Params::Validate::Strict qw(validate_strict);
Subroutine BEGIN: called CC=1 SCAR=0.0
78 1 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
79 1 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
80 1 ▶use URI ();
Subroutine BEGIN: called CC=1 SCAR=0.0
81  
82 1 our $VERSION = '0.023.1';
83  
84 # The place rank an answer must have at each level: rank for a country not
85 # listed, countries for those that differ, by ISO 3166-1 alpha-2 code. Checked
86 # against Nominatim's reverse lookup in November 2026: rank 12
87 # at zoom 8 is the county level in the US, UK, Ireland, Germany, France, Italy,
88 # Pakistan, Mexico, Nigeria, South Africa, Russia, Indonesia and Canada.
89 # Australia, Brazil and Japan have nothing between their states and their
90 # towns, so a point there is nowhere.
91 #
92 # The other levels are plans: tries => [ { zoom, ranks => [low, high], places } ],
93 # with plans => { country => [ tries ] } for a country that differs. places
94 # lets a try take a place mapped as an area (place=city, town, suburb; a
95 # relation or a closed way) as well as an administrative boundary. detail is
96 # how far Nominatim may move an outline's edge to simplify it, in degrees, as
97 # text: it is stored with each outline and compared. name and names are what
98 # the area is called, as a word a caller can name it by to its readers
99 # (name_for). What the reverse lookup gave, November 2026 (zoom: rank, name):
100 #
101 # Toronto 5: 8 Ontario; 8 and 10: 12 Toronto; 12: 18 Toronto Centre
102 # (ward); 14: 20 Financial District
103 # Pune 5: 8 Maharashtra; 8: 12 Pune City Subdistrict; 10 and up:
104 # points only
105 # Canterbury 8: 12 Kent; 10 and 12: 16 Canterbury (the district)
106 # Manchester 8: 10 Greater Manchester; 10: 16 Manchester; 14: 19 City
107 # Centre (place=suburb, a way)
108 # Bristol, York 8: 12, the unitary authority; 10: 16 Bristol (place=city, a
109 # relation), York a point
110 # Hamilton NZ 5: 8 Waikato; 8, 10 and 12: 12 Hamilton City
111 # Auckland 5 and 8: 8 Auckland; 10: 16 Waitemata (a local board)
112 # Wellington 8: 12 Wellington City; 10: a point
113 # Margate 10: 16 Thanet; 12: 18 Margate (place=town, a relation); 13
114 # and 14: points (Cliftonville, Westbrook, Garlinge)
115 # Westgate-on-Sea 14: 20, the civil parish; 12: 18 Birchington, a point
116 1 Readonly my %LEVELS => (
117 country => {
118 name => 'country',
119 detail => '0.02', # Canada 15 KB, India 29 KB
120 tries => [ { zoom => 3, ranks => [ 4, 4 ] } ],
121 },
122 state => {
123 name => 'state',
124 names => {
125 ca => 'province', cn => 'province', es => 'region', fr => 'region',
126 gb => 'country', it => 'region', jp => 'prefecture', nl => 'province',
127 nz => 'region', pk => 'province', za => 'province',
128 },
129 detail => '0.01', # Ontario 12 KB, Maharashtra 30 KB, England 7 KB
130 tries => [ { zoom => 5, ranks => [ 8, 8 ] } ],
131 },
132 county => {
133 rank => 12,
134 countries => {
135 bd => 10, # district (zila); 12 is an upazila
136 cn => 10, # prefecture-level city; 12 is a district
137 es => 10, # province; Spain has no rank 12
138 in => 10, # district; 12 is a sub-district (tehsil, taluk)
139 ke => 8, # county; 12 is a sub-county
140 },
141 name => 'county',
142 names => {
143 bd => 'district', cn => 'prefecture', de => 'district', es => 'province',
144 fr => 'department', in => 'district', it => 'province',
145 },
146 # About 100 m. Each outline is simplified alone, so neighbours' shared
147 # borders differ by up to this, which shows as gaps and overlaps now
148 # that each is drawn on its own side of the border; 0.005 (500 m, 2 KB
149 # a county) was 7 pixels out at the zoom a county fills the map, this
150 # (7 KB) under 2.
151 detail => '0.001',
152 },
153 city => {
154 name => 'city',
155 names => { gb => 'district', nz => 'district' },
156 detail => '0.0005', # Thanet 4 KB
157 tries => [
158 { zoom => 10, ranks => [ 12, 16 ], places => 1 },
159 { zoom => 8, ranks => [ 12, 12 ] },
160 ],
161 plans => {
162 nz => [ { zoom => 8, ranks => [ 8, 12 ] } ],
163 },
164 },
165 town => {
166 name => 'town',
167 detail => '0.0002',
168 tries => [
169 { zoom => 12, ranks => [ 13, 18 ], places => 1 },
170 # A parished English town is its civil parish, rank 20, and zoom
171 # 12 there answers with the nearest town mapped as a point.
172 { zoom => 14, ranks => [ 19, 20 ] },
173 ],
174 },
175 neighbourhood => {
176 name => 'neighbourhood',
177 detail => '0.0002', # Westgate-on-Sea 1 KB
178 tries => [ { zoom => 14, ranks => [ 17, 20 ], places => 1 } ],
179 },
180 );
181  
182 # The levels, largest area first: the order a reader chooses from.
183 1 Readonly my @LEVEL_ORDER => qw(country state county city town neighbourhood);
184  
185 # The reverse zoom that asks for each rank. A zoom names the most detailed
186 # rank it may return, so a point with no area at that rank comes back as
187 # something larger (a state), which the rank check turns away rather than
188 # drawing.
189 1 Readonly my %ZOOM_FOR_RANK => (8 => 5, 10 => 6, 12 => 8);
190  
191 1 Readonly my $REVERSE_URL => 'https://nominatim.openstreetmap.org/reverse';
192 # The detail outlines had before levels did (the county's): an outline at it is
193 # kept under its plain key, so a cache from 0.023.0 is still read.
194 1 Readonly my $PLAIN_DETAIL => '0.001';
195 1 Readonly my $DECIMALS => 3; # of a point, in its key and in what is sent
196 1 Readonly my $DEFAULT_MAX => 20; # Nominatim requests in one call, as map_geocode_online_max
197 1 Readonly my $CACHE_DIR => 'boundary-cache'; # under model_dir, for file_cache()
198 1 Readonly my $CACHE_NAMESPACE => __PACKAGE__;
199 1 Readonly my $CACHE_MAX_SIZE => 8 * 1024 * 1024; # bytes, for the process-wide default
200  
201 1 my $json = JSON::PP->new->canonical->utf8;
202  
203 =head1 NAME
204  
205 Database::BI::Model::Boundary - The outline of the area around each map marker
206  
207 =head1 VERSION
208  
209 0.023.1
210  
211 =head1 SYNOPSIS
212  
213 use Database::BI::Model::Boundary;
214  
215 my $boundary = Database::BI::Model::Boundary->new(
216 cache => Database::BI::Model::Boundary->file_cache('.bi'),
217 online => 1,
218 level => 'state', # country, state, county (default), city, town, neighbourhood
219 );
220  
221 my $result = $boundary->outlines([ [ 39.09, -77.64 ], [ 38.85, -77.31 ] ]);
222 $map->add_geojson(
223 { type => 'FeatureCollection', features => $result->{features} },
224 name => 'County boundaries',
225 );
226  
227 # A point the map page listed as pending, looked up on its own
228 my $answer = $boundary->lookup_point($result->{pending}[0]);
229  
230 =head1 DESCRIPTION
231  
232 Finds the area each point is in, at one level, and returns its outline as a GeoJSON
233 Feature, for L<HTML::OSM>'s C<add_geojson>. The outlines come from
234 OpenStreetMap's Nominatim reverse lookup, through the same throttled user agent
235 as the geocoder (L<Database::BI::Model::Geocode/nominatim_ua>), so the two
236 together never ask more than once a second.
237  
238 Only the point is sent, rounded to three decimal places (about 110 metres).
239 Every answer is cached: a point with the outline it is in, and the outline
240 itself. A point inside an outline already known is matched here, without a
241 request, so many points in one county cost one lookup.
242  
243 The levels, largest first:
244  
245 =over 4
246  
247 =item country
248  
249 =item state
250  
251 A US or Indian state, a Canadian province, a New Zealand region, England.
252  
253 =item county
254  
255 The default; see below.
256  
257 =item city
258  
259 A city or the district named after it: Toronto, Manchester, the district of
260 Canterbury, Hamilton City in New Zealand. Where OpenStreetMap has no city round a point, or has the city only as a point
261 (York, Pune), the area at the county's rank there: the unitary authority, the
262 city district, or the county itself.
263  
264 =item town
265  
266 A town or city ward: Margate, Toronto Centre. An English town that is a civil
267 parish (Westgate-on-Sea) is that parish.
268  
269 =item neighbourhood
270  
271 A neighbourhood, suburb or civil parish that OpenStreetMap has as an area.
272 Many suburbs are mapped only as a point, and those have no outline.
273  
274 =back
275  
276 Larger areas are simplified more, so an outline is a few kilobytes at any
277 level.
278  
279 A county here is the division of a country below its states, whose place rank
280 in Nominatim depends on the country: 12 (a US county or independent city, an
281 English county, a German Landkreis, a French departement) unless the country is
282 listed otherwise: 10 for India, Bangladesh, China and Spain (district,
283 prefecture, province), 8 for Kenya. C<ranks> adds or changes countries.
284 Anything else Nominatim answers is taken as "no county here" and remembered as
285 such. A country with nothing at its rank (Australia, Brazil, Japan) gets no
286 outlines.
287  
288 Each reply names the point's country, so a point is asked at the zoom of the
289 last country seen, and asked again when its own country wants another rank.
290 An outline is kept with the rank it was accepted at; when C<ranks> changes, the
291 points it covered are asked again.
292  
293 =head1 METHODS
294  
295 =head2 new
296  
297 my $boundary = Database::BI::Model::Boundary->new(%args);
298  
299 =head3 API SPECIFICATION
300  
301 =head4 INPUT
302  
303 cache : object with get() and set() (CHI) optional; default one
304 CHI Memory cache for the process
305 online : boolean optional, default 0: may Nominatim be asked
306 online_max : integer optional, default 20: requests in one outlines() call
307 level : string optional, default 'county'; one of levels()
308 ranks : hashref optional: { country code => place rank }, over the
309 county level's own list; codes are two letters (any case),
310 ranks 8, 10 or 12. The other levels ignore it
311 ua : object with get() optional: the user agent to ask with
312 (tests); default Geocode::nominatim_ua()
313  
314 =head4 OUTPUT
315  
316 A Database::BI::Model::Boundary object. Croaks on an unknown level, a cache
317 without get and set, or ranks that are not a hash of two-letter codes to 8, 10
318 or 12.
319  
320 =cut
321  
322 0 ▶sub new {
Subroutine new: not called CC=9 SCAR=45.0
Pod documented
323 0 my $class = shift;
324 0 ▶ my $raw = Params::Get::get_params(undef, \@_) // {};
Condition0%0/2
Params::Get::get_params(undef, \@_) // {}
l!l
00
MC/DC0%0/1
Params::Get::get_params(undef, \@_) // {}
Params::Get::get_params(undef, \@_)
Truth table0%0/2
A
A= Params::Get::get_params(undef, \@_)
Aresult
11
00
325  
326 0 my $args = validate_strict(
327 schema => {
328 cache => { type => 'object', optional => 1, default => undef },
329 online => { type => 'boolean', optional => 1, default => 0 },
330 online_max => { type => 'integer', optional => 1, default => $DEFAULT_MAX, min => 0 },
331 level => { type => 'string', optional => 1, default => 'county' },
332 ua => { type => 'object', optional => 1, default => undef, can => 'get' },
333 ranks => { type => 'hashref', optional => 1, default => undef },
334 },
335 input => $raw,
336 );
337  
338 my $level = $LEVELS{ $args->{level} }
339 0 ▶ or croak "Unknown boundary level '$args->{level}'";
Branch: 1 branch
BranchTrueFalseTotal
my $level = $LEVELS{$$args{"level"}} or croak(qq[Unknown boundary level '$$args{"level"}']) 0 0 0
340 croak 'The boundary cache needs get() and set()'
341 0 ▶ if defined($args->{cache}) && !($args->{cache}->can('get') && $args->{cache}->can('set'));
Branch: 1 branch
BranchTrueFalseTotal
if defined $$args{"cache"} and not $$args{"cache"}->can("get") && $$args{"cache"}->can("set") 0 0 0
Condition0%0/6
defined $$args{"cache"} and not $$args{"cache"}->can("get") && $$args{"cache"}->can("set")
!ll&&!rl&&r
000
$$args{'cache'}->can('get') && $$args{'cache'}->can('set')
!ll&&!rl&&r
000
MC/DC0%0/3
defined $$args{"cache"} and not $$args{"cache"}->can("get") && $$args{"cache"}->can("set")
defined $$args{"cache"} $$args{'cache'}->can('get') $$args{'cache'}->can('set')
Truth table0%0/4
A and not(B && C)
A= defined $$args{"cache"}B= $$args{'cache'}->can('get')C= $$args{'cache'}->can('set')
ABCresult
0XX0
1110
10X1
1101
342  
343 return bless {
344 0 %{$args},
345 0 ▶ ranks => { %{ $level->{countries} // {} }, %{ $class->check_ranks($args->{ranks} // {}) } },
Condition0%0/7
$$level{"countries"} // {}
l!l
00
$$args{"ranks"} // {}
l!l
00
$$args{"cache"} // _default_cache()
l!l&&r!l&&!r
000
MC/DC0%0/1
$$level{"countries"} // {}
$$level{"countries"}
MC/DC0%0/1
$$args{"ranks"} // {}
$$args{"ranks"}
MC/DC0%0/2
$$args{"cache"} // _default_cache()
$$args{"cache"} _default_cache()
Truth table0%0/2
A
A= $$level{"countries"}
Aresult
11
00
Truth table0%0/2
A
A= $$args{"ranks"}
Aresult
11
00
Truth table0%0/3
A // B
A= $$args{"cache"}B= _default_cache()
ABresult
1X1
011
000
346 0 cache => $args->{cache} // _default_cache(),
347 }, $class;
348 }
349  
350 =head2 check_ranks
351  
352 my $ranks = Database::BI::Model::Boundary->check_ranks({ LK => 10 }); # { lk => 10 }
353  
354 =head3 API SPECIFICATION
355  
356 =head4 INPUT
357  
358 ranks : hashref of { country code => place rank }
359  
360 =head4 OUTPUT
361  
362 A new hashref with the codes in lower case and the ranks as numbers. Croaks
363 on a value that is not a hashref, a key that is not a two-letter code, or a
364 rank that is not 8, 10 or 12, naming the entry. Used by C<new> and by the
365 application's startup check of C<map_bounds_ranks>.
366  
367 =cut
368  
369 0 ▶sub check_ranks {
Subroutine check_ranks: not called CC=9 SCAR=45.0
Pod documented
370 0 my (undef, $ranks) = @_;
371 0 ▶ croak 'Boundary ranks must be a hash of country codes to place ranks'
Branch: 1 branch
BranchTrueFalseTotal
unless ref $ranks eq "HASH" 0 0 0
372 unless ref($ranks) eq 'HASH';
373 0 my %checked;
374 0 for my $code (sort keys %{$ranks}) {
375 0 my $rank = $ranks->{$code};
376 0 ▶ croak "'$code' is not a two-letter country code" unless $code =~ /\A[A-Za-z]{2}\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $code =~ /\A[A-Za-z]{2}\z/ 0 0 0
377 0 croak "the rank for '$code' must be one of " . join(', ', sort { $a <=> $b } keys %ZOOM_FOR_RANK)
378 0 ▶ unless defined($rank) && !ref($rank) && $rank =~ /\A\d+\z/ && $ZOOM_FOR_RANK{$rank};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rank and not ref $rank and $rank =~ /\A\d+\z/ and $ZOOM_FOR_RANK{$rank} 0 0 0
Condition0%0/9
defined $rank and not ref $rank and $rank =~ /\A\d+\z/ and $ZOOM_FOR_RANK{$rank}
!ll&&!rl&&r
000
defined $rank and not ref $rank and $rank =~ /\A\d+\z/
!ll&&!rl&&r
000
defined $rank and not ref $rank
!ll&&!rl&&r
000
MC/DC0%0/4
defined $rank and not ref $rank and $rank =~ /\A\d+\z/ and $ZOOM_FOR_RANK{$rank}
defined $rank not ref $rank $rank =~ /\A\d+\z/ $ZOOM_FOR_RANK{$rank}
Truth table0%0/5
A and not(B) and C and D
A= defined $rankB= not ref $rankC= $rank =~ /\A\d+\z/D= $ZOOM_FOR_RANK{$rank}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
379 0 ▶ croak "'$code' is given twice" if exists $checked{ lc $code };
Branch: 1 branch
BranchTrueFalseTotal
if exists $checked{lc $code} 0 0 0
380 0 $checked{ lc $code } = $rank + 0;
381 }
382 0 return \%checked;
383 }
384  
385 =head2 rank_for
386  
387 my $rank = $boundary->rank_for('in'); # 10
388  
389 =head3 API SPECIFICATION
390  
391 =head4 INPUT
392  
393 country : string, an ISO 3166-1 alpha-2 code in any case; undef or ''
394 for a point in no country (the sea)
395  
396 =head4 OUTPUT
397  
398 The place rank an outline in that country must have at the county level: the
399 country's own, from C<ranks> or the built-in list, else 12. At another level,
400 the most detailed rank its first try takes there.
401  
402 =cut
403  
404 0 ▶sub rank_for {
Subroutine rank_for: not called CC=6 SCAR=37.4
Pod documented
405 0 my ($self, $country) = @_;
406 0 my $level = $LEVELS{ $self->{level} };
407 0 ▶ return $self->_plan($country)->[0]{ranks}[1] unless defined $level->{rank};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$level{"rank"} 0 0 0
408 my $rank = (defined($country) && !ref($country) && length($country))
409 0 ▶ ? $self->{ranks}{ lc $country } : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $country && !ref($country) && length $country ? : 0 0 0
Condition0%0/6
defined $country and not ref $country and length $country
!ll&&!rl&&r
000
defined $country and not ref $country
!ll&&!rl&&r
000
MC/DC0%0/3
defined $country and not ref $country and length $country
defined $country not ref $country length $country
Truth table0%0/4
A and not(B) and C
A= defined $countryB= not ref $countryC= length $country
ABCresult
0XX0
10X0
1100
1111
410 0 ▶ return $rank // $level->{rank};
Condition0%0/3
$rank // $$level{"rank"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$rank // $$level{"rank"}
$rank $$level{"rank"}
Truth table0%0/3
A // B
A= $rankB= $$level{"rank"}
ABresult
1X1
011
000
411 }
412  
413 =head2 name_for
414  
415 my $word = $boundary->name_for('in'); # 'district'
416  
417 What this object's level is called in a country, so a caller can say
418 "district" on a map of India rather than "county".
419  
420 =head3 API SPECIFICATION
421  
422 =head4 INPUT
423  
424 country : string, an ISO 3166-1 alpha-2 code in any case; undef or ''
425 for a point in no country
426  
427 =head4 OUTPUT
428  
429 One lower-case English word: C<country>, C<state>, C<region>, C<province>,
430 C<prefecture>, C<county>, C<district>, C<department>, C<city>, C<town> or
431 C<neighbourhood>. A country not listed gets the level's own name
432 (L</level_name>). It is a word to look a translation up by, not text to show.
433 Called on the class, it answers for the county level.
434  
435 =cut
436  
437 0 ▶sub name_for {
Subroutine name_for: not called CC=7 SCAR=40.3
Pod documented
438 0 my ($self, $country) = @_;
439 0 ▶ my $level = $LEVELS{ ref($self) ? $self->{level} : 'county' };
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 0 0 0
440 my $name = (defined($country) && !ref($country) && length($country))
441 0 ▶ ? ($level->{names} // {})->{ lc $country } : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $country && !ref($country) && length $country ? : 0 0 0
Condition0%0/8
defined $country and not ref $country and length $country
!ll&&!rl&&r
000
defined $country and not ref $country
!ll&&!rl&&r
000
$$level{"names"} // {}
l!l
00
MC/DC0%0/3
defined $country and not ref $country and length $country
defined $country not ref $country length $country
MC/DC0%0/1
$$level{"names"} // {}
$$level{"names"}
Truth table0%0/4
A and not(B) and C
A= defined $countryB= not ref $countryC= length $country
ABCresult
0XX0
10X0
1100
1111
Truth table0%0/2
A
A= $$level{"names"}
Aresult
11
00
442 0 ▶ return $name // $level->{name};
Condition0%0/3
$name // $$level{"name"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$name // $$level{"name"}
$name $$level{"name"}
Truth table0%0/3
A // B
A= $nameB= $$level{"name"}
ABresult
1X1
011
000
443 }
444  
445 =head2 level_name
446  
447 my $word = Database::BI::Model::Boundary->level_name('state'); # 'state'
448  
449 =head3 API SPECIFICATION
450  
451 =head4 INPUT
452  
453 level : string, one of levels()
454  
455 =head4 OUTPUT
456  
457 What the level's areas are called where no country says otherwise, as
458 L</name_for> gives it: the word to use before a point's country is known.
459 Undef for a level that is not one.
460  
461 =cut
462  
463 8 ▶sub level_name {
Subroutine level_name: called CC=4 SCAR=18.9
Pod documented
464 8 my (undef, $level) = @_;
465 8 ▶ return unless defined($level) && !ref($level) && $LEVELS{$level};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $level and not ref $level and $LEVELS{$level} 8 0 8
Condition33%2/6
defined $level and not ref $level and $LEVELS{$level}
!ll&&!rl&&r
800
defined $level and not ref $level
!ll&&!rl&&r
800
MC/DC0%0/3
defined $level and not ref $level and $LEVELS{$level}
defined $level not ref $level $LEVELS{$level}
Truth table25%1/4
A and not(B) and C
A= defined $levelB= not ref $levelC= $LEVELS{$level}
ABCresult
0XX0
10X0
1100
1111
466 0 return $LEVELS{$level}{name};
467 }
468  
469 =head2 levels
470  
471 my @levels = Database::BI::Model::Boundary->levels;
472 # ('country', 'state', 'county', 'city', 'town', 'neighbourhood')
473  
474 =head3 API SPECIFICATION
475  
476 =head4 INPUT
477  
478 None.
479  
480 =head4 OUTPUT
481  
482 The names of the levels new() takes, the largest area first.
483  
484 =cut
485  
486 0 ▶sub levels {
Subroutine levels: not called CC=1 SCAR=6.9
Pod documented
487 0 return @LEVEL_ORDER;
488 }
489  
490 =head2 point_key
491  
492 my $key = Database::BI::Model::Boundary->point_key(39.0901, -77.6402); # '39.090,-77.640'
493  
494 =head3 API SPECIFICATION
495  
496 =head4 INPUT
497  
498 lat : number -90 .. 90
499 lon : number -180 .. 180
500  
501 =head4 OUTPUT
502  
503 The point rounded to three decimal places, as C<"lat,lon">: what the cache is
504 keyed by and all of the point that is sent. Undef for a point out of range.
505  
506 =cut
507  
508 0 ▶sub point_key {
Subroutine point_key: not called CC=7 SCAR=40.3
Pod documented
509 0 my (undef, $lat, $lon) = @_;
510  
511 0 ▶ return unless defined($lat) && defined($lon)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $lat and defined $lon and $lat =~ /\A-?\d+(?:\.\d+)?\z/ and $lon =~ /\A-?\d+(?:\.\d+)?\z/ and abs $lat <= 90 and abs $lon <= 180 0 0 0
Condition0%0/15
defined $lat and defined $lon and $lat =~ /\A-?\d+(?:\.\d+)?\z/ and $lon =~ /\A-?\d+(?:\.\d+)?\z/ and abs $lat <= 90 and abs $lon <= 180
!ll&&!rl&&r
000
defined $lat and defined $lon and $lat =~ /\A-?\d+(?:\.\d+)?\z/ and $lon =~ /\A-?\d+(?:\.\d+)?\z/ and abs $lat <= 90
!ll&&!rl&&r
000
defined $lat and defined $lon and $lat =~ /\A-?\d+(?:\.\d+)?\z/ and $lon =~ /\A-?\d+(?:\.\d+)?\z/
!ll&&!rl&&r
000
defined $lat and defined $lon and $lat =~ /\A-?\d+(?:\.\d+)?\z/
!ll&&!rl&&r
000
defined $lat and defined $lon
!ll&&!rl&&r
000
MC/DC0%0/6
defined $lat and defined $lon and $lat =~ /\A-?\d+(?:\.\d+)?\z/ and $lon =~ /\A-?\d+(?:\.\d+)?\z/ and abs $lat <= 90 and abs $lon <= 180
defined $lat defined $lon $lat =~ /\A-?\d+(?:\.\d+)?\z/ $lon =~ /\A-?\d+(?:\.\d+)?\z/ abs $lat <= 90 abs $lon <= 180
Truth table0%0/7
A and B and C and D and E and F
A= defined $latB= defined $lonC= $lat =~ /\A-?\d+(?:\.\d+)?\z/D= $lon =~ /\A-?\d+(?:\.\d+)?\z/E= abs $lat <= 90F= abs $lon <= 180
ABCDEFresult
0XXXXX0
10XXXX0
110XXX0
1110XX0
11110X0
1111100
1111111
512 && $lat =~ /\A-?\d+(?:\.\d+)?\z/ && $lon =~ /\A-?\d+(?:\.\d+)?\z/
513 && abs($lat) <= 90 && abs($lon) <= 180;
514  
515 my @parts = map {
516 0 my $text = sprintf("%.${DECIMALS}f", $_);
517 0 $text =~ s/\A-(?=0\.0+\z)//; # no "-0.000", so a point has one key
518 0 $text;
519 } ($lat, $lon);
520 0 return join(',', @parts);
521 }
522  
523 =head2 outlines
524  
525 my $result = $boundary->outlines(\@points, lookup => 1);
526  
527 =head3 API SPECIFICATION
528  
529 =head4 INPUT
530  
531 points : arrayref of [lat, lon]
532 lookup : boolean, default 1. 0 asks Nominatim nothing: the points not
533 known yet come back in pending, for the caller to look up one at
534 a time (lookup_point)
535  
536 =head4 OUTPUT
537  
538 {
539 features => [ GeoJSON Feature, ... ] one per outline, in the order
540 their first point came; properties { name, osm, called }
541 (called: what the division is called there, as name_for)
542 pending => [ point_key, ... ] points not known, not asked
543 inside => { point_key => osm, ... } the outline each point drawn is in,
544 by the osm of its Feature (to total what is in each)
545 counts => { points, drawn, attempted, nowhere }
546 offline => 0|1 points were left because Nominatim may not be asked
547 failed => 0|1 Nominatim did not answer; the rest were left
548 }
549  
550 A point out of range is ignored. A Feature's name is the place's name as
551 OpenStreetMap has it: text, not HTML.
552  
553 =cut
554  
555 0 ▶sub outlines {
Subroutine outlines: not called CC=22 SCAR=62.3
Pod documented
556 0 my ($self, $points, @rest) = @_;
557 0 ▶ croak 'outlines() needs an arrayref of [lat, lon]' unless ref($points) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $points eq "ARRAY" 0 0 0
558 0 my %opt = @rest;
559 0 ▶ my $lookup = exists($opt{lookup}) ? $opt{lookup} : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $opt{'lookup'} ? : 0 0 0
560  
561 0 local ($@, $!);
562  
563 0 my (@keys, %seen);
564 0 for my $point (@{$points}) {
565 0 ▶ next unless ref($point) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $point eq "ARRAY" 0 0 0
566 0 my $key = $self->point_key(@{$point}[0, 1]);
567 0 ▶ push @keys, $key if defined($key) && !$seen{$key}++;
Branch: 1 branch
BranchTrueFalseTotal
if defined $key and not $seen{$key}++ 0 0 0
Condition0%0/3
defined $key and not $seen{$key}++
!ll&&!rl&&r
000
MC/DC0%0/2
defined $key and not $seen{$key}++
defined $key not $seen{$key}++
Truth table0%0/3
A and not(B)
A= defined $keyB= not $seen{$key}++
ABresult
0X0
100
111
568 }
569  
570 0 my %counts = (points => scalar(@keys), drawn => 0, attempted => 0, nowhere => 0);
571 0 my (@order, %used, %inside, @pending, $failed, $offline);
572 0 my $index = $self->_index;
573  
574 0 for my $key (@keys) {
575 0 my ($osm, $status) = $self->_known($key, $index);
576 0 ▶ if (!defined($status) && $lookup) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $status and $lookup) 0 0 0
Condition0%0/3
not defined $status and $lookup
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $status and $lookup
not defined $status $lookup
Truth table0%0/3
not(A) and B
A= not defined $statusB= $lookup
ABresult
0X0
100
111
577 0 ▶ if (!$self->_can_ask) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $self->_can_ask) { } 0 0 0
elsif (not $failed and $counts{'attempted'} < $$self{'online_max'}) { } 0 0 0
578 0 $offline = 1;
579 ▶ } elsif (!$failed && $counts{attempted} < $self->{online_max}) {
Condition0%0/3
not $failed and $counts{"attempted"} < $$self{"online_max"}
!ll&&!rl&&r
000
MC/DC0%0/2
not $failed and $counts{"attempted"} < $$self{"online_max"}
not $failed $counts{"attempted"} < $$self{"online_max"}
Truth table0%0/3
not(A) and B
A= not $failedB= $counts{"attempted"} < $$self{"online_max"}
ABresult
0X0
100
111
580 0 $counts{attempted}++;
581 0 ($osm, $status) = $self->_ask($key, $index);
582 0 ▶ $failed = 1 if $status eq 'failed';
Branch: 1 branch
BranchTrueFalseTotal
if $status eq "failed" 0 0 0
583 }
584 }
585 0 ▶ if (!defined($status) || $status eq 'failed') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $status or $status eq "failed") 0 0 0
Condition0%0/3
not defined $status or $status eq "failed"
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $status or $status eq "failed"
not defined $status $status eq "failed"
Truth table0%0/3
not(A) or B
A= not defined $statusB= $status eq "failed"
ABresult
1X1
011
000
586 0 push @pending, $key;
587 0 next;
588 }
589 0 ▶ if ($status eq 'nowhere') {
Branch: 1 branch
BranchTrueFalseTotal
if ($status eq "nowhere") 0 0 0
590 0 $counts{nowhere}++;
591 0 next;
592 }
593 0 ▶ push @order, $osm unless $used{$osm}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $used{$osm}++ 0 0 0
594 0 $inside{$key} = $osm;
595 }
596  
597 0 my @features;
598 0 for my $osm (@order) {
599 0 ▶ my $outline = $self->_get($self->_outline_key($osm)) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $outline = $self->_get($self->_outline_key($osm)) or next 0 0 0
600 push @features, {
601 type => 'Feature',
602 properties => { name => $outline->{name}, osm => $osm,
603 called => $self->name_for($outline->{country}) },
604 geometry => $outline->{geometry},
605 0 };
606 }
607 0 $counts{drawn} = scalar @features;
608 0 my %drawn = map { $_->{properties}{osm} => 1 } @features;
609 0 delete @inside{ grep { !$drawn{ $inside{$_} } } keys %inside };
610  
611 return {
612 0 ▶ features => \@features,
Branch: 2 branches
BranchTrueFalseTotal
$offline ? : 0 0 0
$failed ? : 0 0 0
613 pending => \@pending,
614 inside => \%inside,
615 counts => \%counts,
616 offline => $offline ? 1 : 0,
617 failed => $failed ? 1 : 0,
618 };
619 }
620  
621 =head2 lookup_point
622  
623 my $answer = $boundary->lookup_point('39.090,-77.640');
624  
625 =head3 API SPECIFICATION
626  
627 =head4 INPUT
628  
629 key : string, a point_key
630  
631 =head4 OUTPUT
632  
633 { status => 'found' | 'nowhere' | 'held', name => string|undef, asked => 0|1 }
634  
635 'found' and 'nowhere' are remembered. 'held' is a point that could not be
636 settled now - Nominatim did not answer, or may not be asked - and is asked
637 again next time. asked says whether Nominatim was asked. Croaks on a key
638 that is not a point_key.
639  
640 =cut
641  
642 0 ▶sub lookup_point {
Subroutine lookup_point: not called CC=12 SCAR=50.5
Pod documented
643 0 my ($self, $key) = @_;
644  
645 0 ▶ croak 'lookup_point() needs a point key such as 39.090,-77.640'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key and not ref $key and $key =~ /\A(-?\d{1,2}\.\d{$DECIMALS}),(-?\d{1,3}\.\d{$DECIMALS})\z/ and defined $self->point_key($1, $2) and $self->point_key($1, $2) eq $key 0 0 0
Condition0%0/12
defined $key and not ref $key and $key =~ /\A(-?\d{1,2}\.\d{$DECIMALS}),(-?\d{1,3}\.\d{$DECIMALS})\z/ and defined $self->point_key($1, $2) and $self->point_key($1, $2) eq $key
!ll&&!rl&&r
000
defined $key and not ref $key and $key =~ /\A(-?\d{1,2}\.\d{$DECIMALS}),(-?\d{1,3}\.\d{$DECIMALS})\z/ and defined $self->point_key($1, $2)
!ll&&!rl&&r
000
defined $key and not ref $key and $key =~ /\A(-?\d{1,2}\.\d{$DECIMALS}),(-?\d{1,3}\.\d{$DECIMALS})\z/
!ll&&!rl&&r
000
defined $key and not ref $key
!ll&&!rl&&r
000
MC/DC0%0/5
defined $key and not ref $key and $key =~ /\A(-?\d{1,2}\.\d{$DECIMALS}),(-?\d{1,3}\.\d{$DECIMALS})\z/ and defined $self->point_key($1, $2) and $self->point_key($1, $2) eq $key
defined $key not ref $key $key =~ /\A(-?\d{1,2}\.\d{$DECIMALS}),(-?\d{1,3}\.\d{$DECIMALS})\z/ defined $self->point_key($1, $2) $self->point_key($1, $2) eq $key
Truth table0%0/6
A and not(B) and C and D and E
A= defined $keyB= not ref $keyC= $key =~ /\A(-?\d{1,2}\.\d{$DECIMALS}),(-?\d{1,3}\.\d{$DECIMALS})\z/D= defined $self->point_key($1, $2)E= $self->point_key($1, $2) eq $key
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
646 unless defined($key) && !ref($key)
647 && $key =~ /\A(-?\d{1,2}\.\d{$DECIMALS}),(-?\d{1,3}\.\d{$DECIMALS})\z/
648 && defined($self->point_key($1, $2)) && $self->point_key($1, $2) eq $key;
649  
650 0 local ($@, $!);
651  
652 0 my $index = $self->_index;
653 0 my ($osm, $status) = $self->_known($key, $index);
654 0 my $asked = 0;
655 0 ▶ if (!defined($status) && $self->_can_ask) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $status and $self->_can_ask) 0 0 0
Condition0%0/3
not defined $status and $self->_can_ask
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $status and $self->_can_ask
not defined $status $self->_can_ask
Truth table0%0/3
not(A) and B
A= not defined $statusB= $self->_can_ask
ABresult
0X0
100
111
656 0 $asked = 1;
657 0 ($osm, $status) = $self->_ask($key, $index);
658 }
659  
660 0 ▶ return { status => 'held', name => undef, asked => $asked }
Branch: 1 branch
BranchTrueFalseTotal
if not defined $status or $status eq "failed" 0 0 0
Condition0%0/3
not defined $status or $status eq "failed"
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $status or $status eq "failed"
not defined $status $status eq "failed"
Truth table0%0/3
not(A) or B
A= not defined $statusB= $status eq "failed"
ABresult
1X1
011
000
661 if !defined($status) || $status eq 'failed';
662 0 ▶ return { status => 'nowhere', name => undef, asked => $asked } if $status eq 'nowhere';
Branch: 1 branch
BranchTrueFalseTotal
if $status eq "nowhere" 0 0 0
663  
664 0 my $outline = $self->_get($self->_outline_key($osm));
665 0 ▶ return { status => 'found', name => ($outline ? $outline->{name} : undef), asked => $asked };
Branch: 1 branch
BranchTrueFalseTotal
$outline ? : 0 0 0
666 }
667  
668 =head2 file_cache
669  
670 my $cache = Database::BI::Model::Boundary->file_cache($model_dir);
671  
672 =head3 API SPECIFICATION
673  
674 =head4 INPUT
675  
676 dir : string, an existing directory (model_dir)
677  
678 =head4 OUTPUT
679  
680 A CHI File cache under C<< <dir>/boundary-cache >>, with a copy of what it
681 reads in memory for as long as the cache object lives. Croaks when dir is not a directory.
682  
683 =cut
684  
685 0 ▶sub file_cache {
Subroutine file_cache: not called CC=5 SCAR=34.0
Pod documented
686 0 my (undef, $dir) = @_;
687  
688 0 ▶ croak 'The boundary cache needs a directory, not ' . (defined($dir) ? $dir : 'undef')
Branch: 2 branches
BranchTrueFalseTotal
unless defined $dir and not ref $dir and -d $dir 0 0 0
defined $dir ? : 0 0 0
Condition0%0/6
defined $dir and not ref $dir and -d $dir
!ll&&!rl&&r
000
defined $dir and not ref $dir
!ll&&!rl&&r
000
MC/DC0%0/3
defined $dir and not ref $dir and -d $dir
defined $dir not ref $dir -d $dir
Truth table0%0/4
A and not(B) and C
A= defined $dirB= not ref $dirC= -d $dir
ABCresult
0XX0
10X0
1100
1111
689 unless defined($dir) && !ref($dir) && -d $dir;
690  
691 0 require CHI;
692 0 require File::Spec;
693 0 return CHI->new(
694 driver => 'File',
695 root_dir => File::Spec->catdir($dir, $CACHE_DIR),
696 namespace => $CACHE_NAMESPACE,
697 # A copy in memory for this object, which a request makes afresh: it
698 # saves reading the index and outlines again within the request. Not
699 # global, which would share it with every cache of this namespace in
700 # the process, whatever its folder, and keep a worker from seeing
701 # what another has added since.
702 l1_cache => { driver => 'RawMemory', global => 0, max_size => $CACHE_MAX_SIZE },
703 );
704 }
705  
706 # ---------------------------------------------------------------------------
707 # Private
708 # ---------------------------------------------------------------------------
709  
710 1 my $default_cache;
711  
712 0 ▶sub _default_cache {
Subroutine _default_cache: not called CC=2 SCAR=17.9
713 0 require CHI;
714 0 ▶ $default_cache //= CHI->new(
Condition0%0/3
$default_cache //= "CHI"->new("driver", "Memory", "global", 1, "namespace", $CACHE_NAMESPACE, "max_size", $CACHE_MAX_SIZE)
l!l&&r!l&&!r
000
MC/DC0%0/2
$default_cache //= "CHI"->new("driver", "Memory", "global", 1, "namespace", $CACHE_NAMESPACE, "max_size", $CACHE_MAX_SIZE)
$default_cache "CHI"->new("driver", "Memory", "global", 1, "namespace", $CACHE_NAMESPACE, "max_size", $CACHE_MAX_SIZE)
Truth table0%0/3
A //= B
A= $default_cacheB= "CHI"->new("driver", "Memory", "global", 1, "namespace", $CACHE_NAMESPACE, "max_size", $CACHE_MAX_SIZE)
ABresult
1X1
011
000
715 driver => 'Memory',
716 global => 1,
717 namespace => $CACHE_NAMESPACE,
718 max_size => $CACHE_MAX_SIZE,
719 );
720 0 return $default_cache;
721 }
722  
723 # _get($key) / _set($key, $value): the cache, under this level's prefix for a
724 # point and under _outline_key for an outline. A cache that fails reads as empty and writes as nothing: the page
725 # still draws, and asks again next time.
726 0 ▶sub _get {
Subroutine _get: not called CC=1 SCAR=6.9
727 0 my ($self, $key) = @_;
728 0 my $value = eval { $self->{cache}->get($key) };
729 0 return $value;
730 }
731  
732 0 ▶sub _set {
Subroutine _set: not called CC=1 SCAR=6.9
733 0 my ($self, $key, $value) = @_;
734 0 return eval { $self->{cache}->set($key, $value, 'never'); 1 };
735 }
736  
737 0 ▶sub _point_cache_key {
Subroutine _point_cache_key: not called CC=1 SCAR=6.9
738 0 my ($self, $key) = @_;
739 0 return "point:$self->{level}:$key";
740 }
741  
742 # _detail() -> how far this level's outlines are simplified, as text
743 0 ▶sub _detail {
Subroutine _detail: not called CC=1 SCAR=6.9
744 0 my $self = shift;
745 0 return $LEVELS{ $self->{level} }{detail};
746 }
747  
748 # _outline_key($osm) -> the cache key of an outline at this level's detail.
749 # One relation is one outline at every level of the same detail, and a copy
750 # apiece where they differ, so two levels never fetch over each other's.
751 0 ▶sub _outline_key {
Subroutine _outline_key: not called CC=2 SCAR=17.9
752 0 my ($self, $osm) = @_;
753 0 my $detail = $self->_detail;
754 0 ▶ return $detail eq $PLAIN_DETAIL ? "outline:$osm" : "outline:$osm:$detail";
Branch: 1 branch
BranchTrueFalseTotal
$detail eq $PLAIN_DETAIL ? : 0 0 0
755 }
756  
757 # _plan($country) -> arrayref of tries { zoom, ranks => [low, high], places }
758 #
759 # How this level is asked for in a country (undef or '' for none known): the
760 # county level's one rank at the zoom that asks for it, else the level's
761 # tries, or the country's own.
762 0 ▶sub _plan {
Subroutine _plan: not called CC=7 SCAR=40.3
763 0 my ($self, $country) = @_;
764 0 my $level = $LEVELS{ $self->{level} };
765 0 ▶ $country = (defined($country) && !ref($country)) ? lc($country) : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $country && !ref($country) ? : 0 0 0
Condition0%0/3
defined $country and not ref $country
!ll&&!rl&&r
000
MC/DC0%0/2
defined $country and not ref $country
defined $country not ref $country
Truth table0%0/3
A and not(B)
A= defined $countryB= not ref $country
ABresult
0X0
100
111
766 0 ▶ if (defined $level->{rank}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $$level{"rank"}) 0 0 0
767 0 ▶ my $rank = $self->{ranks}{$country} // $level->{rank};
Condition0%0/3
$$self{"ranks"}{$country} // $$level{"rank"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$$self{"ranks"}{$country} // $$level{"rank"}
$$self{"ranks"}{$country} $$level{"rank"}
Truth table0%0/3
A // B
A= $$self{"ranks"}{$country}B= $$level{"rank"}
ABresult
1X1
011
000
768 0 return [ { zoom => $ZOOM_FOR_RANK{$rank}, ranks => [ $rank, $rank ] } ];
769 }
770 0 ▶ return ($level->{plans} // {})->{$country} // $level->{tries};
Condition0%0/5
($$level{"plans"} // {})->{$country} // $$level{"tries"}
l!l&&r!l&&!r
000
$$level{"plans"} // {}
l!l
00
MC/DC0%0/2
($$level{"plans"} // {})->{$country} // $$level{"tries"}
$$level{"plans"} $$level{"tries"}
Truth table0%0/3
A // B
A= $$level{"plans"}B= $$level{"tries"}
ABresult
1X1
011
000
771 }
772  
773 # _plan_sig(\@plan) -> the plan as text, kept with a point found nowhere so a
774 # changed plan asks again.
775 0 ▶sub _plan_sig {
Subroutine _plan_sig: not called CC=2 SCAR=17.9
776 0 my $plan = shift;
777 0 ▶ return join(';', map { "$_->{zoom}:$_->{ranks}[0]-$_->{ranks}[1]" . ($_->{places} ? ':p' : '') } @{$plan});
Branch: 1 branch
BranchTrueFalseTotal
$$_{'places'} ? : 0 0 0
778 }
779  
780 # _accepts(\@plan, $rank) -> 1 when some try of the plan takes that rank
781 0 ▶sub _accepts {
Subroutine _accepts: not called CC=2 SCAR=17.9
782 0 my ($plan, $rank) = @_;
783 0 ▶ return (first { $rank >= $_->{ranks}[0] && $rank <= $_->{ranks}[1] } @{$plan}) ? 1 : 0;
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 2 branches
BranchTrueFalseTotal
&first(sub { $rank >= $$_{'ranks'}[0] and $rank <= $$_{'ranks'}[1]; } , @{$plan;}) ? : 0 0 0
$rank >= $$_{"ranks"}[0] and $rank <= $$_{"ranks"}[1] 0 0 0
784 }
785  
786 # _index() -> arrayref of [osm, south, west, north, east]
787 #
788 # The bounding box of every outline this level has found, so a point can be
789 # tested against the few that could hold it. Two processes adding at once can
790 # lose one's entry; that costs a request later, not a wrong answer.
791 0 ▶sub _index {
Subroutine _index: not called CC=2 SCAR=17.9
792 0 my $self = shift;
793 0 my $index = $self->_get("index:$self->{level}");
794 0 ▶ return ref($index) eq 'ARRAY' ? [ @{$index} ] : [];
Branch: 1 branch
BranchTrueFalseTotal
ref $index eq 'ARRAY' ? : 0 0 0
795 }
796  
797 # _known($key, \@index) -> ($osm, $status) or ()
798 #
799 # What is known of a point without asking: remembered with an outline or with
800 # none, or inside an outline the index holds (which is then remembered).
801 0 ▶sub _known {
Subroutine _known: not called CC=17 SCAR=57.2
802 0 my ($self, $key, $index) = @_;
803  
804 0 my $remembered = $self->_get($self->_point_cache_key($key));
805 0 ▶ if (ref($remembered) eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $remembered eq 'HASH') { } 0 0 0
elsif (defined $remembered and length $remembered) { } 0 0 0
806 # Nowhere, by the plan its country had then. 0.023.0 kept the rank
807 # alone, which names a county plan.
808 0 my $sig = $remembered->{plan};
809 $sig //= "$ZOOM_FOR_RANK{ $remembered->{rank} }:$remembered->{rank}-$remembered->{rank}"
810 0 ▶ if defined($remembered->{rank}) && $ZOOM_FOR_RANK{ $remembered->{rank} };
Branch: 1 branch
BranchTrueFalseTotal
if defined $$remembered{"rank"} and $ZOOM_FOR_RANK{$$remembered{"rank"}} 0 0 0
Condition0%0/5
defined $$remembered{"rank"} and $ZOOM_FOR_RANK{$$remembered{"rank"}}
!ll&&!rl&&r
000
$sig //= qq[$ZOOM_FOR_RANK{$$remembered{"rank"}}:$$remembered{"rank"}-$$remembered{"rank"}]
l!l
00
MC/DC0%0/2
defined $$remembered{"rank"} and $ZOOM_FOR_RANK{$$remembered{"rank"}}
defined $$remembered{"rank"} $ZOOM_FOR_RANK{$$remembered{"rank"}}
MC/DC0%0/1
$sig //= qq[$ZOOM_FOR_RANK{$$remembered{"rank"}}:$$remembered{"rank"}-$$remembered{"rank"}]
$sig
Truth table0%0/3
A and B
A= defined $$remembered{"rank"}B= $ZOOM_FOR_RANK{$$remembered{"rank"}}
ABresult
0X0
100
111
Truth table0%0/2
A
A= $sig
Aresult
11
00
811 return (undef, 'nowhere')
812 0 ▶ if defined($sig) && $sig eq _plan_sig($self->_plan($remembered->{country}));
Branch: 1 branch
BranchTrueFalseTotal
if defined $sig and $sig eq _plan_sig($self->_plan($$remembered{"country"})) 0 0 0
Condition0%0/3
defined $sig and $sig eq _plan_sig($self->_plan($$remembered{"country"}))
!ll&&!rl&&r
000
MC/DC0%0/2
defined $sig and $sig eq _plan_sig($self->_plan($$remembered{"country"}))
defined $sig $sig eq _plan_sig($self->_plan($$remembered{"country"}))
Truth table0%0/3
A and B
A= defined $sigB= $sig eq _plan_sig($self->_plan($$remembered{"country"}))
ABresult
0X0
100
111
813 ▶ } elsif (defined($remembered) && length($remembered)) {
Condition0%0/3
defined $remembered and length $remembered
!ll&&!rl&&r
000
MC/DC0%0/2
defined $remembered and length $remembered
defined $remembered length $remembered
Truth table0%0/3
A and B
A= defined $rememberedB= length $remembered
ABresult
0X0
100
111
814 0 ▶ return ($remembered, 'found') if $self->_current($self->_get($self->_outline_key($remembered)));
Branch: 1 branch
BranchTrueFalseTotal
if $self->_current($self->_get($self->_outline_key($remembered))) 0 0 0
815 # The outline has gone from the cache, or its country's rank changed.
816 }
817 # '' is nowhere from before ranks were kept: ask again.
818  
819 0 my ($lat, $lon) = split /,/, $key;
820 0 for my $entry (@{$index}) {
821 0 my ($osm, $south, $west, $north, $east) = @{$entry};
822 0 ▶ next if $lat < $south || $lat > $north || $lon < $west || $lon > $east;
Branch: 1 branch
BranchTrueFalseTotal
if $lat < $south or $lat > $north or $lon < $west or $lon > $east 0 0 0
Condition0%0/9
$lat < $south or $lat > $north or $lon < $west or $lon > $east
l!l&&r!l&&!r
000
$lat < $south or $lat > $north or $lon < $west
l!l&&r!l&&!r
000
$lat < $south or $lat > $north
l!l&&r!l&&!r
000
MC/DC0%0/4
$lat < $south or $lat > $north or $lon < $west or $lon > $east
$lat < $south $lat > $north $lon < $west $lon > $east
Truth table0%0/5
A or B or C or D
A= $lat < $southB= $lat > $northC= $lon < $westD= $lon > $east
ABCDresult
1XXX1
01XX1
001X1
00011
00000
823 0 my $outline = $self->_get($self->_outline_key($osm));
824 0 ▶ next unless $self->_current($outline) && _contains($outline->{geometry}, $lat, $lon);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_current($outline) and _contains($$outline{"geometry"}, $lat, $lon) 0 0 0
Condition0%0/3
$self->_current($outline) and _contains($$outline{"geometry"}, $lat, $lon)
!ll&&!rl&&r
000
MC/DC0%0/2
$self->_current($outline) and _contains($$outline{"geometry"}, $lat, $lon)
$self->_current($outline) _contains($$outline{"geometry"}, $lat, $lon)
Truth table0%0/3
A and B
A= $self->_current($outline)B= _contains($$outline{"geometry"}, $lat, $lon)
ABresult
0X0
100
111
825 0 $self->_set($self->_point_cache_key($key), $osm);
826 0 return ($osm, 'found');
827 }
828  
829 0 return;
830 }
831  
832 # _current($outline) -> 1 when an outline was accepted at the rank its country
833 # wants now, and simplified as outlines are now. One kept before ranks were
834 # (no rank), or at another level of detail, is not.
835 0 ▶sub _current {
Subroutine _current: not called CC=5 SCAR=34.0
836 0 my ($self, $outline) = @_;
837 0 ▶ return 0 unless ref($outline) eq 'HASH' && defined($outline->{rank});
Branch: 1 branch
BranchTrueFalseTotal
unless ref $outline eq "HASH" and defined $$outline{"rank"} 0 0 0
Condition0%0/3
ref $outline eq "HASH" and defined $$outline{"rank"}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $outline eq "HASH" and defined $$outline{"rank"}
ref $outline eq "HASH" defined $$outline{"rank"}
Truth table0%0/3
A and B
A= ref $outline eq "HASH"B= defined $$outline{"rank"}
ABresult
0X0
100
111
838 0 ▶ return 0 unless ($outline->{detail} // '') eq $self->_detail;
Branch: 1 branch
BranchTrueFalseTotal
unless ($$outline{"detail"} // "") eq $self->_detail 0 0 0
Condition0%0/2
$$outline{"detail"} // ""
l!l
00
MC/DC0%0/1
$$outline{"detail"} // ""
$$outline{"detail"}
Truth table0%0/2
A
A= $$outline{"detail"}
Aresult
11
00
839 0 return _accepts($self->_plan($outline->{country}), $outline->{rank});
840 }
841  
842 # _last_country() / _last_country($code): the country of the last reply at
843 # this level, whose rank the next point is asked at first. Kept in the cache,
844 # so the first point of a later map is not asked twice.
845 0 ▶sub _last_country {
Subroutine _last_country: not called CC=6 SCAR=37.4
846 0 my ($self, @set) = @_;
847 0 my $key = "country:$self->{level}";
848 0 ▶ if (@set) {
Branch: 1 branch
BranchTrueFalseTotal
if (@set) 0 0 0
849 0 $self->{last_country} = $set[0];
850 0 ▶ $self->_set($key, $set[0]) unless ($self->_get($key) // '') eq $set[0];
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_get($key) // "") eq $set[0] 0 0 0
Condition0%0/2
$self->_get($key) // ""
l!l
00
MC/DC0%0/1
$self->_get($key) // ""
$self->_get($key)
Truth table0%0/2
A
A= $self->_get($key)
Aresult
11
00
851 0 return $set[0];
852 }
853 0 ▶ return $self->{last_country} //= $self->_get($key) // '';
Condition0%0/4
$$self{"last_country"} //= $self->_get($key) // ""
l!l
00
$self->_get($key) // ""
l!l
00
MC/DC0%0/1
$$self{"last_country"} //= $self->_get($key) // ""
$$self{"last_country"}
Truth table0%0/2
A
A= $$self{"last_country"}
Aresult
11
00
854 }
855  
856 0 ▶sub _can_ask {
Subroutine _can_ask: not called CC=4 SCAR=30.0
857 0 my $self = shift;
858 0 ▶ return 0 unless $self->{online};
Branch: 1 branch
BranchTrueFalseTotal
unless $$self{"online"} 0 0 0
859 0 ▶ return 1 if $self->{ua};
Branch: 1 branch
BranchTrueFalseTotal
if $$self{"ua"} 0 0 0
860 0 require Database::BI::Model::Geocode;
861 0 ▶ return Database::BI::Model::Geocode::nominatim_ua() ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
Database::BI::Model::Geocode::nominatim_ua() ? : 0 0 0
862 }
863  
864 # _ask($key, \@index) -> ($osm, 'found') | (undef, 'nowhere') | (undef, 'failed')
865 #
866 # Reverse lookups down a plan. The first is the first try of the plan of the
867 # last country seen; its reply names the point's country, whose plan is then
868 # followed from its start (a try at a zoom already asked uses that reply), to
869 # the first try that gives an outline. A found outline is remembered with its
870 # country and rank, added to the index (and to @$index, so the caller's later
871 # points are tested against it), and the point remembered with it. A point no
872 # try gives an outline for is remembered as nowhere, with its country and the
873 # plan. A failure is remembered as nothing.
874 0 ▶sub _ask {
Subroutine _ask: not called CC=18 SCAR=58.3
875 0 my ($self, $key, $index) = @_;
876 0 my ($lat, $lon) = split /,/, $key;
877  
878 0 my %replies; # by zoom
879 0 ▶ my $reverse = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
880 0 my $zoom = shift;
881 0 ▶ return $replies{$zoom} //= $self->_reverse($lat, $lon, $zoom);
Condition0%0/3
$replies{$zoom} //= $self->_reverse($lat, $lon, $zoom)
l!l&&r!l&&!r
000
MC/DC0%0/2
$replies{$zoom} //= $self->_reverse($lat, $lon, $zoom)
$replies{$zoom} $self->_reverse($lat, $lon, $zoom)
Truth table0%0/3
A //= B
A= $replies{$zoom}B= $self->_reverse($lat, $lon, $zoom)
ABresult
1X1
011
000
882 0 };
883  
884 0 my $plan = $self->_plan($self->_last_country);
885 0 ▶ my $first = $reverse->($plan->[0]{zoom}) or return (undef, 'failed');
Branch: 1 branch
BranchTrueFalseTotal
my $first = &$reverse($$plan[0]{"zoom"}) or return undef, "failed" 0 0 0
886  
887 0 ▶ my $country = ref($first->{address}) eq 'HASH' ? $first->{address}{country_code} : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $$first{'address'} eq 'HASH' ? : 0 0 0
888 0 ▶ $country = (defined($country) && !ref($country) && $country =~ /\A[A-Za-z]{2}\z/) ? lc($country) : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $country && !ref($country) && $country =~ /\A[A-Za-z]{2}\z/ ? : 0 0 0
Condition0%0/6
defined $country and not ref $country and $country =~ /\A[A-Za-z]{2}\z/
!ll&&!rl&&r
000
defined $country and not ref $country
!ll&&!rl&&r
000
MC/DC0%0/3
defined $country and not ref $country and $country =~ /\A[A-Za-z]{2}\z/
defined $country not ref $country $country =~ /\A[A-Za-z]{2}\z/
Truth table0%0/4
A and not(B) and C
A= defined $countryB= not ref $countryC= $country =~ /\A[A-Za-z]{2}\z/
ABCresult
0XX0
10X0
1100
1111
889 0 ▶ if (length $country) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $country) 0 0 0
890 0 $self->_last_country($country);
891 0 $plan = $self->_plan($country);
892 }
893  
894 0 my ($reply, $osm);
895 # "Unable to geocode" is the sea at any zoom: no try will do better.
896 0 ▶ for my $try (defined($first->{error}) ? () : @{$plan}) {
Branch: 1 branch
BranchTrueFalseTotal
defined $$first{'error'} ? : 0 0 0
897 0 ▶ my $answer = $reverse->($try->{zoom}) or return (undef, 'failed');
Branch: 1 branch
BranchTrueFalseTotal
my $answer = &$reverse($$try{"zoom"}) or return undef, "failed" 0 0 0
898 0 ▶ $osm = _outline_id($answer, $try) // next;
Condition0%0/2
_outline_id($answer, $try) // (next)
l!l
00
MC/DC0%0/1
_outline_id($answer, $try) // (next)
_outline_id($answer, $try)
Truth table0%0/2
A
A= _outline_id($answer, $try)
Aresult
11
00
899 0 $reply = $answer;
900 0 last;
901 }
902  
903 0 ▶ unless ($reply) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($reply) 0 0 0
904 # The sea, and answers at other ranks or with no shape, alike. rank is
905 # what 0.023.0 kept, and reads back as the same county plan.
906 $self->_set($self->_point_cache_key($key),
907 0 { country => $country, rank => $plan->[0]{ranks}[1], plan => _plan_sig($plan) });
908 0 return (undef, 'nowhere');
909 }
910  
911 0 my $geometry = $reply->{geojson};
912 0 my $name = $reply->{name};
913 0 ▶ $name = $reply->{display_name} unless defined($name) && length($name);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and length $name 0 0 0
Condition0%0/3
defined $name and length $name
!ll&&!rl&&r
000
MC/DC0%0/2
defined $name and length $name
defined $name length $name
Truth table0%0/3
A and B
A= defined $nameB= length $name
ABresult
0X0
100
111
914 $self->_set($self->_outline_key($osm), {
915 name => $name // $osm,
916 geometry => $geometry,
917 country => $country,
918 0 ▶ rank => $reply->{place_rank} + 0,
Condition0%0/3
$name // $osm
l!l&&r!l&&!r
000
MC/DC0%0/2
$name // $osm
$name $osm
Truth table0%0/3
A // B
A= $nameB= $osm
ABresult
1X1
011
000
919 detail => $self->_detail,
920 });
921  
922 0 ▶ unless (grep { $_->[0] eq $osm } @{$index}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$$_[0] eq $osm;} @{$index;}) 0 0 0
923 0 my $box = _bounds($geometry);
924 0 ▶ if ($box) {
Branch: 1 branch
BranchTrueFalseTotal
if ($box) 0 0 0
925 0 push @{$index}, [ $osm, @{$box} ];
926 0 my $stored = $self->_index;
927 0 ▶ push @{$stored}, [ $osm, @{$box} ] unless grep { $_->[0] eq $osm } @{$stored};
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$$_[0] eq $osm;} @{$stored;} 0 0 0
928 0 $self->_set("index:$self->{level}", $stored);
929 }
930 }
931 0 $self->_set($self->_point_cache_key($key), $osm);
932  
933 0 return ($osm, 'found');
934 }
935  
936 # _outline_id($reply, $try) -> 'R<id>' or 'W<id>' when the reply is an outline
937 # the try takes, else undef.
938 #
939 # An outline is an area with a shape, at a rank in the try's range: an
940 # administrative boundary's relation, or, for a try that takes places, a place
941 # mapped as an area (place=city, town, suburb), which may be a closed way. A
942 # place mapped as a point has no shape and is not one.
943 0 ▶sub _outline_id {
Subroutine _outline_id: not called CC=23 SCAR=63.1
944 0 my ($reply, $try) = @_;
945  
946 0 my $geometry = $reply->{geojson};
947 0 my $rank = $reply->{place_rank};
948 0 ▶ my $category = $reply->{category} // '';
Condition0%0/2
$$reply{"category"} // ""
l!l
00
MC/DC0%0/1
$$reply{"category"} // ""
$$reply{"category"}
Truth table0%0/2
A
A= $$reply{"category"}
Aresult
11
00
949 0 ▶ my $osm_type = $reply->{osm_type} // '';
Condition0%0/2
$$reply{"osm_type"} // ""
l!l
00
MC/DC0%0/1
$$reply{"osm_type"} // ""
$$reply{"osm_type"}
Truth table0%0/2
A
A= $$reply{"osm_type"}
Aresult
11
00
950 return unless defined($reply->{osm_id}) && $reply->{osm_id} =~ /\A\d+\z/
951 && defined($rank) && !ref($rank) && $rank =~ /\A\d+\z/
952 && $rank >= $try->{ranks}[0] && $rank <= $try->{ranks}[1]
953 && ref($geometry) eq 'HASH'
954 && ($geometry->{type} // '') =~ /\A(?:Multi)?Polygon\z/
955 0 ▶ && ref($geometry->{coordinates}) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$reply{"osm_id"} and $$reply{"osm_id"} =~ /\A\d+\z/ and defined $rank and not ref $rank and $rank =~ /\A\d+\z/ and $rank >= $$try{"ranks"}[0] and $rank <= $$try{"ranks"}[1] and ref $geometry eq "HASH" and ($$geometry{"type"} // "") =~ /\A(?:Multi)?Polygon\z/ and ref $$geometry{"coordinates"} eq "ARRAY" 0 0 0
Condition0%0/29
defined $$reply{"osm_id"} and $$reply{"osm_id"} =~ /\A\d+\z/ and defined $rank and not ref $rank and $rank =~ /\A\d+\z/ and $rank >= $$try{"ranks"}[0] and $rank <= $$try{"ranks"}[1] and ref $geometry eq "HASH" and ($$geometry{"type"} // "") =~ /\A(?:Multi)?Polygon\z/ and ref $$geometry{"coordinates"} eq "ARRAY"
!ll&&!rl&&r
000
defined $$reply{"osm_id"} and $$reply{"osm_id"} =~ /\A\d+\z/ and defined $rank and not ref $rank and $rank =~ /\A\d+\z/ and $rank >= $$try{"ranks"}[0] and $rank <= $$try{"ranks"}[1] and ref $geometry eq "HASH" and ($$geometry{"type"} // "") =~ /\A(?:Multi)?Polygon\z/
!ll&&!rl&&r
000
defined $$reply{"osm_id"} and $$reply{"osm_id"} =~ /\A\d+\z/ and defined $rank and not ref $rank and $rank =~ /\A\d+\z/ and $rank >= $$try{"ranks"}[0] and $rank <= $$try{"ranks"}[1] and ref $geometry eq "HASH"
!ll&&!rl&&r
000
defined $$reply{"osm_id"} and $$reply{"osm_id"} =~ /\A\d+\z/ and defined $rank and not ref $rank and $rank =~ /\A\d+\z/ and $rank >= $$try{"ranks"}[0] and $rank <= $$try{"ranks"}[1]
!ll&&!rl&&r
000
defined $$reply{"osm_id"} and $$reply{"osm_id"} =~ /\A\d+\z/ and defined $rank and not ref $rank and $rank =~ /\A\d+\z/ and $rank >= $$try{"ranks"}[0]
!ll&&!rl&&r
000
defined $$reply{"osm_id"} and $$reply{"osm_id"} =~ /\A\d+\z/ and defined $rank and not ref $rank and $rank =~ /\A\d+\z/
!ll&&!rl&&r
000
defined $$reply{"osm_id"} and $$reply{"osm_id"} =~ /\A\d+\z/ and defined $rank and not ref $rank
!ll&&!rl&&r
000
defined $$reply{"osm_id"} and $$reply{"osm_id"} =~ /\A\d+\z/ and defined $rank
!ll&&!rl&&r
000
defined $$reply{"osm_id"} and $$reply{"osm_id"} =~ /\A\d+\z/
!ll&&!rl&&r
000
$$geometry{"type"} // ""
l!l
00
MC/DC0%0/10
defined $$reply{"osm_id"} and $$reply{"osm_id"} =~ /\A\d+\z/ and defined $rank and not ref $rank and $rank =~ /\A\d+\z/ and $rank >= $$try{"ranks"}[0] and $rank <= $$try{"ranks"}[1] and ref $geometry eq "HASH" and ($$geometry{"type"} // "") =~ /\A(?:Multi)?Polygon\z/ and ref $$geometry{"coordinates"} eq "ARRAY"
defined $$reply{"osm_id"} $$reply{"osm_id"} =~ /\A\d+\z/ defined $rank not ref $rank $rank =~ /\A\d+\z/ $rank >= $$try{"ranks"}[0] $rank <= $$try{"ranks"}[1] ref $geometry eq "HASH" $$geometry{"type"} ref $$geometry{"coordinates"} eq "ARRAY"
Truth table0%0/11
A and B and C and not(D) and E and F and G and H and I and J
A= defined $$reply{"osm_id"}B= $$reply{"osm_id"} =~ /\A\d+\z/C= defined $rankD= not ref $rankE= $rank =~ /\A\d+\z/F= $rank >= $$try{"ranks"}[0]G= $rank <= $$try{"ranks"}[1]H= ref $geometry eq "HASH"I= $$geometry{"type"}J= ref $$geometry{"coordinates"} eq "ARRAY"
ABCDEFGHIJresult
0XXXXXXXXX0
10XXXXXXXX0
110XXXXXXX0
1110XXXXXX0
11110XXXXX0
111110XXXX0
1111110XXX0
11111110XX0
111111110X0
11111111100
11111111111
956  
957 0 ▶ my $boundary = $category eq 'boundary' && ($reply->{type} // '') eq 'administrative'
Condition0%0/8
$category eq 'boundary' && ($$reply{'type'} // '') eq 'administrative' && $osm_type eq 'relation'
!ll&&!rl&&r
000
$category eq "boundary" and ($$reply{"type"} // "") eq "administrative"
!ll&&!rl&&r
000
$$reply{"type"} // ""
l!l
00
MC/DC0%0/3
$category eq 'boundary' && ($$reply{'type'} // '') eq 'administrative' && $osm_type eq 'relation'
$category eq "boundary" $$reply{"type"} $osm_type eq 'relation'
Truth table0%0/4
A and B && C
A= $category eq "boundary"B= $$reply{"type"}C= $osm_type eq 'relation'
ABCresult
0XX0
10X0
1100
1111
958 && $osm_type eq 'relation';
959 0 ▶ my $place = $try->{places} && $category eq 'place' && ($osm_type eq 'relation' || $osm_type eq 'way');
Condition0%0/9
$$try{'places'} && $category eq 'place' && ($osm_type eq 'relation' || $osm_type eq 'way')
!ll&&!rl&&r
000
$$try{"places"} and $category eq "place"
!ll&&!rl&&r
000
$osm_type eq "relation" or $osm_type eq "way"
l!l&&r!l&&!r
000
MC/DC0%0/4
$$try{'places'} && $category eq 'place' && ($osm_type eq 'relation' || $osm_type eq 'way')
$$try{"places"} $category eq "place" $osm_type eq "relation" $osm_type eq "way"
Truth table0%0/5
A and B && C or D
A= $$try{"places"}B= $category eq "place"C= $osm_type eq "relation"D= $osm_type eq "way"
ABCDresult
0XXX0
10XX0
11000
111X1
11011
960 0 ▶ return unless $boundary || $place;
Branch: 1 branch
BranchTrueFalseTotal
unless $boundary or $place 0 0 0
Condition0%0/3
$boundary or $place
l!l&&r!l&&!r
000
MC/DC0%0/2
$boundary or $place
$boundary $place
Truth table0%0/3
A or B
A= $boundaryB= $place
ABresult
1X1
011
000
961  
962 0 ▶ return ($osm_type eq 'way' ? 'W' : 'R') . $reply->{osm_id};
Branch: 1 branch
BranchTrueFalseTotal
$osm_type eq 'way' ? : 0 0 0
963 }
964  
965 # _reverse($lat, $lon, $zoom) -> the decoded reply, or undef when Nominatim
966 # did not answer with JSON.
967 0 ▶sub _reverse {
Subroutine _reverse: not called CC=7 SCAR=40.3
968 0 my ($self, $lat, $lon, $zoom) = @_;
969  
970 0 my $uri = URI->new($REVERSE_URL);
971 0 $uri->query_form(
972 lat => $lat,
973 lon => $lon,
974 zoom => $zoom,
975 format => 'jsonv2',
976 polygon_geojson => 1,
977 polygon_threshold => $self->_detail,
978 'accept-language' => 'en',
979 );
980  
981 0 require Database::BI::Model::Geocode;
982 0 ▶ my $ua = $self->{ua} // Database::BI::Model::Geocode::nominatim_ua();
Condition0%0/3
$$self{"ua"} // Database::BI::Model::Geocode::nominatim_ua()
l!l&&r!l&&!r
000
MC/DC0%0/2
$$self{"ua"} // Database::BI::Model::Geocode::nominatim_ua()
$$self{"ua"} Database::BI::Model::Geocode::nominatim_ua()
Truth table0%0/3
A // B
A= $$self{"ua"}B= Database::BI::Model::Geocode::nominatim_ua()
ABresult
1X1
011
000
983 0 my $res = eval { $ua->get($uri) };
984 0 ▶ return unless blessed($res) && $res->can('is_success') && $res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($res) and $res->can("is_success") and $res->is_success 0 0 0
Condition0%0/6
builtin::blessed($res) and $res->can("is_success") and $res->is_success
!ll&&!rl&&r
000
builtin::blessed($res) and $res->can("is_success")
!ll&&!rl&&r
000
MC/DC0%0/3
builtin::blessed($res) and $res->can("is_success") and $res->is_success
builtin::blessed($res) $res->can("is_success") $res->is_success
Truth table0%0/4
A and B and C
A= builtin::blessed($res)B= $res->can("is_success")C= $res->is_success
ABCresult
0XX0
10X0
1100
1111
985  
986 0 ▶ my $reply = eval { $json->decode($res->decoded_content // '') };
Condition0%0/2
$res->decoded_content // ""
l!l
00
MC/DC0%0/1
$res->decoded_content // ""
$res->decoded_content
Truth table0%0/2
A
A= $res->decoded_content
Aresult
11
00
987 0 ▶ return ref($reply) eq 'HASH' ? $reply : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $reply eq 'HASH' ? : 0 0 0
988 }
989  
990 # _polygons($geometry) -> list of polygons, each an arrayref of rings, each
991 # ring an arrayref of [lon, lat].
992 0 ▶sub _polygons {
Subroutine _polygons: not called CC=7 SCAR=40.3
993 0 my $geometry = shift;
994 0 ▶ return () unless ref($geometry) eq 'HASH' && ref($geometry->{coordinates}) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $geometry eq "HASH" and ref $$geometry{"coordinates"} eq "ARRAY" 0 0 0
Condition0%0/3
ref $geometry eq "HASH" and ref $$geometry{"coordinates"} eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $geometry eq "HASH" and ref $$geometry{"coordinates"} eq "ARRAY"
ref $geometry eq "HASH" ref $$geometry{"coordinates"} eq "ARRAY"
Truth table0%0/3
A and B
A= ref $geometry eq "HASH"B= ref $$geometry{"coordinates"} eq "ARRAY"
ABresult
0X0
100
111
995 0 ▶ return ($geometry->{coordinates}) if ($geometry->{type} // '') eq 'Polygon';
Branch: 1 branch
BranchTrueFalseTotal
if ($$geometry{"type"} // "") eq "Polygon" 0 0 0
Condition0%0/2
$$geometry{"type"} // ""
l!l
00
MC/DC0%0/1
$$geometry{"type"} // ""
$$geometry{"type"}
Truth table0%0/2
A
A= $$geometry{"type"}
Aresult
11
00
996 0 ▶ return @{ $geometry->{coordinates} } if ($geometry->{type} // '') eq 'MultiPolygon';
Branch: 1 branch
BranchTrueFalseTotal
if ($$geometry{"type"} // "") eq "MultiPolygon" 0 0 0
Condition0%0/2
$$geometry{"type"} // ""
l!l
00
MC/DC0%0/1
$$geometry{"type"} // ""
$$geometry{"type"}
Truth table0%0/2
A
A= $$geometry{"type"}
Aresult
11
00
997 0 return ();
998 }
999  
1000 # _bounds($geometry) -> [south, west, north, east], or undef with no points
1001 0 ▶sub _bounds {
Subroutine _bounds: not called CC=13 SCAR=52.0
1002 0 my $geometry = shift;
1003 0 my ($south, $west, $north, $east);
1004 0 for my $polygon (_polygons($geometry)) {
1005 0 ▶ for my $position (@{ $polygon->[0] // [] }) {
Condition0%0/2
$$polygon[0] // []
l!l
00
MC/DC0%0/1
$$polygon[0] // []
$$polygon[0]
Truth table0%0/2
A
A= $$polygon[0]
Aresult
11
00
1006 0 my ($lon, $lat) = @{$position};
1007 0 ▶ $south = $lat if !defined($south) || $lat < $south;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $south or $lat < $south 0 0 0
Condition0%0/3
not defined $south or $lat < $south
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $south or $lat < $south
not defined $south $lat < $south
Truth table0%0/3
not(A) or B
A= not defined $southB= $lat < $south
ABresult
1X1
011
000
1008 0 ▶ $north = $lat if !defined($north) || $lat > $north;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $north or $lat > $north 0 0 0
Condition0%0/3
not defined $north or $lat > $north
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $north or $lat > $north
not defined $north $lat > $north
Truth table0%0/3
not(A) or B
A= not defined $northB= $lat > $north
ABresult
1X1
011
000
1009 0 ▶ $west = $lon if !defined($west) || $lon < $west;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $west or $lon < $west 0 0 0
Condition0%0/3
not defined $west or $lon < $west
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $west or $lon < $west
not defined $west $lon < $west
Truth table0%0/3
not(A) or B
A= not defined $westB= $lon < $west
ABresult
1X1
011
000
1010 0 ▶ $east = $lon if !defined($east) || $lon > $east;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $east or $lon > $east 0 0 0
Condition0%0/3
not defined $east or $lon > $east
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $east or $lon > $east
not defined $east $lon > $east
Truth table0%0/3
not(A) or B
A= not defined $eastB= $lon > $east
ABresult
1X1
011
000
1011 }
1012 }
1013 0 ▶ return defined($south) ? [ $south, $west, $north, $east ] : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $south ? : 0 0 0
1014 }
1015  
1016 # _contains($geometry, $lat, $lon) -> 1 when the point is inside a polygon's
1017 # outer ring and none of its holes (Fairfax County's hole is the City of
1018 # Fairfax). Even-odd ray casting, with longitude as x.
1019 0 ▶sub _contains {
Subroutine _contains: not called CC=4 SCAR=30.0
1020 0 my ($geometry, $lat, $lon) = @_;
1021 0 for my $polygon (_polygons($geometry)) {
1022 0 my ($outer, @holes) = @{$polygon};
1023 0 ▶ next unless _in_ring($outer, $lon, $lat);
Branch: 1 branch
BranchTrueFalseTotal
unless _in_ring($outer, $lon, $lat) 0 0 0
1024 0 ▶ return 1 unless grep { _in_ring($_, $lon, $lat) } @holes;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {_in_ring($_, $lon, $lat);} @holes 0 0 0
1025 }
1026 0 return 0;
1027 }
1028  
1029 0 ▶sub _in_ring {
Subroutine _in_ring: not called CC=7 SCAR=40.3
1030 0 my ($ring, $x, $y) = @_;
1031 0 ▶ return 0 unless ref($ring) eq 'ARRAY' && @{$ring} >= 3;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $ring eq "ARRAY" and @{$ring;} >= 3 0 0 0
Condition0%0/3
ref $ring eq "ARRAY" and @{$ring;} >= 3
!ll&&!rl&&r
000
MC/DC0%0/2
ref $ring eq "ARRAY" and @{$ring;} >= 3
ref $ring eq "ARRAY" @{$ring;} >= 3
Truth table0%0/3
A and B
A= ref $ring eq "ARRAY"B= @{$ring;} >= 3
ABresult
0X0
100
111
1032 0 my $inside = 0;
1033 0 my ($px, $py) = @{ $ring->[-1] };
1034 0 for my $position (@{$ring}) {
1035 0 my ($qx, $qy) = @{$position};
1036 0 ▶ if ((($qy > $y) != ($py > $y))
Branch: 1 branch
BranchTrueFalseTotal
if ($qy > $y != $py > $y and $x < ($px - $qx) * ($y - $qy) / ($py - $qy) + $qx) 0 0 0
Condition0%0/3
$qy > $y != $py > $y and $x < ($px - $qx) * ($y - $qy) / ($py - $qy) + $qx
!ll&&!rl&&r
000
MC/DC0%0/2
$qy > $y != $py > $y and $x < ($px - $qx) * ($y - $qy) / ($py - $qy) + $qx
$qy > $y != $py > $y $x < ($px - $qx) * ($y - $qy) / ($py - $qy) + $qx
Truth table0%0/3
A and B
A= $qy > $y != $py > $yB= $x < ($px - $qx) * ($y - $qy) / ($py - $qy) + $qx
ABresult
0X0
100
111
1037 && ($x < ($px - $qx) * ($y - $qy) / ($py - $qy) + $qx)) {
1038 0 $inside = !$inside;
1039 }
1040 0 ($px, $py) = ($qx, $qy);
1041 }
1042 0 ▶ return $inside ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$inside ? : 0 0 0
1043 }
1044  
1045 1 1;
1046  
1047 __END__
« blib/lib/Database/BI/Controller/Dashboard.pm ↑ Summary blib/lib/Database/BI/Model/DataSource.pm »