Skip to content

Commit

Permalink
add toGeoJson(), close grimzy#113
Browse files Browse the repository at this point in the history
  • Loading branch information
limenet committed Oct 1, 2020
1 parent f70a82e commit 086b012
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 1 deletion.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,16 @@ $point = new Point(40.7484404, -73.9878441);

json_encode($point); // or $point->toJson();

// {
// "type": "Point",
// "coordinates": [
// -73.9878441,
// 40.7484404
// ]
// }

$point->toGeoJson();

// {
// "type": "Feature",
// "properties": {},
Expand Down
10 changes: 9 additions & 1 deletion src/Types/Geometry.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Grimzy\LaravelMysqlSpatial\Types;

use GeoIO\WKB\Parser\Parser;
use GeoJson\Feature\Feature;
use GeoJson\GeoJson;
use Grimzy\LaravelMysqlSpatial\Exceptions\UnknownWKTTypeException;
use Illuminate\Contracts\Support\Jsonable;
Expand Down Expand Up @@ -108,7 +109,7 @@ public static function fromJson($geoJson)
$geoJson = $geoJson->getGeometry();
}

$type = '\Grimzy\LaravelMysqlSpatial\Types\\'.$geoJson->getType();
$type = '\Grimzy\LaravelMysqlSpatial\Types\\' . $geoJson->getType();

return $type::fromJson($geoJson);
}
Expand All @@ -117,4 +118,11 @@ public function toJson($options = 0)
{
return json_encode($this, $options);
}

public function toGeoJson($properties = [], $options = 0)
{
$feature = new Feature($this->jsonSerialize(), $properties);

return json_encode($feature->jsonSerialize(), $options);
}
}
9 changes: 9 additions & 0 deletions src/Types/GeometryCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,15 @@ public function jsonSerialize()
return new \GeoJson\Geometry\GeometryCollection($geometries);
}

public function toGeoJson($properties = [], $options = 0)
{
if (static::class !== GeometryCollection::class) {
return parent::toGeoJson();
}

return json_encode($this->jsonSerialize(), $options);
}

/**
* Checks whether the items are valid to create this collection.
*
Expand Down
6 changes: 6 additions & 0 deletions tests/Unit/Types/GeometryCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ public function testJsonSerialize()
$this->assertSame('{"type":"GeometryCollection","geometries":[{"type":"LineString","coordinates":[[0,0],[1,0],[1,1],[0,1],[0,0]]},{"type":"Point","coordinates":[200,100]}]}', json_encode($this->getGeometryCollection()->jsonSerialize()));
}

public function testToGeoJson()
{
$this->assertJson($this->getGeometryCollection()->toGeoJson());
$this->assertJsonStringEqualsJsonString('{"type":"GeometryCollection","geometries":[{"type":"LineString","coordinates":[[0,0],[1,0],[1,1],[0,1],[0,0]]},{"type":"Point","coordinates":[200,100]}]}', $this->getGeometryCollection()->toGeoJson());
}

public function testCanCreateEmptyGeometryCollection()
{
$geometryCollection = new GeometryCollection([]);
Expand Down
8 changes: 8 additions & 0 deletions tests/Unit/Types/LineStringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,12 @@ public function testJsonSerialize()
$this->assertInstanceOf(\GeoJson\Geometry\LineString::class, $lineString->jsonSerialize());
$this->assertSame('{"type":"LineString","coordinates":[[0,0],[1,1],[2,2]]}', json_encode($lineString));
}

public function testToGeoJson()
{
$lineString = new LineString($this->points);

$this->assertJson($lineString->toGeoJson());
$this->assertJsonStringEqualsJsonString('{"type":"Feature","geometry":{"type":"LineString","coordinates":[[0,0],[1,1],[2,2]]},"properties":{}}', $lineString->toGeoJson());
}
}
8 changes: 8 additions & 0 deletions tests/Unit/Types/MultiLineStringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ public function testJsonSerialize()
$this->assertSame('{"type":"MultiLineString","coordinates":[[[0,0],[1,1],[1,2]],[[2,3],[3,2],[5,4]]]}', json_encode($multilinestring));
}

public function testToGeoJson()
{
$multilinestring = MultiLineString::fromWKT('MULTILINESTRING((0 0,1 1,1 2),(2 3,3 2,5 4))');

$this->assertJson($multilinestring->toGeoJson());
$this->assertJsonStringEqualsJsonString('{"type":"Feature","geometry":{"type":"MultiLineString","coordinates":[[[0,0],[1,1],[1,2]],[[2,3],[3,2],[5,4]]]},"properties":{}}', $multilinestring->toGeoJson());
}

public function testInvalidArgumentExceptionAtLeastOneEntry()
{
$this->assertException(
Expand Down
10 changes: 10 additions & 0 deletions tests/Unit/Types/MultiPointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ public function testJsonSerialize()
$this->assertSame('{"type":"MultiPoint","coordinates":[[0,0],[1,0],[1,1]]}', json_encode($multipoint));
}

public function testToGeoJson()
{
$collection = [new Point(0, 0), new Point(0, 1), new Point(1, 1)];

$multipoint = new MultiPoint($collection);

$this->assertJson($multipoint->toGeoJson());
$this->assertJsonStringEqualsJsonString('{"type":"Feature","geometry":{"type":"MultiPoint","coordinates":[[0,0],[1,0],[1,1]]},"properties":{}}', $multipoint->toGeoJson());
}

public function testInvalidArgumentExceptionAtLeastOneEntry()
{
$this->assertException(
Expand Down
7 changes: 7 additions & 0 deletions tests/Unit/Types/MultiPolygonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,11 @@ private function getPolygon3()
]),
]);
}

public function testToGeoJson()
{
$multiPolygon = MultiPolygon::fromJson('{"type":"MultiPolygon","coordinates":[[[[1,1],[1,2],[2,2],[2,1],[1,1]]],[[[0,0],[0,1],[1,1],[1,0],[0,0]]]]}');
$this->assertJson($multiPolygon->toGeoJson());
$this->assertJsonStringEqualsJsonString('{"type":"Feature","geometry":{"type":"MultiPolygon","coordinates":[[[[1,1],[1,2],[2,2],[2,1],[1,1]]],[[[0,0],[0,1],[1,1],[1,0],[0,0]]]]},"properties":{}}', $multiPolygon->toGeoJson());
}
}
19 changes: 19 additions & 0 deletions tests/Unit/Types/PointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,23 @@ public function testJsonSerialize()
$this->assertInstanceOf(\GeoJson\Geometry\Point::class, $point->jsonSerialize());
$this->assertSame('{"type":"Point","coordinates":[3.4,1.2]}', json_encode($point));
}

public function testToGeoJson()
{
$point = new Point(1.2, 3.4);

$this->assertJson($point->toGeoJson());
$this->assertJsonStringEqualsJsonString('{"type":"Feature","geometry":{"type":"Point","coordinates":[3.4,1.2]},"properties":{}}
', $point->toGeoJson());
}

public function testToGeoJsonWithProperties()
{
$point = new Point(1.2, 3.4);

$properties = ['key' => 'value', 'key_1' => true, 'key_2' => 0, 'key_3' => null, 'key_4' => [1, 2, 'a']];

$this->assertJson($point->toGeoJson($properties));
$this->assertJsonStringEqualsJsonString('{"type":"Feature","geometry":{"type":"Point","coordinates":[3.4,1.2]},"properties":{"key":"value","key_1":true,"key_2":0,"key_3":null,"key_4":[1,2,"a"]}}', $point->toGeoJson($properties));
}
}
6 changes: 6 additions & 0 deletions tests/Unit/Types/PolygonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,10 @@ public function testJsonSerialize()
json_encode($this->polygon)
);
}

public function testToGeoJson()
{
$this->assertJson($this->polygon->toGeoJson());
$this->assertJsonStringEqualsJsonString('{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[0,0],[1,0],[1,1],[0,1],[0,0]]]},"properties":{}}', $this->polygon->toGeoJson());
}
}

0 comments on commit 086b012

Please sign in to comment.