From 7e726e4c63457295c4a39fb0f0c4c993b3c03857 Mon Sep 17 00:00:00 2001 From: julien Date: Tue, 2 Apr 2024 13:35:55 +0200 Subject: [PATCH] Geometry::fromArray added $srid optional paramter (and tests) --- API.md | 2 +- src/Objects/Geometry.php | 4 +- tests/Objects/GeometryCollectionTest.php | 95 ++++++++++++++++++++++++ tests/Objects/LineStringTest.php | 22 ++++++ tests/Objects/MultiLineStringTest.php | 26 +++++++ tests/Objects/MultiPointTest.php | 20 +++++ tests/Objects/MultiPolygonTest.php | 36 +++++++++ tests/Objects/PointTest.php | 16 ++++ tests/Objects/PolygonTest.php | 32 ++++++++ 9 files changed, 250 insertions(+), 3 deletions(-) diff --git a/API.md b/API.md index be90f03..d8bb72a 100644 --- a/API.md +++ b/API.md @@ -12,7 +12,7 @@ Geometry classes can be also created by these static methods: -* `fromArray(array $geometry)` - Creates a geometry object from a [GeoJSON](https://en.wikipedia.org/wiki/GeoJSON) array. +* `fromArray(array $geometry, int|Srid $srid = 0)` - Creates a geometry object from a [GeoJSON](https://en.wikipedia.org/wiki/GeoJSON) array. * `fromJson(string $geoJson, int|Srid $srid = 0)` - Creates a geometry object from a [GeoJSON](https://en.wikipedia.org/wiki/GeoJSON) string. * `fromWkt(string $wkt, int|Srid $srid = 0)` - Creates a geometry object from a [WKT](https://en.wikipedia.org/wiki/Well-known_text_representation_of_geometry). * `fromWkb(string $wkb)` - Creates a geometry object from a [WKB](https://en.wikipedia.org/wiki/Well-known_text_representation_of_geometry#Well-known_binary). diff --git a/src/Objects/Geometry.php b/src/Objects/Geometry.php index 864b8a8..d7ef0e7 100644 --- a/src/Objects/Geometry.php +++ b/src/Objects/Geometry.php @@ -126,11 +126,11 @@ public static function fromJson(string $geoJson, int|Srid $srid = 0): static * * @throws JsonException */ - public static function fromArray(array $geometry): static + public static function fromArray(array $geometry, int|Srid $srid = 0): static { $geoJson = json_encode($geometry, JSON_THROW_ON_ERROR); - return static::fromJson($geoJson); + return static::fromJson($geoJson, $srid); } /** diff --git a/tests/Objects/GeometryCollectionTest.php b/tests/Objects/GeometryCollectionTest.php index 2af8ead..54c94d2 100644 --- a/tests/Objects/GeometryCollectionTest.php +++ b/tests/Objects/GeometryCollectionTest.php @@ -107,6 +107,44 @@ expect($geometryCollectionFromJson)->toEqual($geometryCollection); }); +it('creates geometry collection from Array', function (): void { + $geometryCollection = new GeometryCollection([ + new Polygon([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + new Point(2, 178), + new Point(3, 177), + new Point(0, 180), + ]), + ]), + new Point(0, 180), + ]); + + $geometryCollectionFromJson = GeometryCollection::fromArray(json_decode('{"type":"GeometryCollection","geometries":[{"type":"Polygon","coordinates":[[[180,0],[179,1],[178,2],[177,3],[180,0]]]},{"type":"Point","coordinates":[180,0]}]}',true)); + + expect($geometryCollectionFromJson)->toEqual($geometryCollection); +}); + +it('creates geometry collection with SRID from Array', function (): void { + $geometryCollection = new GeometryCollection([ + new Polygon([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + new Point(2, 178), + new Point(3, 177), + new Point(0, 180), + ]), + ]), + new Point(0, 180), + ], Srid::WGS84->value); + + $geometryCollectionFromJson = GeometryCollection::fromArray(json_decode('{"type":"GeometryCollection","geometries":[{"type":"Polygon","coordinates":[[[180,0],[179,1],[178,2],[177,3],[180,0]]]},{"type":"Point","coordinates":[180,0]}]}',true), Srid::WGS84->value); + + expect($geometryCollectionFromJson)->toEqual($geometryCollection); +}); + it('creates geometry collection from feature collection JSON', function (): void { $geometryCollection = new GeometryCollection([ new Polygon([ @@ -126,6 +164,63 @@ expect($geometryCollectionFromFeatureCollectionJson)->toEqual($geometryCollection); }); +it('creates geometry collection from feature collection with SRID from JSON', function (): void { + $geometryCollection = new GeometryCollection([ + new Polygon([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + new Point(2, 178), + new Point(3, 177), + new Point(0, 180), + ]), + ]), + new Point(0, 180), + ], Srid::WGS84); + + $geometryCollectionFromFeatureCollectionJson = GeometryCollection::fromJson('{"type":"FeatureCollection","features":[{"type":"Feature","properties":[],"geometry":{"type":"Polygon","coordinates":[[[180,0],[179,1],[178,2],[177,3],[180,0]]]}},{"type":"Feature","properties":[],"geometry":{"type":"Point","coordinates":[180,0]}}]}', Srid::WGS84); + + expect($geometryCollectionFromFeatureCollectionJson)->toEqual($geometryCollection); +}); + +it('creates geometry collection from feature collection from Array', function (): void { + $geometryCollection = new GeometryCollection([ + new Polygon([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + new Point(2, 178), + new Point(3, 177), + new Point(0, 180), + ]), + ]), + new Point(0, 180), + ]); + + $geometryCollectionFromFeatureCollectionJson = GeometryCollection::fromArray(json_decode('{"type":"FeatureCollection","features":[{"type":"Feature","properties":[],"geometry":{"type":"Polygon","coordinates":[[[180,0],[179,1],[178,2],[177,3],[180,0]]]}},{"type":"Feature","properties":[],"geometry":{"type":"Point","coordinates":[180,0]}}]}',true)); + + expect($geometryCollectionFromFeatureCollectionJson)->toEqual($geometryCollection); +}); + +it('creates geometry collection from feature collection with SRID from Array', function (): void { + $geometryCollection = new GeometryCollection([ + new Polygon([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + new Point(2, 178), + new Point(3, 177), + new Point(0, 180), + ]), + ]), + new Point(0, 180), + ], Srid::WGS84); + + $geometryCollectionFromFeatureCollectionJson = GeometryCollection::fromArray(json_decode('{"type":"FeatureCollection","features":[{"type":"Feature","properties":[],"geometry":{"type":"Polygon","coordinates":[[[180,0],[179,1],[178,2],[177,3],[180,0]]]}},{"type":"Feature","properties":[],"geometry":{"type":"Point","coordinates":[180,0]}}]}',true), Srid::WGS84); + + expect($geometryCollectionFromFeatureCollectionJson)->toEqual($geometryCollection); +}); + it('generates geometry collection JSON', function (): void { $geometryCollection = new GeometryCollection([ new Polygon([ diff --git a/tests/Objects/LineStringTest.php b/tests/Objects/LineStringTest.php index 2ba5b93..7c1aad2 100644 --- a/tests/Objects/LineStringTest.php +++ b/tests/Objects/LineStringTest.php @@ -66,6 +66,28 @@ expect($lineStringFromJson)->toEqual($lineString); }); +it('creates line string from Array', function (): void { + $lineString = new LineString([ + new Point(0, 180), + new Point(1, 179), + ]); + + $lineStringFromJson = LineString::fromArray(["type"=>"LineString","coordinates"=>[[180,0],[179,1]]]); + + expect($lineStringFromJson)->toEqual($lineString); +}); + +it('creates line string with SRID from Array', function (): void { + $lineString = new LineString([ + new Point(0, 180), + new Point(1, 179), + ], Srid::WGS84->value); + + $lineStringFromJson = LineString::fromArray(["type"=>"LineString","coordinates"=>[[180,0],[179,1]]], Srid::WGS84->value); + + expect($lineStringFromJson)->toEqual($lineString); +}); + it('generates line string JSON', function (): void { $lineString = new LineString([ new Point(0, 180), diff --git a/tests/Objects/MultiLineStringTest.php b/tests/Objects/MultiLineStringTest.php index 5e48b7e..f79fd48 100644 --- a/tests/Objects/MultiLineStringTest.php +++ b/tests/Objects/MultiLineStringTest.php @@ -76,6 +76,32 @@ expect($multiLineStringFromJson)->toEqual($multiLineString); }); +it('creates multi line string from Array', function (): void { + $multiLineString = new MultiLineString([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + ]), + ]); + + $multiLineStringFromJson = MultiLineString::fromArray(["type"=>"MultiLineString","coordinates"=>[[[180,0],[179,1]]]]); + + expect($multiLineStringFromJson)->toEqual($multiLineString); +}); + +it('creates multi line string with SRID from Array', function (): void { + $multiLineString = new MultiLineString([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + ]), + ], Srid::WGS84->value); + + $multiLineStringFromJson = MultiLineString::fromArray(["type"=>"MultiLineString","coordinates"=>[[[180,0],[179,1]]]], Srid::WGS84->value); + + expect($multiLineStringFromJson)->toEqual($multiLineString); +}); + it('generates multi line string JSON', function (): void { $multiLineString = new MultiLineString([ new LineString([ diff --git a/tests/Objects/MultiPointTest.php b/tests/Objects/MultiPointTest.php index 05069e9..4edc7be 100644 --- a/tests/Objects/MultiPointTest.php +++ b/tests/Objects/MultiPointTest.php @@ -61,6 +61,26 @@ expect($multiPointFromJson)->toEqual($multiPoint); }); +it('creates multi point from Array', function (): void { + $multiPoint = new MultiPoint([ + new Point(0, 180), + ]); + + $multiPointFromJson = MultiPoint::fromArray(["type"=>"MultiPoint","coordinates"=>[[180,0]]]); + + expect($multiPointFromJson)->toEqual($multiPoint); +}); + +it('creates multi point with SRID from Array', function (): void { + $multiPoint = new MultiPoint([ + new Point(0, 180), + ], Srid::WGS84->value); + + $multiPointFromJson = MultiPoint::fromArray(["type"=>"MultiPoint","coordinates"=>[[180,0]]], Srid::WGS84->value); + + expect($multiPointFromJson)->toEqual($multiPoint); +}); + it('generates multi point JSON', function (): void { $multiPoint = new MultiPoint([ new Point(0, 180), diff --git a/tests/Objects/MultiPolygonTest.php b/tests/Objects/MultiPolygonTest.php index d85ae6a..6548697 100644 --- a/tests/Objects/MultiPolygonTest.php +++ b/tests/Objects/MultiPolygonTest.php @@ -102,6 +102,42 @@ expect($multiPolygonFromJson)->toEqual($multiPolygon); }); +it('creates multi polygon from Array', function (): void { + $multiPolygon = new MultiPolygon([ + new Polygon([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + new Point(2, 178), + new Point(3, 177), + new Point(0, 180), + ]), + ]), + ]); + + $multiPolygonFromJson = MultiPolygon::fromArray(["type"=>"MultiPolygon","coordinates"=>[[[[180,0],[179,1],[178,2],[177,3],[180,0]]]]]); + + expect($multiPolygonFromJson)->toEqual($multiPolygon); +}); + +it('creates multi polygon with SRID from Array', function (): void { + $multiPolygon = new MultiPolygon([ + new Polygon([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + new Point(2, 178), + new Point(3, 177), + new Point(0, 180), + ]), + ]), + ], Srid::WGS84->value); + + $multiPolygonFromJson = MultiPolygon::fromArray(["type"=>"MultiPolygon","coordinates"=>[[[[180,0],[179,1],[178,2],[177,3],[180,0]]]]], Srid::WGS84->value); + + expect($multiPolygonFromJson)->toEqual($multiPolygon); +}); + it('generates multi polygon JSON', function (): void { $multiPolygon = new MultiPolygon([ new Polygon([ diff --git a/tests/Objects/PointTest.php b/tests/Objects/PointTest.php index 69e5281..141b2d8 100644 --- a/tests/Objects/PointTest.php +++ b/tests/Objects/PointTest.php @@ -49,6 +49,22 @@ expect($pointFromJson)->toEqual($point); }); +it('creates point from Array', function (): void { + $point = new Point(0, 180); + + $pointFromJson = Point::fromArray(["type"=>"Point","coordinates"=>[180,0]]); + + expect($pointFromJson)->toEqual($point); +}); + +it('creates point with SRID from Array', function (): void { + $point = new Point(0, 180, Srid::WGS84->value); + + $pointFromJson = Point::fromArray(["type"=>"Point","coordinates"=>[180,0]], Srid::WGS84->value); + + expect($pointFromJson)->toEqual($point); +}); + it('generates point JSON', function (): void { $point = new Point(0, 180); diff --git a/tests/Objects/PolygonTest.php b/tests/Objects/PolygonTest.php index df356d8..370881d 100644 --- a/tests/Objects/PolygonTest.php +++ b/tests/Objects/PolygonTest.php @@ -91,6 +91,38 @@ expect($polygonFromJson)->toEqual($polygon); }); +it('creates polygon from Array', function (): void { + $polygon = new Polygon([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + new Point(2, 178), + new Point(3, 177), + new Point(0, 180), + ]), + ]); + + $polygonFromJson = Polygon::fromArray(["type"=>"Polygon","coordinates"=>[[[180,0],[179,1],[178,2],[177,3],[180,0]]]]); + + expect($polygonFromJson)->toEqual($polygon); +}); + +it('creates polygon with SRID from Array', function (): void { + $polygon = new Polygon([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + new Point(2, 178), + new Point(3, 177), + new Point(0, 180), + ]), + ], Srid::WGS84->value); + + $polygonFromJson = Polygon::fromArray(["type"=>"Polygon","coordinates"=>[[[180,0],[179,1],[178,2],[177,3],[180,0]]]], Srid::WGS84->value); + + expect($polygonFromJson)->toEqual($polygon); +}); + it('generates polygon JSON', function (): void { $polygon = new Polygon([ new LineString([