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