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

Allow a GeometryCollection to be constructed from a GeometryCollection GeoJSON #150

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
24 changes: 22 additions & 2 deletions src/Types/GeometryCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Countable;
use GeoJson\Feature\FeatureCollection;
use GeoJson\GeoJson;
use GeoJson\Geometry\GeometryCollection as GeometryGeometryCollection;
use Grimzy\LaravelMysqlSpatial\Exceptions\InvalidGeoJsonException;
use Illuminate\Contracts\Support\Arrayable;
use InvalidArgumentException;
Expand Down Expand Up @@ -129,10 +130,19 @@ public static function fromJson($geoJson)
$geoJson = GeoJson::jsonUnserialize(json_decode($geoJson));
}

if (!is_a($geoJson, FeatureCollection::class)) {
throw new InvalidGeoJsonException('Expected '.FeatureCollection::class.', got '.get_class($geoJson));
if (is_a($geoJson, FeatureCollection::class)) {
return self::featureCollectionFromJson($geoJson);
}

if (is_a($geoJson, GeometryGeometryCollection::class)) {
return self::geometryCollectionFromJson($geoJson);
}

throw new InvalidGeoJsonException('Expected '.FeatureCollection::class.' or '.GeometryGeometryCollection::class.', got '.get_class($geoJson));
}

protected static function featureCollectionFromJson(FeatureCollection $geoJson)
{
$set = [];
foreach ($geoJson->getFeatures() as $feature) {
$set[] = parent::fromJson($feature);
Expand All @@ -141,6 +151,16 @@ public static function fromJson($geoJson)
return new self($set);
}

protected static function geometryCollectionFromJson(GeometryGeometryCollection $geoJson)
{
$set = [];
foreach ($geoJson->getGeometries() as $feature) {
$set[] = parent::fromJson($feature);
}

return new self($set);
}

/**
* Convert to GeoJson GeometryCollection that is jsonable to GeoJSON.
*
Expand Down
15 changes: 13 additions & 2 deletions tests/Unit/Types/GeometryCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public function testArrayAccess()
$geometryCollection[] = 1;
}

public function testFromJson()
public function testFeatureCollectionFromJson()
{
$geometryCollection = GeometryCollection::fromJson('{"type":"FeatureCollection","features":[{"type":"Feature","properties":{},"geometry":{"type":"Point","coordinates":[1,2]}},{"type":"Feature","properties":{},"geometry":{"type":"Point","coordinates":[3,4]}}]}');
$this->assertInstanceOf(GeometryCollection::class, $geometryCollection);
Expand All @@ -131,11 +131,22 @@ public function testInvalidGeoJsonException()
{
$this->assertException(
\Grimzy\LaravelMysqlSpatial\Exceptions\InvalidGeoJsonException::class,
sprintf('Expected %s, got %s', GeoJson\Feature\FeatureCollection::class, GeoJson\Geometry\Point::class)
sprintf('Expected %s or %s, got %s', GeoJson\Feature\FeatureCollection::class, GeoJson\Geometry\GeometryCollection::class, GeoJson\Geometry\Point::class)
);
GeometryCollection::fromJson('{"type":"Point","coordinates":[3.4,1.2]}');
}

public function testGeometryCollectionFromJson()
{
$geometryCollection = GeometryCollection::fromJson('{"type":"GeometryCollection","geometries":[{"type":"Point","coordinates":[30,30]},{"type":"LineString","coordinates":[[0,0],[10,10],[20,20]]}]}');
$this->assertInstanceOf(GeometryCollection::class, $geometryCollection);
$geometryCollectionPoints = $geometryCollection->getGeometries();
$this->assertEquals(2, count($geometryCollectionPoints));
$this->assertEquals(new Point(30, 30), $geometryCollectionPoints[0]);
$this->assertEquals(new LineString([new Point(0, 0), new Point(10, 10), new Point(20, 20)]), $geometryCollectionPoints[1]);
$this->assertJsonStringEqualsJsonString('{"type":"GeometryCollection","geometries":[{"type":"Point","coordinates":[30,30]},{"type":"LineString","coordinates":[[0,0],[10,10],[20,20]]}]}', $geometryCollection->toJson());
}

private function getGeometryCollection()
{
return new GeometryCollection([$this->getLineString(), $this->getPoint()]);
Expand Down