-
Notifications
You must be signed in to change notification settings - Fork 128
Expand file tree
/
Copy pathMultiPolygon.php
More file actions
142 lines (121 loc) · 3.49 KB
/
Copy pathMultiPolygon.php
File metadata and controls
142 lines (121 loc) · 3.49 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
namespace League\Geotools\Polygon;
use League\Geotools\Coordinate\Coordinate;
use League\Geotools\Coordinate\CoordinateInterface;
use League\Geotools\GeometryCollection;
class MultiPolygon extends GeometryCollection implements PolygonInterface
{
use PolygonCenterTrait;
const TYPE = 'MULTIPOLYGON';
/**
* @return string
*/
public function getGeometryType()
{
return self::TYPE;
}
/**
* @param CoordinateInterface $coordinate
* @return boolean
*/
public function pointInPolygon(CoordinateInterface $coordinate)
{
/** @var PolygonInterface $polygon */
foreach ($this->elements as $polygon) {
if ($polygon->pointInPolygon($coordinate)) {
return true;
}
}
return false;
}
/**
* @param CoordinateInterface $coordinate
* @return boolean
*/
public function pointOnBoundary(CoordinateInterface $coordinate)
{
/** @var PolygonInterface $polygon */
foreach ($this->elements as $polygon) {
if ($polygon->pointOnBoundary($coordinate)) {
return true;
}
}
return false;
}
/**
* @param CoordinateInterface $coordinate
* @return boolean
*/
public function pointOnVertex(CoordinateInterface $coordinate)
{
/** @var PolygonInterface $polygon */
foreach ($this->elements as $polygon) {
if ($polygon->pointOnVertex($coordinate)) {
return true;
}
}
return false;
}
/**
* Returns the geographic centroid of all coordinates across every polygon
* in this collection.
*
* @return CoordinateInterface|null null when the collection is empty
*/
public function getCenter(): ?CoordinateInterface
{
$sumX = $sumY = $sumZ = 0.0;
$count = 0;
$ellipsoid = null;
foreach ($this->getCoordinates() as $coordinate) {
[$x, $y, $z] = $this->coordinateToXYZ($coordinate);
$sumX += $x;
$sumY += $y;
$sumZ += $z;
$count++;
if ($ellipsoid === null) {
$ellipsoid = $coordinate->getEllipsoid();
}
}
if ($count === 0) {
return null;
}
$x = $sumX / $count;
$y = $sumY / $count;
$z = $sumZ / $count;
$lng = rad2deg(atan2($y, $x));
$hyp = sqrt($x * $x + $y * $y);
$lat = rad2deg(atan2($z, $hyp));
return new Coordinate([$lat, $lng], $ellipsoid);
}
/**
* Returns the radius of the multi-polygon in meters, defined as the
* maximum haversine distance from the centroid to any vertex.
*
* @return float
*/
public function getRadius(): float
{
$center = $this->getCenter();
if ($center === null) {
return 0.0;
}
$radius = 0.0;
$R = $center->getEllipsoid()->getArithmeticMeanRadius();
$latC = deg2rad($center->getLatitude());
$lngC = deg2rad($center->getLongitude());
foreach ($this->getCoordinates() as $coordinate) {
$d = $this->haversineDistance(
$R,
$latC,
$lngC,
deg2rad($coordinate->getLatitude()),
deg2rad($coordinate->getLongitude())
);
if ($d > $radius) {
$radius = $d;
}
}
return $radius;
}
}