Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
52.94% |
9 / 17 |
|
33.33% |
2 / 6 |
CRAP | |
0.00% |
0 / 1 |
BoundingBox | |
52.94% |
9 / 17 |
|
33.33% |
2 / 6 |
14.67 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
newFromPoints | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
topLeft | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
bottomRight | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
area | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
center | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 |
1 | <?php |
2 | |
3 | namespace GeoData; |
4 | |
5 | /** |
6 | * Class that represents a bounding box |
7 | * Currently, only Earth is supported |
8 | */ |
9 | class BoundingBox { |
10 | /** @var float */ |
11 | public $lat1; |
12 | /** @var float */ |
13 | public $lon1; |
14 | /** @var float */ |
15 | public $lat2; |
16 | /** @var float */ |
17 | public $lon2; |
18 | /** @var string */ |
19 | public $globe; |
20 | |
21 | /** |
22 | * @param float $lat1 |
23 | * @param float $lon1 |
24 | * @param float $lat2 |
25 | * @param float $lon2 |
26 | * @param string $globe |
27 | */ |
28 | public function __construct( $lat1, $lon1, $lat2, $lon2, $globe = 'earth' ) { |
29 | $this->lat1 = $lat1; |
30 | $this->lon1 = $lon1; |
31 | $this->lat2 = $lat2; |
32 | $this->lon2 = $lon2; |
33 | $this->globe = $globe; |
34 | } |
35 | |
36 | /** |
37 | * Constructs a bounding box from 2 corner coordinates |
38 | * |
39 | * @param Coord $topLeft |
40 | * @param Coord $bottomRight |
41 | * @return self |
42 | */ |
43 | public static function newFromPoints( Coord $topLeft, Coord $bottomRight ): self { |
44 | return new self( $topLeft->lat, $topLeft->lon, $bottomRight->lat, $bottomRight->lon, |
45 | $topLeft->globe ); |
46 | } |
47 | |
48 | /** |
49 | * @return Coord Top left corner of this bounding box |
50 | */ |
51 | public function topLeft(): Coord { |
52 | return new Coord( $this->lat1, $this->lon1, $this->globe ); |
53 | } |
54 | |
55 | /** |
56 | * @return Coord Bottom right corner of this bounding box |
57 | */ |
58 | public function bottomRight(): Coord { |
59 | return new Coord( $this->lat2, $this->lon2, $this->globe ); |
60 | } |
61 | |
62 | /** |
63 | * Computes a (very approximate) area of this bounding box |
64 | * |
65 | * @return float |
66 | */ |
67 | public function area() { |
68 | $midLat = ( $this->lat2 + $this->lat1 ) / 2; |
69 | $vert = Math::distance( $this->lat1, 0, $this->lat2, 0 ); |
70 | $horz = Math::distance( $midLat, $this->lon1, $midLat, $this->lon2 ); |
71 | |
72 | return $horz * $vert; |
73 | } |
74 | |
75 | /** |
76 | * Returns center of this bounding box |
77 | * |
78 | * @return Coord |
79 | */ |
80 | public function center(): Coord { |
81 | $lon = ( $this->lon2 + $this->lon1 ) / 2.0; |
82 | if ( $this->lon1 > $this->lon2 ) { |
83 | // Wrap around |
84 | $lon += ( $lon < 0 ) ? 180 : -180; |
85 | } |
86 | |
87 | return new Coord( ( $this->lat1 + $this->lat2 ) / 2.0, $lon ); |
88 | } |
89 | } |