diff --git a/README.md b/README.md index cab18d80..53ac1961 100644 --- a/README.md +++ b/README.md @@ -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": {}, diff --git a/src/Types/Geometry.php b/src/Types/Geometry.php index f840874c..84ffc7a9 100644 --- a/src/Types/Geometry.php +++ b/src/Types/Geometry.php @@ -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; @@ -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); + } } diff --git a/src/Types/GeometryCollection.php b/src/Types/GeometryCollection.php index 35f093f7..489c9856 100755 --- a/src/Types/GeometryCollection.php +++ b/src/Types/GeometryCollection.php @@ -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. * diff --git a/tests/Unit/Types/GeometryCollectionTest.php b/tests/Unit/Types/GeometryCollectionTest.php index a0d6f016..f3ca8679 100644 --- a/tests/Unit/Types/GeometryCollectionTest.php +++ b/tests/Unit/Types/GeometryCollectionTest.php @@ -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([]); diff --git a/tests/Unit/Types/LineStringTest.php b/tests/Unit/Types/LineStringTest.php index ce5713d2..e836f7f6 100644 --- a/tests/Unit/Types/LineStringTest.php +++ b/tests/Unit/Types/LineStringTest.php @@ -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()); + } } diff --git a/tests/Unit/Types/MultiLineStringTest.php b/tests/Unit/Types/MultiLineStringTest.php index 16477feb..37951b77 100644 --- a/tests/Unit/Types/MultiLineStringTest.php +++ b/tests/Unit/Types/MultiLineStringTest.php @@ -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( diff --git a/tests/Unit/Types/MultiPointTest.php b/tests/Unit/Types/MultiPointTest.php index a8a94f41..7ed2cadb 100644 --- a/tests/Unit/Types/MultiPointTest.php +++ b/tests/Unit/Types/MultiPointTest.php @@ -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( diff --git a/tests/Unit/Types/MultiPolygonTest.php b/tests/Unit/Types/MultiPolygonTest.php index 3e49d32d..ffd2effd 100644 --- a/tests/Unit/Types/MultiPolygonTest.php +++ b/tests/Unit/Types/MultiPolygonTest.php @@ -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()); + } } diff --git a/tests/Unit/Types/PointTest.php b/tests/Unit/Types/PointTest.php index 518a8a56..bd2f147b 100644 --- a/tests/Unit/Types/PointTest.php +++ b/tests/Unit/Types/PointTest.php @@ -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)); + } } diff --git a/tests/Unit/Types/PolygonTest.php b/tests/Unit/Types/PolygonTest.php index aaab437b..6942a4a0 100644 --- a/tests/Unit/Types/PolygonTest.php +++ b/tests/Unit/Types/PolygonTest.php @@ -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()); + } }