Skip to content

Commit 7d07618

Browse files
committed
Add Point::isEqualTo() (WIP: finish? keep?)
1 parent 2314ad0 commit 7d07618

File tree

5 files changed

+151
-6
lines changed

5 files changed

+151
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ The following breaking changes may only affect you if you're writing your own en
122122
**New features**
123123

124124
- New engine methods: `GeometryEngine::lineInterpolatePoint()`, `lineInterpolatePoints()` (#55 by @arminwinkt)
125+
- New method: `Point::isEqualTo()`
125126

126127
**Improvements**
127128

src/CompoundCurve.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,8 @@ public function __construct(CoordinateSystem $cs, LineString|CircularString ...$
5454
$previousCurve = null;
5555

5656
foreach ($curves as $curve) {
57-
if ($previousCurve) {
58-
$endPoint = $previousCurve->endPoint();
59-
$startPoint = $curve->startPoint();
60-
61-
if ($endPoint != $startPoint) { // on purpose by-value comparison!
57+
if ($previousCurve !== null) {
58+
if (! $curve->startPoint()->isEqualTo($previousCurve->endPoint())) {
6259
throw new InvalidGeometryException('Incontinuous compound curve.');
6360
}
6461
}

src/Exception/EmptyGeometryException.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
namespace Brick\Geo\Exception;
66

77
/**
8-
* Exception thrown when trying to get a non-existent value out of an empty geometry.
8+
* Exception thrown when trying to get a non-existent value out of an empty geometry,
9+
* or when trying to perform an operation that does not support empty geometries.
910
*/
1011
final class EmptyGeometryException extends GeometryException
1112
{

src/Point.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,4 +263,26 @@ public function project(Projector $projector) : static
263263
{
264264
return $projector->project($this);
265265
}
266+
267+
/**
268+
* Returns whether this Point is exactly equal to another Point.
269+
* The points must have the same coordinates, in the same coordinate system.
270+
*/
271+
public function isEqualTo(Point $that) : bool
272+
{
273+
return $this->x === $that->x
274+
&& $this->y === $that->y
275+
&& $this->z === $that->z
276+
&& $this->m === $that->m
277+
&& $this->coordinateSystem->isEqualTo($that->coordinateSystem);
278+
}
279+
280+
/**
281+
* Returns whether this Point is spatially equal to another Point.
282+
* M coordinates are ignored in the comparison.
283+
*/
284+
public function isSpatiallyEqualTo(Point $that) : bool
285+
{
286+
return $this->withoutM()->isEqualTo($that->withoutM());
287+
}
266288
}

tests/PointTest.php

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,4 +202,128 @@ public static function providerToArray() : array
202202
['POINT ZM (4.5 5.6 6.7 7.8)', [4.5, 5.6, 6.7, 7.8]],
203203
];
204204
}
205+
206+
#[DataProvider('providerIsEqualTo')]
207+
public function testIsEqualTo(string $wkt1, int $srid1, string $wkt2, int $srid2, bool $isEqual): void
208+
{
209+
$point1 = Point::fromText($wkt1, $srid1);
210+
$point2 = Point::fromText($wkt2, $srid2);
211+
212+
self::assertSame($isEqual, $point1->isEqualTo($point2));
213+
self::assertSame($isEqual, $point2->isEqualTo($point1));
214+
}
215+
216+
public static function providerIsEqualTo(): array
217+
{
218+
return [
219+
['POINT EMPTY', 0, 'POINT EMPTY', 0, true],
220+
['POINT EMPTY', 0, 'POINT EMPTY', 4326, false],
221+
['POINT Z EMPTY', 0, 'POINT Z EMPTY', 0, true],
222+
['POINT Z EMPTY', 0, 'POINT Z EMPTY', 4326, false],
223+
['POINT M EMPTY', 0, 'POINT M EMPTY', 0, true],
224+
['POINT M EMPTY', 0, 'POINT M EMPTY', 4326, false],
225+
['POINT ZM EMPTY', 0, 'POINT ZM EMPTY', 0, true],
226+
['POINT ZM EMPTY', 0, 'POINT ZM EMPTY', 4326, false],
227+
228+
['POINT EMPTY', 0, 'POINT Z EMPTY', 0, false],
229+
['POINT EMPTY', 0, 'POINT M EMPTY', 0, false],
230+
['POINT EMPTY', 0, 'POINT ZM EMPTY', 0, false],
231+
['POINT Z EMPTY', 0, 'POINT M EMPTY', 0, false],
232+
['POINT Z EMPTY', 0, 'POINT ZM EMPTY', 0, false],
233+
['POINT M EMPTY', 0, 'POINT ZM EMPTY', 0, false],
234+
235+
['POINT (1 2)', 0, 'POINT (1 2)', 0, true],
236+
['POINT (1 2)', 0, 'POINT (0 2)', 0, false],
237+
['POINT (1 2)', 0, 'POINT (1 0)', 0, false],
238+
['POINT (1 2)', 0, 'POINT (1 2)', 4326, false],
239+
240+
['POINT Z (1 2 3)', 0, 'POINT Z (1 2 3)', 0, true],
241+
['POINT Z (1 2 3)', 0, 'POINT Z (0 2 3)', 0, false],
242+
['POINT Z (1 2 3)', 0, 'POINT Z (1 0 3)', 0, false],
243+
['POINT Z (1 2 3)', 0, 'POINT Z (1 2 0)', 0, false],
244+
['POINT Z (1 2 3)', 0, 'POINT Z (1 2 3)', 4326, false],
245+
246+
['POINT M (1 2 3)', 0, 'POINT M (1 2 3)', 0, true],
247+
['POINT M (1 2 3)', 0, 'POINT M (0 2 3)', 0, false],
248+
['POINT M (1 2 3)', 0, 'POINT M (1 0 3)', 0, false],
249+
['POINT M (1 2 3)', 0, 'POINT M (1 2 0)', 0, false],
250+
['POINT M (1 2 3)', 0, 'POINT M (1 2 3)', 4326, false],
251+
252+
['POINT ZM (1 2 3 4)', 0, 'POINT ZM (1 2 3 4)', 0, true],
253+
['POINT ZM (1 2 3 4)', 0, 'POINT ZM (0 2 3 4)', 0, false],
254+
['POINT ZM (1 2 3 4)', 0, 'POINT ZM (1 0 3 4)', 0, false],
255+
['POINT ZM (1 2 3 4)', 0, 'POINT ZM (1 2 0 4)', 0, false],
256+
['POINT ZM (1 2 3 4)', 0, 'POINT ZM (1 2 3 0)', 0, false],
257+
['POINT ZM (1 2 3 4)', 0, 'POINT ZM (1 2 3 4)', 4326, false],
258+
259+
['POINT (1 2)', 0, 'POINT Z (1 2 0)', 0, false],
260+
['POINT (1 2)', 0, 'POINT M (1 2 0)', 0, false],
261+
['POINT (1 2)', 0, 'POINT ZM (1 2 0 0)', 0, false],
262+
['POINT Z (1 2 3)', 0, 'POINT M (1 2 3)', 0, false],
263+
['POINT Z (1 2 3)', 0, 'POINT ZM (1 2 3 4)', 0, false],
264+
['POINT M (1 2 3)', 0, 'POINT ZM (1 2 3 4)', 0, false],
265+
];
266+
}
267+
268+
#[DataProvider('providerIsSpatiallyEqualTo')]
269+
public function testIsSpatiallyEqualTo(string $wkt1, int $srid1, string $wkt2, int $srid2, bool $isEqual): void
270+
{
271+
$point1 = Point::fromText($wkt1, $srid1);
272+
$point2 = Point::fromText($wkt2, $srid2);
273+
274+
self::assertSame($isEqual, $point1->isSpatiallyEqualTo($point2));
275+
self::assertSame($isEqual, $point2->isSpatiallyEqualTo($point1));
276+
}
277+
278+
public static function providerIsSpatiallyEqualTo(): array
279+
{
280+
return [
281+
['POINT EMPTY', 0, 'POINT EMPTY', 0, true],
282+
['POINT EMPTY', 0, 'POINT EMPTY', 4326, false],
283+
['POINT Z EMPTY', 0, 'POINT Z EMPTY', 0, true],
284+
['POINT Z EMPTY', 0, 'POINT Z EMPTY', 4326, false],
285+
['POINT M EMPTY', 0, 'POINT M EMPTY', 0, true],
286+
['POINT M EMPTY', 0, 'POINT M EMPTY', 4326, false],
287+
['POINT ZM EMPTY', 0, 'POINT ZM EMPTY', 0, true],
288+
['POINT ZM EMPTY', 0, 'POINT ZM EMPTY', 4326, false],
289+
290+
['POINT EMPTY', 0, 'POINT Z EMPTY', 0, false],
291+
['POINT EMPTY', 0, 'POINT M EMPTY', 0, true],
292+
['POINT EMPTY', 0, 'POINT ZM EMPTY', 0, false],
293+
['POINT Z EMPTY', 0, 'POINT M EMPTY', 0, false],
294+
['POINT Z EMPTY', 0, 'POINT ZM EMPTY', 0, true],
295+
['POINT M EMPTY', 0, 'POINT ZM EMPTY', 0, false],
296+
297+
['POINT (1 2)', 0, 'POINT (1 2)', 0, true],
298+
['POINT (1 2)', 0, 'POINT (0 2)', 0, false],
299+
['POINT (1 2)', 0, 'POINT (1 0)', 0, false],
300+
['POINT (1 2)', 0, 'POINT (1 2)', 4326, false],
301+
302+
['POINT Z (1 2 3)', 0, 'POINT Z (1 2 3)', 0, true],
303+
['POINT Z (1 2 3)', 0, 'POINT Z (0 2 3)', 0, false],
304+
['POINT Z (1 2 3)', 0, 'POINT Z (1 0 3)', 0, false],
305+
['POINT Z (1 2 3)', 0, 'POINT Z (1 2 0)', 0, false],
306+
['POINT Z (1 2 3)', 0, 'POINT Z (1 2 3)', 4326, false],
307+
308+
['POINT M (1 2 3)', 0, 'POINT M (1 2 3)', 0, true],
309+
['POINT M (1 2 3)', 0, 'POINT M (0 2 3)', 0, false],
310+
['POINT M (1 2 3)', 0, 'POINT M (1 0 3)', 0, false],
311+
['POINT M (1 2 3)', 0, 'POINT M (1 2 0)', 0, true],
312+
['POINT M (1 2 3)', 0, 'POINT M (1 2 3)', 4326, false],
313+
314+
['POINT ZM (1 2 3 4)', 0, 'POINT ZM (1 2 3 4)', 0, true],
315+
['POINT ZM (1 2 3 4)', 0, 'POINT ZM (0 2 3 4)', 0, false],
316+
['POINT ZM (1 2 3 4)', 0, 'POINT ZM (1 0 3 4)', 0, false],
317+
['POINT ZM (1 2 3 4)', 0, 'POINT ZM (1 2 0 4)', 0, false],
318+
['POINT ZM (1 2 3 4)', 0, 'POINT ZM (1 2 3 0)', 0, true],
319+
['POINT ZM (1 2 3 4)', 0, 'POINT ZM (1 2 3 4)', 4326, false],
320+
321+
['POINT (1 2)', 0, 'POINT Z (1 2 0)', 0, false],
322+
['POINT (1 2)', 0, 'POINT M (1 2 0)', 0, true],
323+
['POINT (1 2)', 0, 'POINT ZM (1 2 0 0)', 0, false],
324+
['POINT Z (1 2 3)', 0, 'POINT M (1 2 3)', 0, false],
325+
['POINT Z (1 2 3)', 0, 'POINT ZM (1 2 3 4)', 0, true],
326+
['POINT M (1 2 3)', 0, 'POINT ZM (1 2 3 4)', 0, false],
327+
];
328+
}
205329
}

0 commit comments

Comments
 (0)