-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
GEOSEngine.php
375 lines (322 loc) · 11.1 KB
/
GEOSEngine.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
<?php
declare(strict_types=1);
namespace Brick\Geo\Engine;
use Brick\Geo\Curve;
use Brick\Geo\Exception\GeometryEngineException;
use Brick\Geo\IO\EWKBReader;
use Brick\Geo\IO\EWKBWriter;
use Brick\Geo\Geometry;
use Brick\Geo\MultiCurve;
use Brick\Geo\MultiSurface;
use Brick\Geo\Point;
use Brick\Geo\Surface;
use Brick\Geo\MultiPolygon;
use Brick\Geo\Polygon;
use GEOSWKBReader;
use GEOSWKBWriter;
use GEOSWKTReader;
use GEOSWKTWriter;
/**
* GeometryEngine implementation based on the GEOS PHP bindings.
*/
class GEOSEngine implements GeometryEngine
{
private GEOSWKBReader $wkbReader;
private GEOSWKBWriter $wkbWriter;
private GEOSWKTReader $wktReader;
private GEOSWKTWriter $wktWriter;
private EWKBReader $ewkbReader;
private EWKBWriter $ewkbWriter;
/**
* Whether the GEOS version in use has support for binary read() and write() methods.
*
* These methods are available since GEOS 3.5.0.
*/
private bool $hasBinaryReadWrite;
public function __construct()
{
$this->wkbReader = new \GEOSWKBReader();
$this->wkbWriter = new \GEOSWKBWriter();
$this->wktReader = new \GEOSWKTReader();
$this->wktWriter = new \GEOSWKTWriter();
$this->ewkbReader = new EWKBReader();
$this->ewkbWriter = new EWKBWriter();
/** @psalm-suppress RedundantCondition These methods are not available before GEOS 3.5.0 */
$this->hasBinaryReadWrite =
method_exists($this->wkbReader, 'read') &&
method_exists($this->wkbWriter, 'write');
}
private function toGEOS(Geometry $geometry) : \GEOSGeometry
{
if ($geometry->isEmpty()) {
$geosGeometry = $this->wktReader->read($geometry->asText());
$geosGeometry->setSRID($geometry->SRID());
return $geosGeometry;
}
if ($this->hasBinaryReadWrite) {
return $this->wkbReader->read($this->ewkbWriter->write($geometry));
}
return $this->wkbReader->readHEX(bin2hex($this->ewkbWriter->write($geometry)));
}
private function fromGEOS(\GEOSGeometry $geometry) : Geometry
{
if ($geometry->isEmpty()) {
return Geometry::fromText($this->wktWriter->write($geometry), $geometry->getSRID());
}
if ($this->hasBinaryReadWrite) {
return $this->ewkbReader->read($this->wkbWriter->write($geometry));
}
return $this->ewkbReader->read(hex2bin($this->wkbWriter->writeHEX($geometry)));
}
public function union(Geometry $a, Geometry $b) : Geometry
{
try {
return $this->fromGEOS($this->toGEOS($a)->union($this->toGEOS($b)));
} catch (\Exception $e) {
throw GeometryEngineException::operationNotSupportedByEngine($e);
}
}
public function difference(Geometry $a, Geometry $b) : Geometry
{
try {
return $this->fromGEOS($this->toGEOS($a)->difference($this->toGEOS($b)));
} catch (\Exception $e) {
throw GeometryEngineException::operationNotSupportedByEngine($e);
}
}
public function envelope(Geometry $g) : Geometry
{
try {
return $this->fromGEOS($this->toGEOS($g)->envelope());
} catch (\Exception $e) {
throw GeometryEngineException::operationNotSupportedByEngine($e);
}
}
public function length(Curve|MultiCurve $g) : float
{
try {
return $this->toGEOS($g)->length();
} catch (\Exception $e) {
throw GeometryEngineException::operationNotSupportedByEngine($e);
}
}
public function area(Surface|MultiSurface $g) : float
{
try {
return $this->toGEOS($g)->area();
} catch (\Exception $e) {
throw GeometryEngineException::operationNotSupportedByEngine($e);
}
}
public function azimuth(Point $observer, Point $subject) : float
{
throw GeometryEngineException::unimplementedMethod(__METHOD__);
}
/**
* @psalm-suppress LessSpecificReturnStatement
* @psalm-suppress MoreSpecificReturnType
*/
public function centroid(Geometry $g) : Point
{
try {
return $this->fromGEOS($this->toGEOS($g)->centroid());
} catch (\Exception $e) {
throw GeometryEngineException::operationNotSupportedByEngine($e);
}
}
public function pointOnSurface(Surface|MultiSurface $g) : Geometry
{
try {
return $this->fromGEOS($this->toGEOS($g)->pointOnSurface());
} catch (\Exception $e) {
throw GeometryEngineException::operationNotSupportedByEngine($e);
}
}
public function boundary(Geometry $g) : Geometry
{
try {
return $this->fromGEOS($this->toGEOS($g)->boundary());
} catch (\Exception $e) {
throw GeometryEngineException::operationNotSupportedByEngine($e);
}
}
public function isValid(Geometry $g) : bool
{
try {
return $this->toGEOS($g)->checkValidity()['valid'];
} catch (\Exception $e) {
throw GeometryEngineException::operationNotSupportedByEngine($e);
}
}
public function isClosed(Geometry $g) : bool
{
try {
return $this->toGEOS($g)->isClosed();
} catch (\Exception $e) {
throw GeometryEngineException::operationNotSupportedByEngine($e);
}
}
public function isSimple(Geometry $g) : bool
{
try {
return $this->toGEOS($g)->isSimple();
} catch (\Exception $e) {
throw GeometryEngineException::operationNotSupportedByEngine($e);
}
}
public function isRing(Curve $curve) : bool
{
try {
return $this->toGEOS($curve)->isRing();
} catch (\Exception $e) {
throw GeometryEngineException::operationNotSupportedByEngine($e);
}
}
public function equals(Geometry $a, Geometry $b) : bool
{
try {
return $this->toGEOS($a)->equals($this->toGEOS($b));
} catch (\Exception $e) {
throw GeometryEngineException::operationNotSupportedByEngine($e);
}
}
public function disjoint(Geometry $a, Geometry $b) : bool
{
try {
return $this->toGEOS($a)->disjoint($this->toGEOS($b));
} catch (\Exception $e) {
throw GeometryEngineException::operationNotSupportedByEngine($e);
}
}
public function intersects(Geometry $a, Geometry $b) : bool
{
try {
return $this->toGEOS($a)->intersects($this->toGEOS($b));
} catch (\Exception $e) {
throw GeometryEngineException::operationNotSupportedByEngine($e);
}
}
public function touches(Geometry $a, Geometry $b) : bool
{
try {
return $this->toGEOS($a)->touches($this->toGEOS($b));
} catch (\Exception $e) {
throw GeometryEngineException::operationNotSupportedByEngine($e);
}
}
public function crosses(Geometry $a, Geometry $b) : bool
{
try {
return $this->toGEOS($a)->crosses($this->toGEOS($b));
} catch (\Exception $e) {
throw GeometryEngineException::operationNotSupportedByEngine($e);
}
}
public function within(Geometry $a, Geometry $b) : bool
{
try {
return $this->toGEOS($a)->within($this->toGEOS($b));
} catch (\Exception $e) {
throw GeometryEngineException::operationNotSupportedByEngine($e);
}
}
public function contains(Geometry $a, Geometry $b) : bool
{
try {
return $this->toGEOS($a)->contains($this->toGEOS($b));
} catch (\Exception $e) {
throw GeometryEngineException::operationNotSupportedByEngine($e);
}
}
public function overlaps(Geometry $a, Geometry $b) : bool
{
try {
return $this->toGEOS($a)->overlaps($this->toGEOS($b));
} catch (\Exception $e) {
throw GeometryEngineException::operationNotSupportedByEngine($e);
}
}
public function relate(Geometry $a, Geometry $b, string $matrix) : bool
{
try {
$result = $this->toGEOS($a)->relate($this->toGEOS($b), $matrix);
// giving a matrix should always return a boolean
assert(is_bool($result));
return $result;
} catch (\Exception $e) {
throw GeometryEngineException::operationNotSupportedByEngine($e);
}
}
public function locateAlong(Geometry $g, float $mValue) : Geometry
{
throw GeometryEngineException::unimplementedMethod(__METHOD__);
}
public function locateBetween(Geometry $g, float $mStart, float $mEnd) : Geometry
{
throw GeometryEngineException::unimplementedMethod(__METHOD__);
}
public function distance(Geometry $a, Geometry $b) : float
{
try {
return $this->toGEOS($a)->distance($this->toGEOS($b));
} catch (\Exception $e) {
throw GeometryEngineException::operationNotSupportedByEngine($e);
}
}
public function buffer(Geometry $g, float $distance) : Geometry
{
try {
return $this->fromGEOS($this->toGEOS($g)->buffer($distance));
} catch (\Exception $e) {
throw GeometryEngineException::operationNotSupportedByEngine($e);
}
}
public function convexHull(Geometry $g) : Geometry
{
try {
return $this->fromGEOS($this->toGEOS($g)->convexHull());
} catch (\Exception $e) {
throw GeometryEngineException::operationNotSupportedByEngine($e);
}
}
public function intersection(Geometry $a, Geometry $b) : Geometry
{
try {
return $this->fromGEOS($this->toGEOS($a)->intersection($this->toGEOS($b)));
} catch (\Exception $e) {
throw GeometryEngineException::operationNotSupportedByEngine($e);
}
}
public function symDifference(Geometry $a, Geometry $b) : Geometry
{
try {
return $this->fromGEOS($this->toGEOS($a)->symDifference($this->toGEOS($b)));
} catch (\Exception $e) {
throw GeometryEngineException::operationNotSupportedByEngine($e);
}
}
public function snapToGrid(Geometry $g, float $size) : Geometry
{
throw GeometryEngineException::unimplementedMethod(__METHOD__);
}
public function simplify(Geometry $g, float $tolerance) : Geometry
{
try {
return $this->fromGEOS($this->toGEOS($g)->simplify($tolerance));
} catch (\Exception $e) {
throw GeometryEngineException::operationNotSupportedByEngine($e);
}
}
public function maxDistance(Geometry $a, Geometry $b) : float
{
throw GeometryEngineException::unimplementedMethod(__METHOD__);
}
public function boundingPolygons(Polygon $p) : MultiPolygon
{
throw GeometryEngineException::unimplementedMethod(__METHOD__);
}
public function transform(Geometry $g, int $srid) : Geometry
{
throw GeometryEngineException::unimplementedMethod(__METHOD__);
}
}