Code Coverage |
||||||||||||||||
Lines |
Branches |
Paths |
Functions and Methods |
Classes and Traits |
||||||||||||
| Total | |
100.00% |
29 / 29 |
|
86.67% |
39 / 45 |
|
66.67% |
22 / 33 |
|
54.55% |
6 / 11 |
CRAP | |
0.00% |
0 / 1 |
| LatLonRange | |
100.00% |
29 / 29 |
|
86.67% |
39 / 45 |
|
66.67% |
22 / 33 |
|
100.00% |
11 / 11 |
57.04 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| __destruct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| __toString | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| min | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| max | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| distance | |
100.00% |
7 / 7 |
|
60.00% |
3 / 5 |
|
25.00% |
1 / 4 |
|
100.00% |
1 / 1 |
6.80 | |||
| isPointContained | |
100.00% |
2 / 2 |
|
100.00% |
7 / 7 |
|
50.00% |
4 / 8 |
|
100.00% |
1 / 1 |
6.00 | |||
| position | |
100.00% |
3 / 3 |
|
85.71% |
6 / 7 |
|
75.00% |
3 / 4 |
|
100.00% |
1 / 1 |
4.25 | |||
| getNorthPosition | |
100.00% |
3 / 3 |
|
85.71% |
6 / 7 |
|
75.00% |
3 / 4 |
|
100.00% |
1 / 1 |
4.25 | |||
| getCenterPosition | |
100.00% |
3 / 3 |
|
85.71% |
6 / 7 |
|
75.00% |
3 / 4 |
|
100.00% |
1 / 1 |
4.25 | |||
| getSouthPosition | |
100.00% |
3 / 3 |
|
85.71% |
6 / 7 |
|
75.00% |
3 / 4 |
|
100.00% |
1 / 1 |
4.25 | |||
| 1 | <?php |
| 2 | namespace Fdsn\Webservices\Event\Range; |
| 3 | |
| 4 | use Fdsn\Webservices\Event\Data\LatLon; |
| 5 | |
| 6 | /** |
| 7 | * Data structure to handle Latitude,Longitude bounding box |
| 8 | * |
| 9 | * @param LatLon $min Min Latitude,Longitude |
| 10 | * @param LatLon $max Max Latitude,Longitude |
| 11 | */ |
| 12 | class LatLonRange { |
| 13 | private const earthRadius = 6372.795; |
| 14 | private LatLon $min; |
| 15 | private LatLon $max; |
| 16 | |
| 17 | private $validLatitude = array('min' => -180, 'max' => 180); |
| 18 | private $validLongitude = array('min' => -180, 'max' => 180); |
| 19 | |
| 20 | function __construct( LatLon $min, LatLon $max) { |
| 21 | $this->min = $min; |
| 22 | $this->max = $max; |
| 23 | } |
| 24 | |
| 25 | function __destruct(){ } |
| 26 | |
| 27 | /** |
| 28 | * Returns bounding box coordinates |
| 29 | * |
| 30 | * @return string Bounding box in [%.5f,%.5f],[%.5f,%.5f] [min lat,lon],[max lat,lon] format |
| 31 | */ |
| 32 | function __toString(){ |
| 33 | return sprintf("[%.5f,%.5f],[%.5f,%.5f]", |
| 34 | $this->min->lat(), $this->min->lon(), |
| 35 | $this->max->lat(), $this->max->lon()); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Get min bouding box coordinates |
| 40 | * |
| 41 | * @return LatLon Min bouding box coordinates |
| 42 | */ |
| 43 | public function min():LatLon { return $this->min; } |
| 44 | |
| 45 | /** |
| 46 | * Get max bouding box coordinates |
| 47 | * |
| 48 | * @return LatLon Max bouding box coordinates |
| 49 | */ |
| 50 | public function max():LatLon { return $this->max; } |
| 51 | |
| 52 | /** |
| 53 | * Get distance (in km) between the 2 points |
| 54 | * |
| 55 | * @return float distance in km |
| 56 | */ |
| 57 | public function distance():float{ |
| 58 | $colat1 = ( 90 - $this->min->lat()) * pi() / 180; |
| 59 | $colat2 = ( 90 - $this->max->lat()) * pi() / 180; |
| 60 | |
| 61 | $phi = ($this->max->lon() - $this->min->lon()) * pi() / 180; |
| 62 | $arg = cos($colat1) * cos($colat2) + sin($colat1) * sin($colat2) * cos($phi); |
| 63 | |
| 64 | if( $arg > 1 ) $arg = 1; |
| 65 | if( $arg < -1 ) $arg = -1; |
| 66 | |
| 67 | return abs(self::earthRadius * acos($arg)); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Check if a point (LatLon obj) is contained by the bounding box (this LatLonRange) |
| 72 | * |
| 73 | * @param LatLon $point point to check if is contained by the bounding box |
| 74 | * |
| 75 | * @return bool true if is contained by the bounding box, false otherwise |
| 76 | */ |
| 77 | public function isPointContained(LatLon $point):bool{ |
| 78 | return ( $this->min->lat() <= $point->lat() && $point->lat() <= $this->max->lat() |
| 79 | && $this->min->lon() <= $point->lon() && $point->lon() <= $this->max->lon() ); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Get spatial position first point respect to second point |
| 84 | * possible values are: |
| 85 | * NO N NE |
| 86 | * O C E |
| 87 | * SO S SE |
| 88 | * @return string position of first point respect to second point (see __construct()) |
| 89 | */ |
| 90 | public function position():string{ |
| 91 | //North |
| 92 | if( $this->min->lat() > $this->max->lat() ) return $this->getNorthPosition(); |
| 93 | |
| 94 | //Center |
| 95 | if( $this->min->lat() == $this->max->lat() ) return $this->getCenterPosition(); |
| 96 | |
| 97 | //South |
| 98 | if( $this->min->lat() < $this->max->lat() ) return $this->getSouthPosition(); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Get north position |
| 103 | * possible values are: |
| 104 | * NO N NE |
| 105 | * @return string position of first point respect to second point (see __construct()) |
| 106 | */ |
| 107 | private function getNorthPosition():string{ |
| 108 | if($this->min->lon() < $this->max->lon()) return 'NO'; |
| 109 | if($this->min->lon() == $this->max->lon()) return 'N'; |
| 110 | if($this->min->lon() > $this->max->lon()) return 'NE'; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Get center position |
| 115 | * possible values are: |
| 116 | * O C E |
| 117 | * @return string position of first point respect to second point (see __construct()) |
| 118 | */ |
| 119 | private function getCenterPosition():string{ |
| 120 | if($this->min->lon() < $this->max->lon()) return 'O'; |
| 121 | if($this->min->lon() == $this->max->lon()) return 'C'; |
| 122 | if($this->min->lon() > $this->max->lon()) return 'E'; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Get south position |
| 127 | * possible values are: |
| 128 | * SO S SE |
| 129 | * @return string position of first point respect to second point (see __construct()) |
| 130 | */ |
| 131 | private function getSouthPosition():string{ |
| 132 | if($this->min->lon() > $this->max->lon()) return 'SO'; |
| 133 | if($this->min->lon() == $this->max->lon()) return 'S'; |
| 134 | if($this->min->lon() < $this->max->lon()) return 'SE'; |
| 135 | } |
| 136 | |
| 137 | } |