| 1 | package Geo::AnomalyDetector; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Statistics::Basic qw(mean stddev); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Math::Trig; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | # use Geo::Inverse; | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | =head1 NAME | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | Geo::AnomalyDetector - Detect anomalies in geospatial coordinate datasets | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | This module analyzes latitude and longitude data points to identify anomalies based on their distance from the mean location. | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | use Geo::AnomalyDetector; | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | my $detector = Geo::AnomalyDetector->new(threshold => 3); | ||||||||||||||||||||||||||||
| 21 | my $coords = [ [37.7749, -122.4194], [40.7128, -74.0060], [35.6895, 139.6917] ]; | ||||||||||||||||||||||||||||
| 22 | my $anomalies = $detector->detect_anomalies($coords); | ||||||||||||||||||||||||||||
| 23 | print "Anomalies: " . join ", ", map { "($_->[0], $_->[1])" } @{$anomalies}; | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | Each co-ordinate can be either a two element array of [latitude, longitude] or an object that has | ||||||||||||||||||||||||||||
| 26 | C<latitude> and C<longitude> methods. | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | =head1 VERSION | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 0.02 | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | =cut | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 1 | our $VERSION = '0.02'; | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 1 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 37 | 1 | my ($class, %args) = @_; | |||||||||||||||||||||||||||
| 38 | my $self = { | ||||||||||||||||||||||||||||
| 39 | threshold => $args{threshold} || 3, | ||||||||||||||||||||||||||||
| 40 | 1 | ▶ | unit => $args{unit} || 'K', | ||||||||||||||||||||||||||
|
Condition50%2/4
$args{'threshold'} || 3
$args{'unit'} || 'K'
MC/DC0%0/1
$args{'threshold'} || 3
$args{'threshold'}
MC/DC0%0/1
$args{'unit'} || 'K'
$args{'unit'}
Truth table50%1/2
A
A= $args{'threshold'}
Truth table50%1/2
A
A= $args{'unit'}
| |||||||||||||||||||||||||||||
| 41 | }; | ||||||||||||||||||||||||||||
| 42 | 1 | bless $self, $class; | |||||||||||||||||||||||||||
| 43 | 1 | return $self; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 1 | ▶ | sub detect_anomalies { | ||||||||||||||||||||||||||
|
Subroutine
detect_anomalies: called CC=12 SCAR=25.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 47 | 1 | my ($self, $coordinates) = @_; | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 1 | my @distances; | |||||||||||||||||||||||||||
| 50 | 1 | ▶ | my $mean_lat = mean(map { (ref($_) eq 'ARRAY') ? $_->[0] : $_->latitude() } @{$coordinates}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | 1 | ▶ | my $mean_lon = mean(map { (ref($_) eq 'ARRAY') ? $_->[1] : $_->longitude() } @{$coordinates}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | # my $inverse = Geo::Inverse->new(); | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 1 | ▶ | die if(!defined($mean_lat) || !defined($mean_lon)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $mean_lat and defined $mean_lon
MC/DC0%0/2
defined $mean_lat and defined $mean_lon
defined $mean_lat
defined $mean_lon
Truth table33%1/3
A and B
A= defined $mean_latB= defined $mean_lon
| |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 1 | foreach my $coord (@{$coordinates}) { | |||||||||||||||||||||||||||
| 58 | 4 | ▶ | my ($lat, $lon) = (ref($coord) eq 'ARRAY') ? @{$coord} : ($coord->latitude(), $coord->longitude()); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 4 | ▶ | die if(!defined($lat) || !defined($lon)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $lat and defined $lon
MC/DC0%0/2
defined $lat and defined $lon
defined $lat
defined $lon
Truth table33%1/3
A and B
A= defined $latB= defined $lon
| |||||||||||||||||||||||||||||
| 60 | # my $distance = distance($lat, $lon, $mean_lat, $mean_lon, 'K'); | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | # Thanks to Robbie Hatley for working out the arguments to Math::Trig | ||||||||||||||||||||||||||||
| 63 | 4 | my $t1 = $lon * (pi/180); | |||||||||||||||||||||||||||
| 64 | 4 | my $p1 = (pi/2) - ($lat * (pi/180)); | |||||||||||||||||||||||||||
| 65 | 4 | my $t2 = $mean_lon * (pi/180); | |||||||||||||||||||||||||||
| 66 | 4 | my $p2 = (pi/2) - ($mean_lat * (pi/180)); | |||||||||||||||||||||||||||
| 67 | 4 | ▶ | my $rho = ($self->{'unit'} eq 'M') ? 3959.0 : 6371.0; # radius of Earth in miles/Km | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 4 | my $distance = Math::Trig::great_circle_distance($t1, $p1, $t2, $p2, $rho); | |||||||||||||||||||||||||||
| 69 | 4 | push @distances, $distance; | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 1 | my $mean_dist = mean(@distances); | |||||||||||||||||||||||||||
| 73 | 1 | my $std_dist = stddev(@distances); | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 1 | my @anomalies; | |||||||||||||||||||||||||||
| 76 | 1 | for my $i (0 .. $#distances) { | |||||||||||||||||||||||||||
| 77 | 4 | ▶ | if (abs($distances[$i] - $mean_dist) > ($self->{threshold} * $std_dist)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 1 | push @anomalies, $coordinates->[$i]; | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 1 | return \@anomalies; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | # Now use Math::Trig. I tried Geo::Inverse, but that always throws messages about undefined variables | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | # =head2 distance | ||||||||||||||||||||||||||||
| 88 | # | ||||||||||||||||||||||||||||
| 89 | # Calculate the distance between two geographical points using latitude and longitude. | ||||||||||||||||||||||||||||
| 90 | # Supports distance in kilometres (K), nautical miles (N), or miles. | ||||||||||||||||||||||||||||
| 91 | # | ||||||||||||||||||||||||||||
| 92 | # From L<http://www.geodatasource.com/developers/perl> | ||||||||||||||||||||||||||||
| 93 | # FIXME: use Math::Trig | ||||||||||||||||||||||||||||
| 94 | # | ||||||||||||||||||||||||||||
| 95 | # =cut | ||||||||||||||||||||||||||||
| 96 | # | ||||||||||||||||||||||||||||
| 97 | # sub distance { | ||||||||||||||||||||||||||||
| 98 | # my ($lat1, $lon1, $lat2, $lon2, $unit) = @_; | ||||||||||||||||||||||||||||
| 99 | # my $theta = $lon1 - $lon2; | ||||||||||||||||||||||||||||
| 100 | # my $dist = sin(_deg2rad($lat1)) * sin(_deg2rad($lat2)) + cos(_deg2rad($lat1)) * cos(_deg2rad($lat2)) * cos(_deg2rad($theta)); | ||||||||||||||||||||||||||||
| 101 | # $dist = _acos($dist); | ||||||||||||||||||||||||||||
| 102 | # $dist = _rad2deg($dist); | ||||||||||||||||||||||||||||
| 103 | # $dist = $dist * 60 * 1.1515; | ||||||||||||||||||||||||||||
| 104 | # if ($unit eq 'K') { | ||||||||||||||||||||||||||||
| 105 | # $dist = $dist * 1.609344; # number of kilometres in a mile | ||||||||||||||||||||||||||||
| 106 | # } elsif ($unit eq 'N') { | ||||||||||||||||||||||||||||
| 107 | # $dist = $dist * 0.8684; | ||||||||||||||||||||||||||||
| 108 | # } | ||||||||||||||||||||||||||||
| 109 | # return ($dist); | ||||||||||||||||||||||||||||
| 110 | # } | ||||||||||||||||||||||||||||
| 111 | # | ||||||||||||||||||||||||||||
| 112 | # my $pi = atan2(1,1) * 4; | ||||||||||||||||||||||||||||
| 113 | # | ||||||||||||||||||||||||||||
| 114 | # #:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: | ||||||||||||||||||||||||||||
| 115 | # #::: This function get the arccos function using arctan function ::: | ||||||||||||||||||||||||||||
| 116 | # #:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: | ||||||||||||||||||||||||||||
| 117 | # sub _acos { | ||||||||||||||||||||||||||||
| 118 | # my ($rad) = @_; | ||||||||||||||||||||||||||||
| 119 | # my $ret = atan2(sqrt(1 - $rad**2), $rad); | ||||||||||||||||||||||||||||
| 120 | # return $ret; | ||||||||||||||||||||||||||||
| 121 | # } | ||||||||||||||||||||||||||||
| 122 | # | ||||||||||||||||||||||||||||
| 123 | # #:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: | ||||||||||||||||||||||||||||
| 124 | # #::: This function converts decimal degrees to radians ::: | ||||||||||||||||||||||||||||
| 125 | # #:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: | ||||||||||||||||||||||||||||
| 126 | # sub _deg2rad { | ||||||||||||||||||||||||||||
| 127 | # my ($deg) = @_; | ||||||||||||||||||||||||||||
| 128 | # return ($deg * $pi / 180); | ||||||||||||||||||||||||||||
| 129 | # } | ||||||||||||||||||||||||||||
| 130 | # | ||||||||||||||||||||||||||||
| 131 | # #:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: | ||||||||||||||||||||||||||||
| 132 | # #::: This function converts radians to decimal degrees ::: | ||||||||||||||||||||||||||||
| 133 | # #:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: | ||||||||||||||||||||||||||||
| 134 | # sub _rad2deg { | ||||||||||||||||||||||||||||
| 135 | # my ($rad) = @_; | ||||||||||||||||||||||||||||
| 136 | # return ($rad * 180 / $pi); | ||||||||||||||||||||||||||||
| 137 | # } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | Nigel Horne, C<< <njh at nigelhorne.com> >> | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | =head1 BUGS | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | =over 4 | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | =item * L<Geo::Location::Point> | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | =item * L<Math::Trig> | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | =back | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | This module is provided as-is without any warranty. | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | Please report any bugs or feature requests to C<bug-geo-anomalydetector at rt.cpan.org>, | ||||||||||||||||||||||||||||
| 160 | or through the web interface at | ||||||||||||||||||||||||||||
| 161 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Geo-AnomalyDetector>. | ||||||||||||||||||||||||||||
| 162 | I will be notified, and then you'll | ||||||||||||||||||||||||||||
| 163 | automatically be notified of progress on your bug as I make changes. | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | perldoc Geo::AnomalyDetector | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | You can also look for information at: | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | =over 4 | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | =item * MetaCPAN | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | L<https://metacpan.org/dist/Geo-AnomalyDetector> | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | =item * RT: CPAN's request tracker | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Geo-AnomalyDetector> | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | =item * CPAN Testers' Matrix | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | L<http://matrix.cpantesters.org/?dist=Geo-AnomalyDetector> | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | =item * CPAN Testers Dependencies | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | L<http://deps.cpantesters.org/?module=Geo::AnomalyDetector> | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | =back | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | Copyright 2025 Nigel Horne. | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | This program is released under the following licence: GPL2 | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | =cut | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | 1 | 1; | |||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | __END__ | ||||||||||||||||||||||||||||