Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Geometry::fromArray added $srid optional paramter (and tests) #118

Merged
merged 1 commit into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion API.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
4 changes: 2 additions & 2 deletions src/Objects/Geometry.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
95 changes: 95 additions & 0 deletions tests/Objects/GeometryCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand All @@ -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([
Expand Down
22 changes: 22 additions & 0 deletions tests/Objects/LineStringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
26 changes: 26 additions & 0 deletions tests/Objects/MultiLineStringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down
20 changes: 20 additions & 0 deletions tests/Objects/MultiPointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
36 changes: 36 additions & 0 deletions tests/Objects/MultiPolygonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down
16 changes: 16 additions & 0 deletions tests/Objects/PointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
32 changes: 32 additions & 0 deletions tests/Objects/PolygonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down
Loading