SpatialDB.pm

filter: Statementstmt 37.1% 46 / 124 Branchbran 2.5% 1 / 40 Conditioncond 7.8% 3 / 38 MC/DCmcdc 0.0% 0 / 23 Subroutinesub 65.0% 13 / 20 Podpod 25.0% 1 / 4 totaltotal 25.7% 64 / 249 CC 44 SCAR 46.2
CC
44
Cov
25%
add_entity
306.0
_register_entity_within
56.0
find_in
56.0
CRAP 101.9
↑ Summary SpatialDB/Entity.pm »
1 package Geo::SpatialDB;
2 2 $Geo::SpatialDB::VERSION = '0.000_001'; # TRIAL
3  
4 2 ▶$Geo::SpatialDB::VERSION = '0.000001';use Moo 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Geo::SpatialDB::BBox;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Geo::SpatialDB::Location;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Geo::SpatialDB::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Geo::SpatialDB::RouteSegment;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Geo::SpatialDB::Route;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Geo::SpatialDB::Area;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Module::Runtime 'require_module';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Log::Any '$log';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 sub _croak { require Carp; goto &Carp::croak }
14 2 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 # ABSTRACT: Generic reverse-geocoding engine on top of key/value storage
17  
18  
19 0 ▶has zoom_levels => is => 'rw', default => sub { [
Subroutine __ANON__: not called CC=1 SCAR=6.9
20 # tiles per circle, microdegrees per tile
21 0 [ 360*4, int(1_000_000/4) ],
22 [ 360*32, int(1_000_000/32) ],
23 [ 360*128, int(1_000_000/128) ],
24 2 ] };
25 1 ▶has latlon_precision => is => 'rw', default => sub { 1_000_000 };
Subroutine __ANON__: called CC=1 SCAR=0.0
26 2 has storage => is => 'lazy', coerce => \&_build_storage;
27  
28 1 ▶sub _build_storage {
Subroutine _build_storage: called CC=7 SCAR=27.6
29 1 ▶ if (!$_[0] || ref($_[0]) eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (not $_[0] or ref $_[0] eq 'HASH') { } 1 0 1
elsif ($_[0] and ref($_[0])->can('get')) { } 0 0 0
Condition33%1/3
not $_[0] or ref $_[0] eq "HASH"
l!l&&r!l&&!r
010
MC/DC0%0/2
not $_[0] or ref $_[0] eq "HASH"
not $_[0] ref $_[0] eq "HASH"
Truth table33%1/3
not(A) or B
A= not $_[0]B= ref $_[0] eq "HASH"
ABresult
1X1
011
000
30 1 ▶ my %cfg= %{ $_[0] // {} };
Condition50%1/2
$_[0] // {}
l!l
10
MC/DC0%0/1
$_[0] // {}
$_[0]
Truth table50%1/2
A
A= $_[0]
Aresult
11
00
31 1 ▶ my $class= delete $cfg{CLASS} // 'LMDB_Storable';
Condition50%1/2
delete $cfg{"CLASS"} // "LMDB_Storable"
l!l
01
MC/DC0%0/1
delete $cfg{"CLASS"} // "LMDB_Storable"
delete $cfg{"CLASS"}
Truth table50%1/2
A
A= delete $cfg{"CLASS"}
Aresult
11
00
32 1 $class= "Geo::SpatialDB::Storage::$class";
33 1 require_module($class);
34 0 $class->new(%cfg);
35 }
36 ▶ elsif ($_[0] && ref($_[0])->can('get')) {
Condition0%0/3
$_[0] and ref($_[0])->can("get")
!ll&&!rl&&r
000
MC/DC0%0/2
$_[0] and ref($_[0])->can("get")
$_[0] ref($_[0])->can("get")
Truth table0%0/3
A and B
A= $_[0]B= ref($_[0])->can("get")
ABresult
0X0
100
111
37 0 $_[0]
38 } else {
39 0 _croak("Can't coerce $_[0] to Storage instance");
40 }
41 }
42  
43 0 ▶sub tile_for_lat_lon {
Subroutine tile_for_lat_lon: not called CC=3 SCAR=19.6
Pod undocumented
44 0 my ($self, $lat, $lon, $tile_udeg)= @_;
45 2 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 0 $lat= $lat % 360_000_000;
47 0 ▶ $lat += 360_000_000 if $lat < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $lat < 0 0 0 0
48 0 $lon= $lon % 360_000_000;
49 0 ▶ $lon += 360_000_000 if $lon < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $lon < 0 0 0 0
50 0 return ($lat / $tile_udeg, $lon / $tile_udeg);
51 }
52  
53 0 ▶sub _register_entity_within {
Subroutine _register_entity_within: not called CC=7 SCAR=40.3
54 0 my ($self, $ent, $lat0, $lon0, $lat1, $lon1)= @_;
55 0 my $stor= $self->storage;
56 # Convert radius to arc degrees
57 0 my $level= $#{ $self->zoom_levels };
58 0 ▶ $level-- while $level && ($lat1 - $lat0 > $self->zoom_levels->[$level][1]);
Branch: 1 branch
BranchTrueFalseTotal
$level and $lat1 - $lat0 > $self->zoom_levels->[$level][1] and do { --$level } 0 0 0
Condition0%0/3
$level and $lat1 - $lat0 > $self->zoom_levels->[$level][1]
!ll&&!rl&&r
000
MC/DC0%0/2
$level and $lat1 - $lat0 > $self->zoom_levels->[$level][1]
$level $lat1 - $lat0 > $self->zoom_levels->[$level][1]
Truth table0%0/3
A and B
A= $levelB= $lat1 - $lat0 > $self->zoom_levels->[$level][1]
ABresult
0X0
100
111
59 0 my ($tile_per_circle, $tile_udeg)= @{ $self->zoom_levels->[$level] };
60 0 my ($lat_key_0, $lon_key_0)= $self->tile_for_lat_lon($lat0, $lon0, $tile_udeg);
61 0 my ($lat_key_1, $lon_key_1)= $self->tile_for_lat_lon($lat1-1, $lon1-1, $tile_udeg);
62
63 # TODO: correctly handle wrap-around at lon=0, and edge cases at the poles
64 # or, choose an entirely different bucket layout
65 0 for my $lat_k ($lat_key_0 .. $lat_key_1) {
66 0 for my $lon_k ($lon_key_0 .. $lon_key_1) {
67 # Load detail node, add new entity ref, and save detail node
68 0 my $bucket_key= ":$level,$lat_k,$lon_k";
69 0 ▶ my $bucket= $stor->get($bucket_key) // {};
Condition0%0/2
$stor->get($bucket_key) // {}
l!l
00
MC/DC0%0/1
$stor->get($bucket_key) // {}
$stor->get($bucket_key)
Truth table0%0/2
A
A= $stor->get($bucket_key)
Aresult
11
00
70 0 my %seen;
71 0 ▶ $bucket->{ent}= [ grep { !$seen{$_}++ } @{ $bucket->{ent}//[] }, $ent->id ];
Condition0%0/2
$bucket->{"ent"} // []
l!l
00
MC/DC0%0/1
$bucket->{"ent"} // []
$bucket->{"ent"}
Truth table0%0/2
A
A= $bucket->{"ent"}
Aresult
11
00
72 0 $stor->put($bucket_key, $bucket);
73 }
74 }
75 }
76  
77 0 ▶sub add_entity {
Subroutine add_entity: not called CC=17 SCAR=57.2
Pod undocumented
78 0 my ($self, $e)= @_;
79 # If it's a location, index the point. Use radius to determine what level to include it in.
80 0 ▶ if ($e->isa('Geo::SpatialDB::Location')) {
Branch: 3 branches
BranchTrueFalseTotal
if ($e->isa('Geo::SpatialDB::Location')) { } 0 0 0
elsif ($e->isa('Geo::SpatialDB::RouteSegment')) { } 0 0 0
elsif ($e->isa('Geo::SpatialDB::Route')) { } 0 0 0
81 0 ▶ my ($lat, $lon, $rad)= ($e->lat, $e->lon, $e->rad//0);
Condition0%0/2
$e->rad // 0
l!l
00
MC/DC0%0/1
$e->rad // 0
$e->rad
Truth table0%0/2
A
A= $e->rad
Aresult
11
00
82 # Convert radius to lat arc degrees and lon arc degrees
83 0 ▶ my $dLat= $rad? ($rad / 111000 * $self->latlon_precision) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$rad ? : 0 0 0
84 # Longitude is affected by latitude
85 0 ▶ my $dLon= $rad? ($rad / (111699 * cos($lat / (360*$self->latlon_precision)))) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$rad ? : 0 0 0
86 0 $self->storage->put($e->id, $e);
87 0 $self->_register_entity_within($e, $lat - $dLat, $lon - $dLon, $lat + $dLat, $lon + $dLon);
88 }
89 elsif ($e->isa('Geo::SpatialDB::RouteSegment')) {
90 0 ▶ unless (@{ $e->path }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$e->path;}) 0 0 0
91 0 $log->warn("RouteSegment with zero-length path...");
92 }
93 0 my ($lat0, $lon0, $lat1, $lon1);
94 0 for my $pt (@{ $e->path }) {
95 0 ▶ $lat0= $pt->[0] if !defined $lat0 or $lat0 > $pt->[0];
Branch: 1 branch
BranchTrueFalseTotal
if not defined $lat0 or $lat0 > $pt->[0] 0 0 0
Condition0%0/3
not defined $lat0 or $lat0 > $pt->[0]
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $lat0 or $lat0 > $pt->[0]
not defined $lat0 $lat0 > $pt->[0]
Truth table0%0/3
not(A) or B
A= not defined $lat0B= $lat0 > $pt->[0]
ABresult
1X1
011
000
96 0 ▶ $lat1= $pt->[0] if !defined $lat1 or $lat1 < $pt->[0];
Branch: 1 branch
BranchTrueFalseTotal
if not defined $lat1 or $lat1 < $pt->[0] 0 0 0
Condition0%0/3
not defined $lat1 or $lat1 < $pt->[0]
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $lat1 or $lat1 < $pt->[0]
not defined $lat1 $lat1 < $pt->[0]
Truth table0%0/3
not(A) or B
A= not defined $lat1B= $lat1 < $pt->[0]
ABresult
1X1
011
000
97 0 ▶ $lon0= $pt->[1] if !defined $lon0 or $lon0 > $pt->[1];
Branch: 1 branch
BranchTrueFalseTotal
if not defined $lon0 or $lon0 > $pt->[1] 0 0 0
Condition0%0/3
not defined $lon0 or $lon0 > $pt->[1]
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $lon0 or $lon0 > $pt->[1]
not defined $lon0 $lon0 > $pt->[1]
Truth table0%0/3
not(A) or B
A= not defined $lon0B= $lon0 > $pt->[1]
ABresult
1X1
011
000
98 0 ▶ $lon1= $pt->[1] if !defined $lon1 or $lon1 < $pt->[1];
Branch: 1 branch
BranchTrueFalseTotal
if not defined $lon1 or $lon1 < $pt->[1] 0 0 0
Condition0%0/3
not defined $lon1 or $lon1 < $pt->[1]
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $lon1 or $lon1 < $pt->[1]
not defined $lon1 $lon1 < $pt->[1]
Truth table0%0/3
not(A) or B
A= not defined $lon1B= $lon1 < $pt->[1]
ABresult
1X1
011
000
99 }
100 0 $self->storage->put($e->id, $e);
101 0 $self->_register_entity_within($e, $lat0, $lon0, $lat1, $lon1);
102 }
103 elsif ($e->isa('Geo::SpatialDB::Route')) {
104 # Routes don't get added to positional buckets. Just their segments.
105 0 $self->storage->put($e->id, $e);
106 }
107 else {
108 0 $log->warn("Ignoring entity ".$e->id);
109 }
110 }
111  
112 # min_rad - the minimum radius (meters) of object that we care to see
113 0 ▶sub _get_bucket_keys_for_area {
Subroutine _get_bucket_keys_for_area: not called CC=6 SCAR=37.4
114 0 my ($self, $bbox, $min_dLat)= @_;
115 0 my @keys;
116 0 ▶ $log->debugf(" sw %d,%d ne %d,%d min arc %d",
Branch: 1 branch
BranchTrueFalseTotal
if $Geo::SpatialDB::log->is_debug 0 0 0
117 $bbox->lat0,$bbox->lon0, $bbox->lat1,$bbox->lon1, $min_dLat)
118 if $log->is_debug;
119  
120 0 for my $level (0 .. $#{ $self->zoom_levels }) {
121 0 my ($tile_per_circle, $tile_udeg)= @{ $self->zoom_levels->[$level] };
122 0 ▶ last if $tile_udeg < $min_dLat;
Branch: 1 branch
BranchTrueFalseTotal
if $tile_udeg < $min_dLat 0 0 0
123 # Iterate south to north, west to east
124 0 my ($lat_key_0, $lon_key_0)= $self->tile_for_lat_lon($bbox->lat0, $bbox->lon0, $tile_udeg);
125 0 my ($lat_key_1, $lon_key_1)= $self->tile_for_lat_lon($bbox->lat1-1, $bbox->lon1-1, $tile_udeg);
126 # TODO: correctly handle wrap-around at lon=0, and edge cases at the poles
127 # or, choose an entirely different bucket layout
128 0 for my $lat_key ($lat_key_0 .. $lat_key_1) {
129 push @keys, ":$level,$lat_key,$_"
130 0 for $lon_key_0 .. $lon_key_1;
131 }
132 }
133 0 return @keys;
134 }
135  
136  
137 0 ▶sub find_at {
Subroutine find_at: not called CC=3 SCAR=24.8
Pod documented
138 0 my ($self, $lat, $lon, $radius, $filter)= @_;
139 # Convert radius to lat arc degrees and lon arc degrees
140 0 ▶ my $dLat= $radius? ($radius / 111000 * $self->latlon_precision) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$radius ? : 0 0 0
141 # Longitude is affected by latitude
142 0 ▶ my $dLon= $radius? ($radius / (111699 * cos($lat / (360*$self->latlon_precision)))) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$radius ? : 0 0 0
143 0 $self->find_in(
144 Geo::SpatialDB::BBox->new($lat-$dLat, $lon-$dLon, $lat+$dLat, $lon+$dLon),
145 $dLat/200
146 );
147 }
148  
149 0 ▶sub find_in {
Subroutine find_in: not called CC=7 SCAR=40.3
Pod undocumented
150 0 my ($self, $bbox, $min_arc)= @_;
151 0 $bbox= Geo::SpatialDB::BBox->coerce($bbox);
152 0 ▶ $min_arc //= $bbox->dLat/200;
Condition0%0/2
$min_arc //= $bbox->dLat / 200
l!l
00
MC/DC0%0/1
$min_arc //= $bbox->dLat / 200
$min_arc
Truth table0%0/2
A
A= $min_arc
Aresult
11
00
153 0 my @keys= $self->_get_bucket_keys_for_area($bbox, $min_arc);
154 0 my %result= ( bbox => $bbox->clone );
155 0 $log->debugf(" searching buckets: %s", \@keys);
156 0 for (@keys) {
157 0 ▶ my $bucket= $self->storage->get($_)
Branch: 1 branch
BranchTrueFalseTotal
my $bucket = $self->storage->get($_) or next 0 0 0
158 or next;
159 0 ▶ for (@{ $bucket->{ent} // [] }) {
Condition0%0/2
$bucket->{"ent"} // []
l!l
00
MC/DC0%0/1
$bucket->{"ent"} // []
$bucket->{"ent"}
Truth table0%0/2
A
A= $bucket->{"ent"}
Aresult
11
00
160 0 ▶ $result{entities}{$_} //= $self->storage->get($_);
Condition0%0/3
$result{"entities"}{$_} //= $self->storage->get($_)
l!l&&r!l&&!r
000
MC/DC0%0/2
$result{"entities"}{$_} //= $self->storage->get($_)
$result{"entities"}{$_} $self->storage->get($_)
Truth table0%0/3
A //= B
A= $result{"entities"}{$_}B= $self->storage->get($_)
ABresult
1X1
011
000
161 }
162 }
163 0 \%result;
164 }
165  
166 2 1;
167  
168 __END__
↑ Summary SpatialDB/Entity.pm »