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