1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Location; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Coordinate Bounds Class |
9
|
|
|
* |
10
|
|
|
* @author Marcus Jaschen <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
class Bounds |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var Coordinate |
16
|
|
|
*/ |
17
|
|
|
protected $northWest; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var Coordinate |
21
|
|
|
*/ |
22
|
|
|
protected $southEast; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param Coordinate $northWest |
26
|
|
|
* @param Coordinate $southEast |
27
|
|
|
*/ |
28
|
|
|
public function __construct(Coordinate $northWest, Coordinate $southEast) |
29
|
|
|
{ |
30
|
|
|
$this->northWest = $northWest; |
31
|
|
|
$this->southEast = $southEast; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Getter |
36
|
|
|
* |
37
|
|
|
* @return Coordinate |
38
|
|
|
*/ |
39
|
|
|
public function getNorthWest(): Coordinate |
40
|
|
|
{ |
41
|
|
|
return $this->northWest; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Getter |
46
|
|
|
* |
47
|
|
|
* @return Coordinate |
48
|
|
|
*/ |
49
|
|
|
public function getSouthEast(): Coordinate |
50
|
|
|
{ |
51
|
|
|
return $this->southEast; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return float |
56
|
|
|
*/ |
57
|
|
|
public function getNorth(): float |
58
|
|
|
{ |
59
|
|
|
return $this->northWest->getLat(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return float |
64
|
|
|
*/ |
65
|
|
|
public function getSouth(): float |
66
|
|
|
{ |
67
|
|
|
return $this->southEast->getLat(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return float |
72
|
|
|
*/ |
73
|
|
|
public function getWest(): float |
74
|
|
|
{ |
75
|
|
|
return $this->northWest->getLng(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return float |
80
|
|
|
*/ |
81
|
|
|
public function getEast(): float |
82
|
|
|
{ |
83
|
|
|
return $this->southEast->getLng(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Calculates the center of this bounds object and returns it as a |
88
|
|
|
* Coordinate instance. |
89
|
|
|
* |
90
|
|
|
* @return Coordinate |
91
|
|
|
* @throws \InvalidArgumentException |
92
|
|
|
*/ |
93
|
|
|
public function getCenter(): Coordinate |
94
|
|
|
{ |
95
|
|
|
$centerLat = ($this->getNorth() + $this->getSouth()) / 2; |
96
|
|
|
|
97
|
|
|
return new Coordinate($centerLat, $this->getCenterLng()); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return float |
102
|
|
|
*/ |
103
|
|
|
protected function getCenterLng(): float |
104
|
|
|
{ |
105
|
|
|
$centerLng = ($this->getEast() + $this->getWest()) / 2; |
106
|
|
|
|
107
|
|
|
$overlap = $this->getWest() > 0 && $this->getEast() < 0; |
108
|
|
|
|
109
|
|
|
if ($overlap && $centerLng > 0) { |
110
|
|
|
return -180.0 + $centerLng; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
if ($overlap && $centerLng < 0) { |
114
|
|
|
return 180.0 + $centerLng; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
if ($overlap && $centerLng == 0) { |
118
|
|
|
return 180.0; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return $centerLng; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|