| 1 | package Geo::Distance; | ||||||||||||||||||||||||||||
| 2 | 4 | ▶ | use 5.008001; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 4 | our $VERSION = '0.24'; | |||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | =encoding utf8 | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | =head1 NAME | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | Geo::Distance - Calculate distances and closest locations. (DEPRECATED) | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | use Geo::Distance; | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | my $geo = new Geo::Distance; | ||||||||||||||||||||||||||||
| 18 | $geo->formula('hsin'); | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | $geo->reg_unit( 'toad_hop', 200120 ); | ||||||||||||||||||||||||||||
| 21 | $geo->reg_unit( 'frog_hop' => 6 => 'toad_hop' ); | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | my $distance = $geo->distance( 'unit_type', $lon1,$lat1 => $lon2,$lat2 ); | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | my $locations = $geo->closest( | ||||||||||||||||||||||||||||
| 26 | dbh => $dbh, | ||||||||||||||||||||||||||||
| 27 | table => $table, | ||||||||||||||||||||||||||||
| 28 | lon => $lon, | ||||||||||||||||||||||||||||
| 29 | lat => $lat, | ||||||||||||||||||||||||||||
| 30 | unit => $unit_type, | ||||||||||||||||||||||||||||
| 31 | distance => $dist_in_unit | ||||||||||||||||||||||||||||
| 32 | ); | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | This perl library aims to provide as many tools to make it as simple as possible to calculate | ||||||||||||||||||||||||||||
| 37 | distances between geographic points, and anything that can be derived from that. Currently | ||||||||||||||||||||||||||||
| 38 | there is support for finding the closest locations within a specified distance, to find the | ||||||||||||||||||||||||||||
| 39 | closest number of points to a specified point, and to do basic point-to-point distance | ||||||||||||||||||||||||||||
| 40 | calculations. | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | =head1 DEPRECATED | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | This module has been gutted and is now a wrapper around L<GIS::Distance>, please | ||||||||||||||||||||||||||||
| 45 | use that module instead. | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | When switching from this module to L<GIS::Distance> make sure you reverse the | ||||||||||||||||||||||||||||
| 48 | coordinates when passing them to L<GIS::Distance/distance>. GIS::Distance takes | ||||||||||||||||||||||||||||
| 49 | lat/lon pairs while Geo::Distance takes lon/lat pairs. | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | =head1 STABILITY | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | The interface to Geo::Distance is fairly stable nowadays. If this changes it | ||||||||||||||||||||||||||||
| 54 | will be noted here. | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | C<0.21> - All distance calculations are now handled by L<GIS::Distance>. | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | C<0.10> - The closest() method has a changed argument syntax and no longer supports array searches. | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | C<0.09> - Changed the behavior of the reg_unit function. | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | C<0.07> - OO only, and other changes all over. | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | =cut | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 4 | ▶ | use GIS::Distance; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 67 | 4 | ▶ | use GIS::Distance::Constants qw( :all ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 68 | 4 | ▶ | use Carp qw( croak ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 69 | 4 | ▶ | use Const::Fast; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | =head1 FORMULAS | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | C<alt> - See L<GIS::Distance::ALT>. | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | C<cos> - See L<GIS::Distance::Cosine>. | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | C<gcd> - See L<GIS::Distance::GreatCircle>. | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | C<hsin> - See L<GIS::Distance::Haversine>. | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | C<mt> - See L<GIS::Distance::MathTrig>. | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | C<null> - See L<GIS::Distance::Null>. | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | C<polar> - See L<GIS::Distance::Polar>. | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | C<tv> - See L<GIS::Distance::Vincenty>. | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | =cut | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 4 | const our %GEO_TO_GIS_FORMULA_MAP => (qw( | |||||||||||||||||||||||||||
| 92 | alt ALT | ||||||||||||||||||||||||||||
| 93 | cos Cosine | ||||||||||||||||||||||||||||
| 94 | gcd GreatCircle | ||||||||||||||||||||||||||||
| 95 | hsin Haversine | ||||||||||||||||||||||||||||
| 96 | mt MathTrig | ||||||||||||||||||||||||||||
| 97 | null Null | ||||||||||||||||||||||||||||
| 98 | polar Polar | ||||||||||||||||||||||||||||
| 99 | tv Vincenty | ||||||||||||||||||||||||||||
| 100 | )); | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 4 | const our @FORMULAS => (keys %GEO_TO_GIS_FORMULA_MAP); | |||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | =head1 PROPERTIES | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | =head2 UNITS | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | All functions accept a unit type to do the computations of distance with. By default no units | ||||||||||||||||||||||||||||
| 109 | are defined in a Geo::Distance object. You can add units with reg_unit() or create some default | ||||||||||||||||||||||||||||
| 110 | units with default_units(). | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | =head2 LATITUDE AND LONGITUDE | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | When a function needs a longitude and latitude, they must always be in decimal degree format. | ||||||||||||||||||||||||||||
| 115 | Here is some sample code for converting from other formats to decimal: | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | # DMS to Decimal | ||||||||||||||||||||||||||||
| 118 | my $decimal = $degrees + ($minutes/60) + ($seconds/3600); | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | # Precision Six Integer to Decimal | ||||||||||||||||||||||||||||
| 121 | my $decimal = $integer * .000001; | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | If you want to convert from decimal radians to degrees you can use Math::Trig's rad2deg function. | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | =head1 METHODS | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | =head2 new | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | my $geo = new Geo::Distance; | ||||||||||||||||||||||||||||
| 130 | my $geo = new Geo::Distance( no_units=>1 ); | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | Returns a blessed Geo::Distance object. The new constructor accepts one optional | ||||||||||||||||||||||||||||
| 133 | argument. | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | no_units - Whether or not to load the default units. Defaults to 0 (false). | ||||||||||||||||||||||||||||
| 136 | kilometer, kilometre, meter, metre, centimeter, centimetre, millimeter, | ||||||||||||||||||||||||||||
| 137 | millimetre, yard, foot, inch, light second, mile, nautical mile, | ||||||||||||||||||||||||||||
| 138 | poppy seed, barleycorn, rod, pole, perch, chain, furlong, league, | ||||||||||||||||||||||||||||
| 139 | fathom | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | =cut | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | sub new { | ||||||||||||||||||||||||||||
| 144 | 4 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 145 | 4 | my $self = bless {}, $class; | |||||||||||||||||||||||||||
| 146 | 4 | my %args = @_; | |||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 4 | $self->{formula} = 'hsin'; | |||||||||||||||||||||||||||
| 149 | 4 | $self->{units} = {}; | |||||||||||||||||||||||||||
| 150 | 4 | ▶ | if(!$args{no_units}){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | 4 | $self->reg_unit( $KILOMETER_RHO, 'kilometer' ); | |||||||||||||||||||||||||||
| 152 | 4 | $self->reg_unit( 1000, 'meter', => 'kilometer' ); | |||||||||||||||||||||||||||
| 153 | 4 | $self->reg_unit( 100, 'centimeter' => 'meter' ); | |||||||||||||||||||||||||||
| 154 | 4 | $self->reg_unit( 10, 'millimeter' => 'centimeter' ); | |||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 4 | $self->reg_unit( 'kilometre' => 'kilometer' ); | |||||||||||||||||||||||||||
| 157 | 4 | $self->reg_unit( 'metre' => 'meter' ); | |||||||||||||||||||||||||||
| 158 | 4 | $self->reg_unit( 'centimetre' => 'centimeter' ); | |||||||||||||||||||||||||||
| 159 | 4 | $self->reg_unit( 'millimetre' => 'millimeter' ); | |||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 4 | $self->reg_unit( 'mile' => 1609.344, 'meter' ); | |||||||||||||||||||||||||||
| 162 | 4 | $self->reg_unit( 'nautical mile' => 1852, 'meter' ); | |||||||||||||||||||||||||||
| 163 | 4 | $self->reg_unit( 'yard' => 0.9144, 'meter' ); | |||||||||||||||||||||||||||
| 164 | 4 | $self->reg_unit( 3, 'foot' => 'yard' ); | |||||||||||||||||||||||||||
| 165 | 4 | $self->reg_unit( 12, 'inch' => 'foot' ); | |||||||||||||||||||||||||||
| 166 | 4 | $self->reg_unit( 'light second' => 299792458, 'meter' ); | |||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 4 | $self->reg_unit( 'poppy seed' => 2.11, 'millimeter' ); | |||||||||||||||||||||||||||
| 169 | 4 | $self->reg_unit( 'barleycorn' => 8.467, 'millimeter' ); | |||||||||||||||||||||||||||
| 170 | 4 | $self->reg_unit( 'rod' => 5.0292, 'meter' ); | |||||||||||||||||||||||||||
| 171 | 4 | $self->reg_unit( 'pole' => 'rod' ); | |||||||||||||||||||||||||||
| 172 | 4 | $self->reg_unit( 'perch' => 'rod' ); | |||||||||||||||||||||||||||
| 173 | 4 | $self->reg_unit( 'chain' => 20.1168, 'meter' ); | |||||||||||||||||||||||||||
| 174 | 4 | $self->reg_unit( 'furlong' => 201.168, 'meter' ); | |||||||||||||||||||||||||||
| 175 | 4 | $self->reg_unit( 'league' => 4.828032, 'kilometer' ); | |||||||||||||||||||||||||||
| 176 | 4 | $self->reg_unit( 1.8288, 'fathom' => 'meter' ); | |||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 4 | return $self; | |||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | =head2 formula | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | if($geo->formula eq 'hsin'){ ... } | ||||||||||||||||||||||||||||
| 185 | $geo->formula('cos'); | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | Allows you to retrieve and set the formula that is currently being used to | ||||||||||||||||||||||||||||
| 188 | calculate distances. See the available L</FORMULAS>. | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | C<hsin> is the default. Both C<mt> and C<cos> are inferior in speed | ||||||||||||||||||||||||||||
| 191 | and accuracy to C<hsin>. | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | =cut | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | sub formula { | ||||||||||||||||||||||||||||
| 196 | 2 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
formula: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | 2 | ▶ | return $self->{formula} if !$_[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 199 | 2 | my $formula = shift; | |||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | 2 | my $gis_formula = $GEO_TO_GIS_FORMULA_MAP{ $formula }; | |||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | 2 | ▶ | croak( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 204 | 'Unknown formula (available formulas are ', | ||||||||||||||||||||||||||||
| 205 | join(', ', sort @FORMULAS), | ||||||||||||||||||||||||||||
| 206 | ')', | ||||||||||||||||||||||||||||
| 207 | ) if !$gis_formula; | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | 2 | $self->{formula} = $formula; | |||||||||||||||||||||||||||
| 210 | 2 | $self->{gis_formula} = $gis_formula; | |||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | 2 | return $formula; | |||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | =head2 reg_unit | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | $geo->reg_unit( $radius, $key ); | ||||||||||||||||||||||||||||
| 218 | $geo->reg_unit( $key1 => $key2 ); | ||||||||||||||||||||||||||||
| 219 | $geo->reg_unit( $count1, $key1 => $key2 ); | ||||||||||||||||||||||||||||
| 220 | $geo->reg_unit( $key1 => $count2, $key2 ); | ||||||||||||||||||||||||||||
| 221 | $geo->reg_unit( $count1, $key1 => $count2, $key2 ); | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | This method is used to create custom unit types. There are several ways of calling it, | ||||||||||||||||||||||||||||
| 224 | depending on if you are defining the unit from scratch, or if you are basing it off | ||||||||||||||||||||||||||||
| 225 | of an existing unit (such as saying 12 inches = 1 foot ). When defining a unit from | ||||||||||||||||||||||||||||
| 226 | scratch you pass the name and rho (radius of the earth in that unit) value. | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | So, if you wanted to do your calculations in human adult steps you would have to have an | ||||||||||||||||||||||||||||
| 229 | average human adult walk from the crust of the earth to the core (ignore the fact that | ||||||||||||||||||||||||||||
| 230 | this is impossible). So, assuming we did this and we came up with 43,200 steps, you'd | ||||||||||||||||||||||||||||
| 231 | do something like the following. | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | # Define adult step unit. | ||||||||||||||||||||||||||||
| 234 | $geo->reg_unit( 43200, 'adult step' ); | ||||||||||||||||||||||||||||
| 235 | # This can be read as "It takes 43,200 adult_steps to walk the radius of the earth". | ||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | Now, if you also wanted to do distances in baby steps you might think "well, now I | ||||||||||||||||||||||||||||
| 238 | gotta get a baby to walk to the center of the earth". But, you don't have to! If you do some | ||||||||||||||||||||||||||||
| 239 | research you'll find (no research was actually conducted) that there are, on average, | ||||||||||||||||||||||||||||
| 240 | 4.7 baby steps in each adult step. | ||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | # Define baby step unit. | ||||||||||||||||||||||||||||
| 243 | $geo->reg_unit( 4.7, 'baby step' => 'adult step' ); | ||||||||||||||||||||||||||||
| 244 | # This can be read as "4.7 baby steps is the same as one adult step". | ||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | And if we were doing this in reverse and already had the baby step unit but not | ||||||||||||||||||||||||||||
| 247 | the adult step, you would still use the exact same syntax as above. | ||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | =cut | ||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | sub reg_unit { | ||||||||||||||||||||||||||||
| 252 | 92 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
reg_unit: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||
| 253 | 92 | my $units = $self->{units}; | |||||||||||||||||||||||||||
| 254 | 92 | my($count1,$key1,$count2,$key2); | |||||||||||||||||||||||||||
| 255 | 92 | $count1 = shift; | |||||||||||||||||||||||||||
| 256 | 92 | ▶ | if($count1=~/[^\.0-9]/ or !@_){ $key1=$count1; $count1=1; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$count1 =~ /[^\.0-9]/ or not @_
MC/DC50%1/2
$count1 =~ /[^\.0-9]/ or not @_
$count1 =~ /[^\.0-9]/
not @_
Truth table66%2/3
A or not(B)
A= $count1 =~ /[^\.0-9]/B= not @_
| |||||||||||||||||||||||||||||
| 257 | 28 | else{ $key1 = shift; } | |||||||||||||||||||||||||||
| 258 | 92 | ▶ | if(!@_){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 259 | 4 | $units->{$key1} = $count1; | |||||||||||||||||||||||||||
| 260 | }else{ | ||||||||||||||||||||||||||||
| 261 | 88 | $count2 = shift; | |||||||||||||||||||||||||||
| 262 | 88 | ▶ | if($count2=~/[^\.0-9]/ or !@_){ $key2=$count2; $count2=1; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$count2 =~ /[^\.0-9]/ or not @_
MC/DC50%1/2
$count2 =~ /[^\.0-9]/ or not @_
$count2 =~ /[^\.0-9]/
not @_
Truth table66%2/3
A or not(B)
A= $count2 =~ /[^\.0-9]/B= not @_
| |||||||||||||||||||||||||||||
| 263 | 40 | else{ $key2 = shift; } | |||||||||||||||||||||||||||
| 264 | 88 | ▶ | ($key1,$key2) = ($key2,$key1) if( defined $units->{$key1} ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 265 | 88 | $units->{$key1} = ($units->{$key2}*$count1) / $count2; | |||||||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||||||
| 267 | } | ||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||
| 269 | =head2 distance | ||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||
| 271 | my $distance = $geo->distance( 'unit_type', $lon1,$lat1 => $lon2,$lat2 ); | ||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||
| 273 | Calculates the distance between two lon/lat points. | ||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | =cut | ||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | sub distance { | ||||||||||||||||||||||||||||
| 278 | 39 | ▶ | my ($self, $unit, $lon1, $lat1, $lon2, $lat2) = @_; | ||||||||||||||||||||||||||
|
Subroutine
distance: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | 39 | my $unit_rho = $self->{units}->{$unit}; | |||||||||||||||||||||||||||
| 281 | 39 | ▶ | croak('Unkown unit type "' . $unit . '"') if !$unit_rho; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||
| 283 | 39 | my $gis = GIS::Distance->new( $self->{gis_formula} ); | |||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | # Reverse lon/lat to lat/lon, the way GIS::Distance wants it. | ||||||||||||||||||||||||||||
| 286 | 39 | my $km = $gis->{code}->( $lat1, $lon1, $lat2, $lon2 ); | |||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||
| 288 | 39 | return $km * ($unit_rho / $KILOMETER_RHO); | |||||||||||||||||||||||||||
| 289 | } | ||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||
| 291 | 4 | ▶ | use Math::Trig qw( acos asin atan deg2rad great_circle_distance pi tan ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||
| 293 | sub old_distance { | ||||||||||||||||||||||||||||
| 294 | 0 | ▶ | my($self,$unit,$lon1,$lat1,$lon2,$lat2) = @_; | ||||||||||||||||||||||||||
|
Subroutine
old_distance: not called CC=12 SCAR=50.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 295 | 0 | ▶ | croak('Unkown unit type "'.$unit.'"') unless($unit = $self->{units}->{$unit}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||
| 297 | 0 | ▶ | return 0 if $self->{formula} eq 'null'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||
| 299 | 0 | ▶ | if($self->{formula} eq 'mt'){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 300 | 0 | return great_circle_distance( | |||||||||||||||||||||||||||
| 301 | deg2rad($lon1), | ||||||||||||||||||||||||||||
| 302 | deg2rad(90 - $lat1), | ||||||||||||||||||||||||||||
| 303 | deg2rad($lon2), | ||||||||||||||||||||||||||||
| 304 | deg2rad(90 - $lat2), | ||||||||||||||||||||||||||||
| 305 | $unit | ||||||||||||||||||||||||||||
| 306 | ); | ||||||||||||||||||||||||||||
| 307 | } | ||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||
| 309 | 0 | $lon1 = deg2rad($lon1); $lat1 = deg2rad($lat1); | |||||||||||||||||||||||||||
| 310 | 0 | $lon2 = deg2rad($lon2); $lat2 = deg2rad($lat2); | |||||||||||||||||||||||||||
| 311 | 0 | my $c; | |||||||||||||||||||||||||||
| 312 | 0 | ▶ | if($self->{formula} eq 'cos'){ | ||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||
| 313 | 0 | my $a = sin($lat1) * sin($lat2); | |||||||||||||||||||||||||||
| 314 | 0 | my $b = cos($lat1) * cos($lat2) * cos($lon2 - $lon1); | |||||||||||||||||||||||||||
| 315 | 0 | $c = acos($a + $b); | |||||||||||||||||||||||||||
| 316 | } | ||||||||||||||||||||||||||||
| 317 | elsif($self->{formula} eq 'hsin'){ | ||||||||||||||||||||||||||||
| 318 | 0 | my $dlon = $lon2 - $lon1; | |||||||||||||||||||||||||||
| 319 | 0 | my $dlat = $lat2 - $lat1; | |||||||||||||||||||||||||||
| 320 | 0 | my $a = (sin($dlat/2)) ** 2 + cos($lat1) * cos($lat2) * (sin($dlon/2)) ** 2; | |||||||||||||||||||||||||||
| 321 | 0 | $c = 2 * atan2(sqrt($a), sqrt(abs(1-$a))); | |||||||||||||||||||||||||||
| 322 | } | ||||||||||||||||||||||||||||
| 323 | elsif($self->{formula} eq 'polar'){ | ||||||||||||||||||||||||||||
| 324 | 0 | my $a = pi/2 - $lat1; | |||||||||||||||||||||||||||
| 325 | 0 | my $b = pi/2 - $lat2; | |||||||||||||||||||||||||||
| 326 | 0 | $c = sqrt( $a ** 2 + $b ** 2 - 2 * $a * $b * cos($lon2 - $lon1) ); | |||||||||||||||||||||||||||
| 327 | } | ||||||||||||||||||||||||||||
| 328 | elsif($self->{formula} eq 'gcd'){ | ||||||||||||||||||||||||||||
| 329 | 0 | $c = 2*asin( sqrt( | |||||||||||||||||||||||||||
| 330 | ( sin(($lat1-$lat2)/2) )**2 + | ||||||||||||||||||||||||||||
| 331 | cos($lat1) * cos($lat2) * | ||||||||||||||||||||||||||||
| 332 | ( sin(($lon1-$lon2)/2) )**2 | ||||||||||||||||||||||||||||
| 333 | ) ); | ||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||
| 335 | # Eric Samuelson recommended this formula. | ||||||||||||||||||||||||||||
| 336 | # http://forums.devshed.com/t54655/sc3d021a264676b9b440ea7cbe1f775a1.html | ||||||||||||||||||||||||||||
| 337 | # http://williams.best.vwh.net/avform.htm | ||||||||||||||||||||||||||||
| 338 | # It seems to produce the same results at the hsin formula, so... | ||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||
| 340 | #my $dlon = $lon2 - $lon1; | ||||||||||||||||||||||||||||
| 341 | #my $dlat = $lat2 - $lat1; | ||||||||||||||||||||||||||||
| 342 | #my $a = (sin($dlat / 2)) ** 2 | ||||||||||||||||||||||||||||
| 343 | # + cos($lat1) * cos($lat2) * (sin($dlon / 2)) ** 2; | ||||||||||||||||||||||||||||
| 344 | #$c = 2 * atan2(sqrt($a), sqrt(1 - $a)); | ||||||||||||||||||||||||||||
| 345 | } | ||||||||||||||||||||||||||||
| 346 | elsif($self->{formula} eq 'tv'){ | ||||||||||||||||||||||||||||
| 347 | 0 | my($a,$b,$f) = (6378137,6356752.3142,1/298.257223563); | |||||||||||||||||||||||||||
| 348 | 0 | my $l = $lon2 - $lon1; | |||||||||||||||||||||||||||
| 349 | 0 | my $u1 = atan((1-$f) * tan($lat1)); | |||||||||||||||||||||||||||
| 350 | 0 | my $u2 = atan((1-$f) * tan($lat2)); | |||||||||||||||||||||||||||
| 351 | 0 | my $sin_u1 = sin($u1); my $cos_u1 = cos($u1); | |||||||||||||||||||||||||||
| 352 | 0 | my $sin_u2 = sin($u2); my $cos_u2 = cos($u2); | |||||||||||||||||||||||||||
| 353 | 0 | my $lambda = $l; | |||||||||||||||||||||||||||
| 354 | 0 | my $lambda_pi = 2 * pi; | |||||||||||||||||||||||||||
| 355 | 0 | my $iter_limit = 20; | |||||||||||||||||||||||||||
| 356 | 0 | my($cos_sq_alpha,$sin_sigma,$cos2sigma_m,$cos_sigma,$sigma); | |||||||||||||||||||||||||||
| 357 | 0 | ▶ | while( abs($lambda-$lambda_pi) > 1e-12 && --$iter_limit>0 ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
abs $lambda - $lambda_pi > "1e-12" and --$iter_limit > 0
MC/DC0%0/2
abs $lambda - $lambda_pi > "1e-12" and --$iter_limit > 0
abs $lambda - $lambda_pi > "1e-12"
--$iter_limit > 0
Truth table0%0/3
A and B
A= abs $lambda - $lambda_pi > "1e-12"B= --$iter_limit > 0
| |||||||||||||||||||||||||||||
| 358 | 0 | my $sin_lambda = sin($lambda); my $cos_lambda = cos($lambda); | |||||||||||||||||||||||||||
| 359 | 0 | $sin_sigma = sqrt(($cos_u2*$sin_lambda) * ($cos_u2*$sin_lambda) + | |||||||||||||||||||||||||||
| 360 | ($cos_u1*$sin_u2-$sin_u1*$cos_u2*$cos_lambda) * ($cos_u1*$sin_u2-$sin_u1*$cos_u2*$cos_lambda)); | ||||||||||||||||||||||||||||
| 361 | 0 | $cos_sigma = $sin_u1*$sin_u2 + $cos_u1*$cos_u2*$cos_lambda; | |||||||||||||||||||||||||||
| 362 | 0 | $sigma = atan2($sin_sigma, $cos_sigma); | |||||||||||||||||||||||||||
| 363 | 0 | my $alpha = asin($cos_u1 * $cos_u2 * $sin_lambda / $sin_sigma); | |||||||||||||||||||||||||||
| 364 | 0 | $cos_sq_alpha = cos($alpha) * cos($alpha); | |||||||||||||||||||||||||||
| 365 | 0 | $cos2sigma_m = $cos_sigma - 2*$sin_u1*$sin_u2/$cos_sq_alpha; | |||||||||||||||||||||||||||
| 366 | 0 | my $cc = $f/16*$cos_sq_alpha*(4+$f*(4-3*$cos_sq_alpha)); | |||||||||||||||||||||||||||
| 367 | 0 | $lambda_pi = $lambda; | |||||||||||||||||||||||||||
| 368 | 0 | $lambda = $l + (1-$cc) * $f * sin($alpha) * | |||||||||||||||||||||||||||
| 369 | ($sigma + $cc*$sin_sigma*($cos2sigma_m+$cc*$cos_sigma*(-1+2*$cos2sigma_m*$cos2sigma_m))); | ||||||||||||||||||||||||||||
| 370 | } | ||||||||||||||||||||||||||||
| 371 | 0 | ▶ | undef if( $iter_limit==0 ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 372 | 0 | my $usq = $cos_sq_alpha*($a*$a-$b*$b)/($b*$b); | |||||||||||||||||||||||||||
| 373 | 0 | my $aa = 1 + $usq/16384*(4096+$usq*(-768+$usq*(320-175*$usq))); | |||||||||||||||||||||||||||
| 374 | 0 | my $bb = $usq/1024 * (256+$usq*(-128+$usq*(74-47*$usq))); | |||||||||||||||||||||||||||
| 375 | 0 | my $delta_sigma = $bb*$sin_sigma*($cos2sigma_m+$bb/4*($cos_sigma*(-1+2*$cos2sigma_m*$cos2sigma_m)- | |||||||||||||||||||||||||||
| 376 | $bb/6*$cos2sigma_m*(-3+4*$sin_sigma*$sin_sigma)*(-3+4*$cos2sigma_m*$cos2sigma_m))); | ||||||||||||||||||||||||||||
| 377 | 0 | $c = ( $b*$aa*($sigma-$delta_sigma) ) / $self->{units}->{meter}; | |||||||||||||||||||||||||||
| 378 | } | ||||||||||||||||||||||||||||
| 379 | else{ | ||||||||||||||||||||||||||||
| 380 | 0 | croak('Unkown distance formula "'.$self->{formula}.'"'); | |||||||||||||||||||||||||||
| 381 | } | ||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||
| 383 | 0 | return $unit * $c; | |||||||||||||||||||||||||||
| 384 | } | ||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||
| 386 | =head2 closest | ||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||
| 388 | my $locations = $geo->closest( | ||||||||||||||||||||||||||||
| 389 | dbh => $dbh, | ||||||||||||||||||||||||||||
| 390 | table => $table, | ||||||||||||||||||||||||||||
| 391 | lon => $lon, | ||||||||||||||||||||||||||||
| 392 | lat => $lat, | ||||||||||||||||||||||||||||
| 393 | unit => $unit_type, | ||||||||||||||||||||||||||||
| 394 | distance => $dist_in_unit | ||||||||||||||||||||||||||||
| 395 | ); | ||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||
| 397 | This method finds the closest locations within a certain distance and returns an | ||||||||||||||||||||||||||||
| 398 | array reference with a hash for each location matched. | ||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||
| 400 | The closest method requires the following arguments: | ||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||
| 402 | dbh - a DBI database handle | ||||||||||||||||||||||||||||
| 403 | table - a table within dbh that contains the locations to search | ||||||||||||||||||||||||||||
| 404 | lon - the longitude of the center point | ||||||||||||||||||||||||||||
| 405 | lat - the latitude of the center point | ||||||||||||||||||||||||||||
| 406 | unit - the unit of measurement to use, such as "meter" | ||||||||||||||||||||||||||||
| 407 | distance - the distance, in units, from the center point to find locations | ||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||
| 409 | The following arguments are optional: | ||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||
| 411 | lon_field - the name of the field in the table that contains the longitude, defaults to "lon" | ||||||||||||||||||||||||||||
| 412 | lat_field - the name of the field in the table that contains the latitude, defaults to "lat" | ||||||||||||||||||||||||||||
| 413 | fields - an array reference of extra field names that you would like returned with each location | ||||||||||||||||||||||||||||
| 414 | where - additional rules for the where clause of the sql | ||||||||||||||||||||||||||||
| 415 | bind - an array reference of bind variables to go with the placeholders in where | ||||||||||||||||||||||||||||
| 416 | sort - whether to sort the locations by their distance, making the closest location the first returned | ||||||||||||||||||||||||||||
| 417 | count - return at most these number of locations (implies sort => 1) | ||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||
| 419 | This method uses some very simplistic calculations to SQL select out of the dbh. This | ||||||||||||||||||||||||||||
| 420 | means that the SQL should work fine on almost any database (only tested on MySQL and SQLite so far) and | ||||||||||||||||||||||||||||
| 421 | this also means that it is fast. Once this sub set of locations has been retrieved | ||||||||||||||||||||||||||||
| 422 | then more precise calculations are made to narrow down the result set. Remember, though, that | ||||||||||||||||||||||||||||
| 423 | the farther out your distance is, and the more locations in the table, the slower your searches will be. | ||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||
| 425 | =cut | ||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||
| 427 | sub closest { | ||||||||||||||||||||||||||||
| 428 | 4 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
closest: called CC=23 SCAR=34.6
Pod
documented
| |||||||||||||||||||||||||||||
| 429 | 4 | my %args = @_; | |||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||
| 431 | # Set defaults and prepare. | ||||||||||||||||||||||||||||
| 432 | 4 | ▶ | my $dbh = $args{dbh} || croak('You must supply a database handle'); | ||||||||||||||||||||||||||
|
Condition33%1/3
$args{'dbh'} || croak('You must supply a database handle')
MC/DC0%0/2
$args{'dbh'} || croak('You must supply a database handle')
$args{'dbh'}
croak('You must supply a database handle')
Truth table33%1/3
A || B
A= $args{'dbh'}B= croak('You must supply a database handle')
| |||||||||||||||||||||||||||||
| 433 | 4 | ▶ | $dbh->isa('DBI::db') || croak('The dbh must be a DBI database handle'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 434 | 4 | ▶ | my $table = $args{table} || croak('You must supply a table name'); | ||||||||||||||||||||||||||
|
Condition33%1/3
$args{'table'} || croak('You must supply a table name')
MC/DC0%0/2
$args{'table'} || croak('You must supply a table name')
$args{'table'}
croak('You must supply a table name')
Truth table33%1/3
A || B
A= $args{'table'}B= croak('You must supply a table name')
| |||||||||||||||||||||||||||||
| 435 | 4 | ▶ | my $lon = $args{lon} || croak('You must supply a longitude'); | ||||||||||||||||||||||||||
|
Condition33%1/3
$args{'lon'} || croak('You must supply a longitude')
MC/DC0%0/2
$args{'lon'} || croak('You must supply a longitude')
$args{'lon'}
croak('You must supply a longitude')
Truth table33%1/3
A || B
A= $args{'lon'}B= croak('You must supply a longitude')
| |||||||||||||||||||||||||||||
| 436 | 4 | ▶ | my $lat = $args{lat} || croak('You must supply a latitude'); | ||||||||||||||||||||||||||
|
Condition33%1/3
$args{'lat'} || croak('You must supply a latitude')
MC/DC0%0/2
$args{'lat'} || croak('You must supply a latitude')
$args{'lat'}
croak('You must supply a latitude')
Truth table33%1/3
A || B
A= $args{'lat'}B= croak('You must supply a latitude')
| |||||||||||||||||||||||||||||
| 437 | 4 | ▶ | my $distance = $args{distance} || croak('You must supply a distance'); | ||||||||||||||||||||||||||
|
Condition33%1/3
$args{'distance'} || croak('You must supply a distance')
MC/DC0%0/2
$args{'distance'} || croak('You must supply a distance')
$args{'distance'}
croak('You must supply a distance')
Truth table33%1/3
A || B
A= $args{'distance'}B= croak('You must supply a distance')
| |||||||||||||||||||||||||||||
| 438 | 4 | ▶ | my $unit = $args{unit} || croak('You must specify a unit type'); | ||||||||||||||||||||||||||
|
Condition33%1/3
$args{'unit'} || croak('You must specify a unit type')
MC/DC0%0/2
$args{'unit'} || croak('You must specify a unit type')
$args{'unit'}
croak('You must specify a unit type')
Truth table33%1/3
A || B
A= $args{'unit'}B= croak('You must specify a unit type')
| |||||||||||||||||||||||||||||
| 439 | 4 | ▶ | my $unit_size = $self->{units}->{$unit} || croak('This unit type is not known'); | ||||||||||||||||||||||||||
|
Condition33%1/3
$self->{'units'}{$unit} || croak('This unit type is not known')
MC/DC0%0/2
$self->{'units'}{$unit} || croak('This unit type is not known')
$self->{'units'}{$unit}
croak('This unit type is not known')
Truth table33%1/3
A || B
A= $self->{'units'}{$unit}B= croak('This unit type is not known')
| |||||||||||||||||||||||||||||
| 440 | 4 | my $degrees = $distance / ( $DEG_RATIO * $unit_size ); | |||||||||||||||||||||||||||
| 441 | 4 | ▶ | my $lon_field = $args{lon_field} || 'lon'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$args{'lon_field'} || 'lon'
MC/DC0%0/1
$args{'lon_field'} || 'lon'
$args{'lon_field'}
Truth table50%1/2
A
A= $args{'lon_field'}
| |||||||||||||||||||||||||||||
| 442 | 4 | ▶ | my $lat_field = $args{lat_field} || 'lat'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$args{'lat_field'} || 'lat'
MC/DC0%0/1
$args{'lat_field'} || 'lat'
$args{'lat_field'}
Truth table50%1/2
A
A= $args{'lat_field'}
| |||||||||||||||||||||||||||||
| 443 | 4 | ▶ | my $fields = $args{fields} || []; | ||||||||||||||||||||||||||
|
Condition50%1/2
$args{'fields'} || []
MC/DC0%0/1
$args{'fields'} || []
$args{'fields'}
Truth table50%1/2
A
A= $args{'fields'}
| |||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||
| 445 | 4 | unshift @$fields, $lon_field, $lat_field; | |||||||||||||||||||||||||||
| 446 | 4 | $fields = join( ',', @$fields ); | |||||||||||||||||||||||||||
| 447 | 4 | ▶ | my $count = $args{count} || 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
$args{'count'} || 0
MC/DC100%1/1
$args{'count'} || 0
$args{'count'}
Truth table100%2/2
A
A= $args{'count'}
| |||||||||||||||||||||||||||||
| 448 | 4 | ▶ | my $sort = $args{sort} || ( $count ? 1 : 0 ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$args{'sort'} || ($count ? 1 : 0)
MC/DC50%1/2
$args{'sort'} || ($count ? 1 : 0)
$args{'sort'}
($count ? 1 : 0)
Truth table66%2/3
A || B
A= $args{'sort'}B= ($count ? 1 : 0)
| |||||||||||||||||||||||||||||
| 449 | 4 | my $where = qq{$lon_field >= ? AND $lat_field >= ? AND $lon_field <= ? AND $lat_field <= ?}; | |||||||||||||||||||||||||||
| 450 | 4 | ▶ | $where .= ( $args{where} ? " AND ($args{where})" : '' ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 451 | |||||||||||||||||||||||||||||
| 452 | my @bind = ( | ||||||||||||||||||||||||||||
| 453 | $lon-$degrees, $lat-$degrees, | ||||||||||||||||||||||||||||
| 454 | $lon+$degrees, $lat+$degrees, | ||||||||||||||||||||||||||||
| 455 | 4 | ▶ | ( $args{bind} ? @{$args{bind}} : () ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 456 | ); | ||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||
| 458 | # Retrieve locations. | ||||||||||||||||||||||||||||
| 459 | 4 | my $sth = $dbh->prepare(qq{ | |||||||||||||||||||||||||||
| 460 | SELECT $fields | ||||||||||||||||||||||||||||
| 461 | FROM $table | ||||||||||||||||||||||||||||
| 462 | WHERE $where | ||||||||||||||||||||||||||||
| 463 | }); | ||||||||||||||||||||||||||||
| 464 | 4 | $sth->execute( @bind ); | |||||||||||||||||||||||||||
| 465 | 4 | my $locations = []; | |||||||||||||||||||||||||||
| 466 | 4 | ▶ | while(my $location = $sth->fetchrow_hashref){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 467 | 35 | push @$locations, $location; | |||||||||||||||||||||||||||
| 468 | } | ||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||
| 470 | # Calculate distances. | ||||||||||||||||||||||||||||
| 471 | 4 | my $closest = []; | |||||||||||||||||||||||||||
| 472 | 4 | foreach my $location (@$locations){ | |||||||||||||||||||||||||||
| 473 | $location->{distance} = $self->distance( | ||||||||||||||||||||||||||||
| 474 | $unit, $lon, $lat, | ||||||||||||||||||||||||||||
| 475 | $location->{$lon_field}, | ||||||||||||||||||||||||||||
| 476 | 35 | $location->{$lat_field} | |||||||||||||||||||||||||||
| 477 | ); | ||||||||||||||||||||||||||||
| 478 | 35 | ▶ | if( $location->{distance} <= $distance ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 479 | 32 | push @$closest, $location; | |||||||||||||||||||||||||||
| 480 | } | ||||||||||||||||||||||||||||
| 481 | } | ||||||||||||||||||||||||||||
| 482 | 4 | $locations = $closest; | |||||||||||||||||||||||||||
| 483 | |||||||||||||||||||||||||||||
| 484 | # Sort. | ||||||||||||||||||||||||||||
| 485 | 4 | ▶ | if( $sort ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 486 | 1 | @$locations = sort { $a->{distance} <=> $b->{distance} } @$locations; | |||||||||||||||||||||||||||
| 487 | } | ||||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||
| 489 | # Split for count. | ||||||||||||||||||||||||||||
| 490 | 4 | ▶ | if( $count and $count < @$locations ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$count and $count < @$locations
MC/DC50%1/2
$count and $count < @$locations
$count
$count < @$locations
Truth table66%2/3
A and B
A= $countB= $count < @$locations
| |||||||||||||||||||||||||||||
| 491 | 1 | splice @$locations, $count; | |||||||||||||||||||||||||||
| 492 | } | ||||||||||||||||||||||||||||
| 493 | |||||||||||||||||||||||||||||
| 494 | 4 | return $locations; | |||||||||||||||||||||||||||
| 495 | } | ||||||||||||||||||||||||||||
| 496 | |||||||||||||||||||||||||||||
| 497 | 4 | 1; | |||||||||||||||||||||||||||
| 498 | __END__ | ||||||||||||||||||||||||||||