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