| 1 | #! perl | ||
| 2 | |||
| 3 | use v5.36; | ||
| 4 | use Object::Pad; | ||
| 5 | use Class::JSON_Object; | ||
| 6 | use utf8; | ||
| 7 | |||
| 8 | =head1 NAME | ||
| 9 | |||
| 10 | Weenect::Position - position data | ||
| 11 | |||
| 12 | =cut | ||
| 13 | |||
| 14 | =head1 DESCRIPTION | ||
| 15 | |||
| 16 | Weenect::Position describes a position. | ||
| 17 | |||
| 18 | =head1 METHODS | ||
| 19 | |||
| 20 | Supported methods include: | ||
| 21 | |||
| 22 | =head2 latitude | ||
| 23 | |||
| 24 | =head2 longitude | ||
| 25 | |||
| 26 | =head2 battery_text | ||
| 27 | |||
| 28 | =head2 gsm_text | ||
| 29 | |||
| 30 | =head2 accuracy_text | ||
| 31 | |||
| 32 | =head2 geofence_name | ||
| 33 | |||
| 34 | =head2 wifi_zone_id | ||
| 35 | |||
| 36 | =head2 date_tracker | ||
| 37 | |||
| 38 | =head2 distance( $other ) | ||
| 39 | |||
| 40 | Returns the distance (in meters) between the position and $other. | ||
| 41 | |||
| 42 | =cut | ||
| 43 | |||
| 44 | class Weenect::Position :does(Class::JSON_Object) { | ||
| 45 | field $latitude; | ||
| 46 | field $longitude; | ||
| 47 | field $battery_text; | ||
| 48 | field $gsm_text; | ||
| 49 | field $accuracy_text; | ||
| 50 | field $geofence_name; | ||
| 51 | field $wifi_zone_id; | ||
| 52 | field $date_tracker; | ||
| 53 | |||
| 54 | method distance($other) { | ||
| 55 | $other = Weenect::Point->new( longitude => $other->longitude, | ||
| 56 | latitude => $other->latitude ) | ||
| 57 | unless $other isa Weenect::Point; | ||
| 58 | Weenect::Point->new( longitude => $longitude, | ||
| 59 | latitude => $latitude )->distance($other); | ||
| 60 | } | ||
| 61 | } | ||
| 62 | |||
| 63 | class Weenect::Point { | ||
| 64 | field $latitude :param; | ||
| 65 | field $longitude :param; | ||
| 66 | |||
| 67 | use constant PI_D => atan2(1,1) / 45; | ||
| 68 | sub d2r($d) { $d * PI_D } # degrees to radians | ||
| 69 | |||
| 70 | method distance($other) { | ||
| 71 | my $longitude1 = d2r($longitude); | ||
| 72 | my $latitude1 = d2r($latitude); | ||
| 73 | my $longitude2 = d2r($other->longitude); | ||
| 74 | my $latitude2 = d2r($other->latitude); | ||
| 75 | my $dlon = $longitude2 - $longitude1; | ||
| 76 | my $dlat = $latitude2 - $latitude1; | ||
| 77 | my $a = (sin($dlat/2))**2 + cos($latitude1) * cos($latitude2) * (sin($dlon/2))**2; | ||
| 78 | my $c = 2 * atan2( sqrt($a), sqrt(1-$a) ); | ||
| 79 | return 6371640 * $c; # meters | ||
| 80 | } | ||
| 81 | } | ||
| 82 | |||
| 83 | 1; |