From 4ca6a89a6f4a1cb2be165203e3e9349dc15862a8 Mon Sep 17 00:00:00 2001 From: Joseph Estefane Date: Sun, 1 Oct 2017 10:02:40 +0200 Subject: [PATCH 1/4] Added fromJson($geoJson) to GeometryInterface (#23) (cherry picked from commit 8e0119e7da42cd2ab59f21cacdc2ae7009abd8ef) --- src/Exceptions/InvalidGeoJsonException.php | 7 ++ src/Types/Geometry.php | 18 +++ src/Types/GeometryCollection.php | 21 ++++ src/Types/GeometryInterface.php | 2 + src/Types/LineString.php | 24 +++- src/Types/MultiLineString.php | 27 ++++- src/Types/MultiPoint.php | 24 +++- src/Types/MultiPolygon.php | 31 ++++- src/Types/Point.php | 24 +++- src/Types/Polygon.php | 28 ++++- tests/Unit/Eloquent/SpatialTraitTest.php | 18 +++ .../Unit/Schema/Grammars/MySqlGrammarTest.php | 20 ++++ tests/Unit/Types/GeometryCollectionTest.php | 14 +++ tests/Unit/Types/GeometryTest.php | 107 ++++++++++++++++++ tests/Unit/Types/LineStringTest.php | 13 +++ tests/Unit/Types/MultiLineStringTest.php | 18 +++ tests/Unit/Types/MultiPointTest.php | 15 +++ tests/Unit/Types/MultiPolygonTest.php | 26 +++++ tests/Unit/Types/PointTest.php | 12 ++ tests/Unit/Types/PolygonTest.php | 22 ++++ 20 files changed, 465 insertions(+), 6 deletions(-) create mode 100644 src/Exceptions/InvalidGeoJsonException.php diff --git a/src/Exceptions/InvalidGeoJsonException.php b/src/Exceptions/InvalidGeoJsonException.php new file mode 100644 index 00000000..f913cf04 --- /dev/null +++ b/src/Exceptions/InvalidGeoJsonException.php @@ -0,0 +1,7 @@ +getType() === 'FeatureCollection') { + return GeometryCollection::fromJson($geoJson); + } + + if($geoJson->getType() === 'Feature') { + $geoJson = $geoJson->getGeometry(); + } + + $type = '\Grimzy\LaravelMysqlSpatial\Types\\'.$geoJson->getType(); + return $type::fromJson($geoJson); + } + public function toJson($options = 0) { return json_encode($this, $options); diff --git a/src/Types/GeometryCollection.php b/src/Types/GeometryCollection.php index 3f857dd4..26a4b439 100755 --- a/src/Types/GeometryCollection.php +++ b/src/Types/GeometryCollection.php @@ -5,6 +5,9 @@ use ArrayAccess; use ArrayIterator; use Countable; +use GeoJson\Feature\FeatureCollection; +use GeoJson\GeoJson; +use Grimzy\LaravelMysqlSpatial\Exceptions\InvalidGeoJsonException; use Illuminate\Contracts\Support\Arrayable; use InvalidArgumentException; use IteratorAggregate; @@ -107,6 +110,24 @@ public function count() return count($this->items); } + public static function fromJson ($geoJson) + { + if(is_string($geoJson)) { + $geoJson = GeoJson::jsonUnserialize(json_decode($geoJson)); + } + + if(!is_a($geoJson, FeatureCollection::class)) { + throw new InvalidGeoJsonException('Expected ' . FeatureCollection::class . ', got ' . get_class($geoJson)); + } + + $set = []; + foreach ($geoJson->getFeatures() as $feature) { + $set[] = parent::fromJson($feature); + } + + return new GeometryCollection($set); + } + /** * Convert to GeoJson GeometryCollection that is jsonable to GeoJSON. * diff --git a/src/Types/GeometryInterface.php b/src/Types/GeometryInterface.php index 330ec927..aca2bfb0 100644 --- a/src/Types/GeometryInterface.php +++ b/src/Types/GeometryInterface.php @@ -11,4 +11,6 @@ public static function fromWKT($wkt); public function __toString(); public static function fromString($wktArgument); + + public static function fromJson($geoJson); } diff --git a/src/Types/LineString.php b/src/Types/LineString.php index 64f64338..59cd3662 100644 --- a/src/Types/LineString.php +++ b/src/Types/LineString.php @@ -2,6 +2,10 @@ namespace Grimzy\LaravelMysqlSpatial\Types; +use GeoJson\GeoJson; +use GeoJson\Geometry\LineString as GeoJsonLineString; +use Grimzy\LaravelMysqlSpatial\Exceptions\InvalidGeoJsonException; + class LineString extends PointCollection { public function toWKT() @@ -31,6 +35,24 @@ public function __toString() return $this->toPairList(); } + public static function fromJson ($geoJson) + { + if(is_string($geoJson)) { + $geoJson = GeoJson::jsonUnserialize(json_decode($geoJson)); + } + + if(!is_a($geoJson, GeoJsonLineString::class)) { + throw new InvalidGeoJsonException('Expected ' . GeoJsonLineString::class . ', got ' . get_class($geoJson)); + } + + $set = []; + foreach($geoJson->getCoordinates() as $coordinate) { + $set[] = new Point($coordinate[1], $coordinate[0]); + } + + return new LineString($set); + } + /** * Convert to GeoJson LineString that is jsonable to GeoJSON. * @@ -43,6 +65,6 @@ public function jsonSerialize() $points[] = $point->jsonSerialize(); } - return new \GeoJson\Geometry\LineString($points); + return new GeoJsonLineString($points); } } diff --git a/src/Types/MultiLineString.php b/src/Types/MultiLineString.php index 7d8dcd18..025fb4e3 100644 --- a/src/Types/MultiLineString.php +++ b/src/Types/MultiLineString.php @@ -2,6 +2,9 @@ namespace Grimzy\LaravelMysqlSpatial\Types; +use GeoJson\GeoJson; +use GeoJson\Geometry\MultiLineString as GeoJsonMultiLineString; +use Grimzy\LaravelMysqlSpatial\Exceptions\InvalidGeoJsonException; use InvalidArgumentException; class MultiLineString extends GeometryCollection @@ -62,6 +65,28 @@ public function offsetSet($offset, $value) parent::offsetSet($offset, $value); } + public static function fromJson ($geoJson) + { + if(is_string($geoJson)) { + $geoJson = GeoJson::jsonUnserialize(json_decode($geoJson)); + } + + if(!is_a($geoJson, GeoJsonMultiLineString::class)) { + throw new InvalidGeoJsonException('Expected ' . GeoJsonMultiLineString::class . ', got ' . get_class($geoJson)); + } + + $set = []; + foreach($geoJson->getCoordinates() as $coordinates) { + $points = []; + foreach($coordinates as $coordinate) { + $points[] = new Point($coordinate[1], $coordinate[0]); + } + $set[] = new LineString($points); + } + + return new MultiLineString($set); + } + /** * Convert to GeoJson Point that is jsonable to GeoJSON. * @@ -75,6 +100,6 @@ public function jsonSerialize() $lineStrings[] = $lineString->jsonSerialize(); } - return new \GeoJson\Geometry\MultiLineString($lineStrings); + return new GeoJsonMultiLineString($lineStrings); } } diff --git a/src/Types/MultiPoint.php b/src/Types/MultiPoint.php index 8e0b0f82..60fad9a5 100644 --- a/src/Types/MultiPoint.php +++ b/src/Types/MultiPoint.php @@ -2,6 +2,10 @@ namespace Grimzy\LaravelMysqlSpatial\Types; +use GeoJson\GeoJson; +use GeoJson\Geometry\MultiPoint as GeoJsonMultiPoint; +use Grimzy\LaravelMysqlSpatial\Exceptions\InvalidGeoJsonException; + class MultiPoint extends PointCollection { public function toWKT() @@ -35,6 +39,24 @@ public function __toString() }, $this->items)); } + public static function fromJson ($geoJson) + { + if(is_string($geoJson)) { + $geoJson = GeoJson::jsonUnserialize(json_decode($geoJson)); + } + + if(!is_a($geoJson, GeoJsonMultiPoint::class)) { + throw new InvalidGeoJsonException('Expected ' . GeoJsonMultiPoint::class . ', got ' . get_class($geoJson)); + } + + $set = []; + foreach($geoJson->getCoordinates() as $coordinate) { + $set[] = new Point($coordinate[1], $coordinate[0]); + } + + return new MultiPoint($set); + } + /** * Convert to GeoJson MultiPoint that is jsonable to GeoJSON. * @@ -47,6 +69,6 @@ public function jsonSerialize() $points[] = $point->jsonSerialize(); } - return new \GeoJson\Geometry\MultiPoint($points); + return new GeoJsonMultiPoint($points); } } diff --git a/src/Types/MultiPolygon.php b/src/Types/MultiPolygon.php index 36f0937e..927df911 100644 --- a/src/Types/MultiPolygon.php +++ b/src/Types/MultiPolygon.php @@ -2,6 +2,9 @@ namespace Grimzy\LaravelMysqlSpatial\Types; +use GeoJson\GeoJson; +use GeoJson\Geometry\MultiPolygon as GeoJsonMultiPolygon; +use Grimzy\LaravelMysqlSpatial\Exceptions\InvalidGeoJsonException; use InvalidArgumentException; class MultiPolygon extends GeometryCollection @@ -97,6 +100,32 @@ public function offsetSet($offset, $value) parent::offsetSet($offset, $value); } + public static function fromJson ($geoJson) + { + if(is_string($geoJson)) { + $geoJson = GeoJson::jsonUnserialize(json_decode($geoJson)); + } + + if(!is_a($geoJson, GeoJsonMultiPolygon::class)) { + throw new InvalidGeoJsonException('Expected ' . GeoJsonMultiPolygon::class . ', got ' . get_class($geoJson)); + } + + $set = []; + foreach($geoJson->getCoordinates() as $polygonCoordinates) { + $lineStrings = []; + foreach($polygonCoordinates as $lineStringCoordinates) { + $points = []; + foreach($lineStringCoordinates as $lineStringCoordinate) { + $points[] = new Point($lineStringCoordinate[1], $lineStringCoordinate[0]); + } + $lineStrings[] = new LineString($points); + } + $set[] = new Polygon($lineStrings); + } + + return new MultiPolygon($set); + } + /** * Convert to GeoJson MultiPolygon that is jsonable to GeoJSON. * @@ -109,6 +138,6 @@ public function jsonSerialize() $polygons[] = $polygon->jsonSerialize(); } - return new \GeoJson\Geometry\MultiPolygon($polygons); + return new GeoJsonMultiPolygon($polygons); } } diff --git a/src/Types/Point.php b/src/Types/Point.php index 6e8eb44f..07148355 100644 --- a/src/Types/Point.php +++ b/src/Types/Point.php @@ -2,6 +2,10 @@ namespace Grimzy\LaravelMysqlSpatial\Types; +use GeoJson\GeoJson; +use GeoJson\Geometry\Point as GeoJsonPoint; +use Grimzy\LaravelMysqlSpatial\Exceptions\InvalidGeoJsonException; + class Point extends Geometry { protected $lat; @@ -61,6 +65,24 @@ public function __toString() return $this->getLng().' '.$this->getLat(); } + /** + * @param $geoJson \GeoJson\Feature\Feature|string + * @return \Grimzy\LaravelMysqlSpatial\Types\Point + */ + public static function fromJson ($geoJson) + { + if(is_string($geoJson)) { + $geoJson = GeoJson::jsonUnserialize(json_decode($geoJson)); + } + + if(!is_a($geoJson, GeoJsonPoint::class)) { + throw new InvalidGeoJsonException('Expected ' . GeoJsonPoint::class . ', got ' . get_class($geoJson)); + } + + $coordinates = $geoJson->getCoordinates(); + return new Point($coordinates[1], $coordinates[0]); + } + /** * Convert to GeoJson Point that is jsonable to GeoJSON. * @@ -68,6 +90,6 @@ public function __toString() */ public function jsonSerialize() { - return new \GeoJson\Geometry\Point([$this->getLng(), $this->getLat()]); + return new GeoJsonPoint([$this->getLng(), $this->getLat()]); } } diff --git a/src/Types/Polygon.php b/src/Types/Polygon.php index 6710da3c..c9850c11 100644 --- a/src/Types/Polygon.php +++ b/src/Types/Polygon.php @@ -2,6 +2,10 @@ namespace Grimzy\LaravelMysqlSpatial\Types; +use GeoJson\GeoJson; +use GeoJson\Geometry\Polygon as GeoJsonPolygon; +use Grimzy\LaravelMysqlSpatial\Exceptions\InvalidGeoJsonException; + class Polygon extends MultiLineString { public function toWKT() @@ -9,6 +13,28 @@ public function toWKT() return sprintf('POLYGON(%s)', (string) $this); } + public static function fromJson ($geoJson) + { + if(is_string($geoJson)) { + $geoJson = GeoJson::jsonUnserialize(json_decode($geoJson)); + } + + if(!is_a($geoJson, GeoJsonPolygon::class)) { + throw new InvalidGeoJsonException('Expected ' . GeoJsonPolygon::class . ', got ' . get_class($geoJson)); + } + + $set = []; + foreach($geoJson->getCoordinates() as $coordinates) { + $points = []; + foreach($coordinates as $coordinate) { + $points[] = new Point($coordinate[1], $coordinate[0]); + } + $set[] = new LineString($points); + } + + return new Polygon($set); + } + /** * Convert to GeoJson Polygon that is jsonable to GeoJSON. * @@ -21,6 +47,6 @@ public function jsonSerialize() $linearRings[] = new \GeoJson\Geometry\LinearRing($lineString->jsonSerialize()->getCoordinates()); } - return new \GeoJson\Geometry\Polygon($linearRings); + return new GeoJsonPolygon($linearRings); } } diff --git a/tests/Unit/Eloquent/SpatialTraitTest.php b/tests/Unit/Eloquent/SpatialTraitTest.php index b67a9695..16fea735 100644 --- a/tests/Unit/Eloquent/SpatialTraitTest.php +++ b/tests/Unit/Eloquent/SpatialTraitTest.php @@ -1,5 +1,6 @@ assertContains("GeomFromText('GEOMETRYCOLLECTION(POINT(2 1),LINESTRING(3 2,3 3))')", $this->queries[1]); } + public function testSettingRawAttributes() { + $attributes['point'] = '0101000000000000000000f03f0000000000000040'; + + $this->model->setRawAttributes($attributes); + $this->assertInstanceOf(Point::class, ($this->model->point)); + } + + public function testSpatialFieldsNotDefinedException() { + $model = new TestNoSpatialModel(); + $this->setExpectedException(SpatialFieldsNotDefinedException::class); + $model->getSpatialFields(); + } + public function testScopeDistance() { $point = new Point(1, 2); @@ -389,6 +403,10 @@ public function testmodels() } } +class TestNoSpatialModel extends Model { + use \Grimzy\LaravelMysqlSpatial\Eloquent\SpatialTrait; +} + class TestPDO extends PDO { public $queries = []; diff --git a/tests/Unit/Schema/Grammars/MySqlGrammarTest.php b/tests/Unit/Schema/Grammars/MySqlGrammarTest.php index 2040e4ab..4773a2ea 100644 --- a/tests/Unit/Schema/Grammars/MySqlGrammarTest.php +++ b/tests/Unit/Schema/Grammars/MySqlGrammarTest.php @@ -86,6 +86,26 @@ public function testAddingGeometryCollection() $this->assertContains('GEOMETRYCOLLECTION', $statements[0]); } + public function testAddRemoveSpatialIndex() + { + $blueprint = new Blueprint('test'); + $blueprint->point('foo'); + $blueprint->spatialIndex('foo'); + $addStatements = $blueprint->toSql($this->getConnection(), $this->getGrammar()); + + $this->assertEquals(2, count($addStatements)); + $this->assertContains('alter table `test` add spatial `test_foo_spatial`(`foo`)', $addStatements[1]); + + $blueprint->dropSpatialIndex(['foo']); + $blueprint->dropSpatialIndex('test_foo_spatial'); + $dropStatements = $blueprint->toSql($this->getConnection(), $this->getGrammar()); + + $expectedSql = 'alter table `test` drop index `test_foo_spatial`'; + $this->assertEquals(5, count($dropStatements)); + $this->assertContains($expectedSql, $dropStatements[3]); + $this->assertContains($expectedSql, $dropStatements[4]); + } + /** * @return Connection */ diff --git a/tests/Unit/Types/GeometryCollectionTest.php b/tests/Unit/Types/GeometryCollectionTest.php index ac88a829..b8089c4f 100644 --- a/tests/Unit/Types/GeometryCollectionTest.php +++ b/tests/Unit/Types/GeometryCollectionTest.php @@ -89,6 +89,20 @@ public function testArrayAccess() $geometryCollection[] = 1; } + public function testFromJson() { + $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); + $geometryCollectionPoints = $geometryCollection->getGeometries(); + $this->assertEquals(2, count($geometryCollectionPoints)); + $this->assertEquals(new Point(2, 1), $geometryCollectionPoints[0]); + $this->assertEquals(new Point(4, 3), $geometryCollectionPoints[1]); + } + + public function testInvalidGeoJsonException() { + $this->setExpectedException(\Grimzy\LaravelMysqlSpatial\Exceptions\InvalidGeoJsonException::class); + GeometryCollection::fromJson('{"type":"Point","coordinates":[3.4,1.2]}'); + } + private function getGeometryCollection() { return new GeometryCollection([$this->getLineString(), $this->getPoint()]); diff --git a/tests/Unit/Types/GeometryTest.php b/tests/Unit/Types/GeometryTest.php index a763b948..3068864d 100644 --- a/tests/Unit/Types/GeometryTest.php +++ b/tests/Unit/Types/GeometryTest.php @@ -53,6 +53,113 @@ public function testGetWKBClass() $this->assertInstanceOf(Point::class, Geometry::fromWKB($prefix.'0101000000000000000000f03f0000000000000040')); } + public function testFromJsonPoint() + { + $point = Geometry::fromJson('{"type":"Point","coordinates":[3.4,1.2]}'); + $this->assertInstanceOf(Point::class, $point); + $this->assertEquals(1.2, $point->getLat()); + $this->assertEquals(3.4, $point->getLng()); + } + + public function testFromJsonLineString() + { + $lineString = Geometry::fromJson('{"type": "LineString","coordinates":[[1,1],[2,2]]}'); + $this->assertInstanceOf(LineString::class, $lineString); + $lineStringPoints = $lineString->getGeometries(); + $this->assertEquals(new Point(1, 1), $lineStringPoints[0]); + $this->assertEquals(new Point(2, 2), $lineStringPoints[1]); + } + + public function testFromJsonPolygon() + { + $polygon1 = Geometry::fromJson('{"type": "Polygon","coordinates":[[[1,1],[2,1],[2,2],[1,2],[1,1]]]}'); + $this->assertInstanceOf(Polygon::class, $polygon1); + $polygonLineStrings1 = $polygon1->getGeometries(); + $this->assertEquals(1, count($polygonLineStrings1)); + $this->assertEquals(new Point(1, 1), $polygonLineStrings1[0][0]); + $this->assertEquals(new Point(1, 2), $polygonLineStrings1[0][1]); + $this->assertEquals(new Point(2, 2), $polygonLineStrings1[0][2]); + $this->assertEquals(new Point(2, 1), $polygonLineStrings1[0][3]); + $this->assertEquals(new Point(1, 1), $polygonLineStrings1[0][4]); + + $polygon2 = Geometry::fromJson('{"type":"Polygon","coordinates":[[[1,1],[2,1],[2,2],[1,2],[1,1]],[[1.2,1.2],[1.6,1.2],[1.6,1.8],[1.2,1.8],[1.2,1.2]]]}'); + $this->assertInstanceOf(Polygon::class, $polygon2); + $polygonLineStrings2 = $polygon2->getGeometries(); + $this->assertEquals(2, count($polygonLineStrings2)); + $this->assertEquals(new Point(1, 1), $polygonLineStrings2[0][0]); + $this->assertEquals(new Point(1, 2), $polygonLineStrings2[0][1]); + $this->assertEquals(new Point(2, 2), $polygonLineStrings2[0][2]); + $this->assertEquals(new Point(2, 1), $polygonLineStrings2[0][3]); + $this->assertEquals(new Point(1, 1), $polygonLineStrings2[0][4]); + $this->assertEquals(new Point(1.2, 1.2), $polygonLineStrings2[1][0]); + $this->assertEquals(new Point(1.2, 1.6), $polygonLineStrings2[1][1]); + $this->assertEquals(new Point(1.8, 1.6), $polygonLineStrings2[1][2]); + $this->assertEquals(new Point(1.8, 1.2), $polygonLineStrings2[1][3]); + $this->assertEquals(new Point(1.2, 1.2), $polygonLineStrings2[1][4]); + } + + public function testFromJsonMultiPoint() + { + $multiPoint = Geometry::fromJson('{"type":"MultiPoint","coordinates":[[1,1],[2,1],[2,2]]}'); + $this->assertInstanceOf(MultiPoint::class, $multiPoint); + $multiPointPoints = $multiPoint->getGeometries(); + $this->assertEquals(3, count($multiPointPoints)); + $this->assertEquals(new Point(1, 1), $multiPointPoints[0]); + $this->assertEquals(new Point(1, 2), $multiPointPoints[1]); + $this->assertEquals(new Point(2, 2), $multiPointPoints[2]); + } + + public function testFromJsonMultiLineString() + { + $multiLineString = Geometry::fromJson('{"type":"MultiLineString","coordinates":[[[1,1],[1,2],[1,3]],[[2,1],[2,2],[2,3]]]}'); + $this->assertInstanceOf(MultiLineString::class, $multiLineString); + $multiLineStringLineStrings = $multiLineString->getGeometries(); + $this->assertEquals(2, count($multiLineStringLineStrings)); + $this->assertEquals(new Point(1, 1), $multiLineStringLineStrings[0][0]); + $this->assertEquals(new Point(2, 1), $multiLineStringLineStrings[0][1]); + $this->assertEquals(new Point(3, 1), $multiLineStringLineStrings[0][2]); + $this->assertEquals(new Point(1, 2), $multiLineStringLineStrings[1][0]); + $this->assertEquals(new Point(2, 2), $multiLineStringLineStrings[1][1]); + $this->assertEquals(new Point(3, 2), $multiLineStringLineStrings[1][2]); + } + + public function testFromJsonMultiPolygon() { + $multiPolygon = Geometry::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->assertInstanceOf(MultiPolygon::class, $multiPolygon); + $multiPolygonPolygons = $multiPolygon->getGeometries(); + $this->assertEquals(2, count($multiPolygonPolygons)); + $this->assertEquals(new Polygon([new LineString([ + new Point(1, 1), + new Point(2, 1), + new Point(2, 2), + new Point(1, 2), + new Point(1, 1) + ])]), $multiPolygonPolygons[0]); + $this->assertEquals(new Polygon([new LineString([ + new Point(0, 0), + new Point(1, 0), + new Point(1, 1), + new Point(0, 1), + new Point(0, 0) + ])]), $multiPolygonPolygons[1]); + } + + public function testFromJsonPointFeature() { + $point = Geometry::fromJson('{"type":"Feature","properties":{},"geometry":{"type":"Point","coordinates":[3.4,1.2]}}'); + $this->assertInstanceOf(Point::class, $point); + $this->assertEquals(1.2, $point->getLat()); + $this->assertEquals(3.4, $point->getLng()); + } + + public function testFromJsonMultiPointFeatureCollection() { + $geometryCollection = Geometry::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); + $geometryCollectionPoints = $geometryCollection->getGeometries(); + $this->assertEquals(2, count($geometryCollectionPoints)); + $this->assertEquals(new Point(2, 1), $geometryCollectionPoints[0]); + $this->assertEquals(new Point(4, 3), $geometryCollectionPoints[1]); + } + public function testToJson() { $point = new Point(1, 1); diff --git a/tests/Unit/Types/LineStringTest.php b/tests/Unit/Types/LineStringTest.php index 6d73d422..063b967c 100644 --- a/tests/Unit/Types/LineStringTest.php +++ b/tests/Unit/Types/LineStringTest.php @@ -34,6 +34,19 @@ public function testToString() $this->assertEquals('0 0,1 1,2 2', (string) $linestring); } + public function testFromJson() { + $lineString = LineString::fromJson('{"type": "LineString","coordinates":[[1,1],[2,2]]}'); + $this->assertInstanceOf(LineString::class, $lineString); + $lineStringPoints = $lineString->getGeometries(); + $this->assertEquals(new Point(1, 1), $lineStringPoints[0]); + $this->assertEquals(new Point(2, 2), $lineStringPoints[1]); + } + + public function testInvalidGeoJsonException() { + $this->setExpectedException(\Grimzy\LaravelMysqlSpatial\Exceptions\InvalidGeoJsonException::class); + LineString::fromJson('{"type":"Point","coordinates":[3.4,1.2]}'); + } + public function testJsonSerialize() { $lineString = new LineString($this->points); diff --git a/tests/Unit/Types/MultiLineStringTest.php b/tests/Unit/Types/MultiLineStringTest.php index 5f0760ec..f67f137d 100644 --- a/tests/Unit/Types/MultiLineStringTest.php +++ b/tests/Unit/Types/MultiLineStringTest.php @@ -29,6 +29,24 @@ public function testToWKT() $this->assertSame('MULTILINESTRING((0 0,1 0,1 1,0 1,0 0))', $multilinestring->toWKT()); } + public function testFromJson() { + $multiLineString = MultiLineString::fromJson('{"type":"MultiLineString","coordinates":[[[1,1],[1,2],[1,3]],[[2,1],[2,2],[2,3]]]}'); + $this->assertInstanceOf(MultiLineString::class, $multiLineString); + $multiLineStringLineStrings = $multiLineString->getGeometries(); + $this->assertEquals(2, count($multiLineStringLineStrings)); + $this->assertEquals(new Point(1, 1), $multiLineStringLineStrings[0][0]); + $this->assertEquals(new Point(2, 1), $multiLineStringLineStrings[0][1]); + $this->assertEquals(new Point(3, 1), $multiLineStringLineStrings[0][2]); + $this->assertEquals(new Point(1, 2), $multiLineStringLineStrings[1][0]); + $this->assertEquals(new Point(2, 2), $multiLineStringLineStrings[1][1]); + $this->assertEquals(new Point(3, 2), $multiLineStringLineStrings[1][2]); + } + + public function testInvalidGeoJsonException() { + $this->setExpectedException(\Grimzy\LaravelMysqlSpatial\Exceptions\InvalidGeoJsonException::class); + MultiLineString::fromJson('{"type":"Point","coordinates":[3.4,1.2]}'); + } + public function testJsonSerialize() { $multilinestring = MultiLineString::fromWKT('MULTILINESTRING((0 0,1 1,1 2),(2 3,3 2,5 4))'); diff --git a/tests/Unit/Types/MultiPointTest.php b/tests/Unit/Types/MultiPointTest.php index ee0bb94d..926ad06b 100644 --- a/tests/Unit/Types/MultiPointTest.php +++ b/tests/Unit/Types/MultiPointTest.php @@ -29,6 +29,21 @@ public function testGetPoints() $this->assertInstanceOf(Point::class, $multipoint->getPoints()[0]); } + public function testFromJson() { + $multiPoint = MultiPoint::fromJson('{"type":"MultiPoint","coordinates":[[1,1],[2,1],[2,2]]}'); + $this->assertInstanceOf(MultiPoint::class, $multiPoint); + $multiPointPoints = $multiPoint->getGeometries(); + $this->assertEquals(3, count($multiPointPoints)); + $this->assertEquals(new Point(1, 1), $multiPointPoints[0]); + $this->assertEquals(new Point(1, 2), $multiPointPoints[1]); + $this->assertEquals(new Point(2, 2), $multiPointPoints[2]); + } + + public function testInvalidGeoJsonException() { + $this->setExpectedException(\Grimzy\LaravelMysqlSpatial\Exceptions\InvalidGeoJsonException::class); + MultiPoint::fromJson('{"type":"Point","coordinates":[3.4,1.2]}'); + } + public function testJsonSerialize() { $collection = [new Point(0, 0), new Point(0, 1), new Point(1, 1)]; diff --git a/tests/Unit/Types/MultiPolygonTest.php b/tests/Unit/Types/MultiPolygonTest.php index f8c87375..02a12f3d 100644 --- a/tests/Unit/Types/MultiPolygonTest.php +++ b/tests/Unit/Types/MultiPolygonTest.php @@ -34,6 +34,32 @@ public function testIssue12() $this->assertInstanceOf(MultiPolygon::class, $polygon); } + public function testFromJson() { + $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->assertInstanceOf(MultiPolygon::class, $multiPolygon); + $multiPolygonPolygons = $multiPolygon->getGeometries(); + $this->assertEquals(2, count($multiPolygonPolygons)); + $this->assertEquals(new Polygon([new LineString([ + new Point(1, 1), + new Point(2, 1), + new Point(2, 2), + new Point(1, 2), + new Point(1, 1) + ])]), $multiPolygonPolygons[0]); + $this->assertEquals(new Polygon([new LineString([ + new Point(0, 0), + new Point(1, 0), + new Point(1, 1), + new Point(0, 1), + new Point(0, 0) + ])]), $multiPolygonPolygons[1]); + } + + public function testInvalidGeoJsonException() { + $this->setExpectedException(\Grimzy\LaravelMysqlSpatial\Exceptions\InvalidGeoJsonException::class); + MultiPolygon::fromJson('{"type":"Point","coordinates":[3.4,1.2]}'); + } + public function testJsonSerialize() { $this->assertInstanceOf(\GeoJson\Geometry\MultiPolygon::class, $this->getMultiPolygon()->jsonSerialize()); diff --git a/tests/Unit/Types/PointTest.php b/tests/Unit/Types/PointTest.php index 576dc039..0c0c9502 100644 --- a/tests/Unit/Types/PointTest.php +++ b/tests/Unit/Types/PointTest.php @@ -53,6 +53,18 @@ public function testToString() $this->assertEquals('1.3 2', (string) $point); } + public function testFromJson() { + $point = Point::fromJson('{"type":"Point","coordinates":[3.4,1.2]}'); + $this->assertInstanceOf(Point::class, $point); + $this->assertEquals(1.2, $point->getLat()); + $this->assertEquals(3.4, $point->getLng()); + } + + public function testInvalidGeoJsonException() { + $this->setExpectedException(\Grimzy\LaravelMysqlSpatial\Exceptions\InvalidGeoJsonException::class); + Point::fromJson('{"type": "LineString","coordinates":[[1,1],[2,2]]}'); + } + public function testJsonSerialize() { $point = new Point(1.2, 3.4); diff --git a/tests/Unit/Types/PolygonTest.php b/tests/Unit/Types/PolygonTest.php index 16c6f248..76320144 100644 --- a/tests/Unit/Types/PolygonTest.php +++ b/tests/Unit/Types/PolygonTest.php @@ -36,6 +36,28 @@ public function testToWKT() $this->assertEquals('POLYGON((0 0,1 0,1 1,0 1,0 0))', $this->polygon->toWKT()); } + public function testFromJson() { + $polygon = Polygon::fromJson('{"type":"Polygon","coordinates":[[[1,1],[2,1],[2,2],[1,2],[1,1]],[[1.2,1.2],[1.6,1.2],[1.6,1.8],[1.2,1.8],[1.2,1.2]]]}'); + $this->assertInstanceOf(Polygon::class, $polygon); + $polygonLineStrings = $polygon->getGeometries(); + $this->assertEquals(2, count($polygonLineStrings)); + $this->assertEquals(new Point(1, 1), $polygonLineStrings[0][0]); + $this->assertEquals(new Point(1, 2), $polygonLineStrings[0][1]); + $this->assertEquals(new Point(2, 2), $polygonLineStrings[0][2]); + $this->assertEquals(new Point(2, 1), $polygonLineStrings[0][3]); + $this->assertEquals(new Point(1, 1), $polygonLineStrings[0][4]); + $this->assertEquals(new Point(1.2, 1.2), $polygonLineStrings[1][0]); + $this->assertEquals(new Point(1.2, 1.6), $polygonLineStrings[1][1]); + $this->assertEquals(new Point(1.8, 1.6), $polygonLineStrings[1][2]); + $this->assertEquals(new Point(1.8, 1.2), $polygonLineStrings[1][3]); + $this->assertEquals(new Point(1.2, 1.2), $polygonLineStrings[1][4]); + } + + public function testInvalidGeoJsonException() { + $this->setExpectedException(\Grimzy\LaravelMysqlSpatial\Exceptions\InvalidGeoJsonException::class); + Polygon::fromJson('{"type":"Point","coordinates":[3.4,1.2]}'); + } + public function testJsonSerialize() { $this->assertInstanceOf(\GeoJson\Geometry\Polygon::class, $this->polygon->jsonSerialize()); From 84ed34abd848a60f6a6be7427308a82d0f862a6c Mon Sep 17 00:00:00 2001 From: Joseph Estefane Date: Sun, 1 Oct 2017 10:10:58 +0200 Subject: [PATCH 2/4] Style CI fix :performing_arts: (cherry picked from commit 8add88a0ea711b62249e52d71d32dc9fa17125e4) --- src/Exceptions/InvalidGeoJsonException.php | 2 +- src/Types/Geometry.php | 10 ++++++---- src/Types/GeometryCollection.php | 10 +++++----- src/Types/LineString.php | 12 ++++++------ src/Types/MultiLineString.php | 14 +++++++------- src/Types/MultiPoint.php | 12 ++++++------ src/Types/MultiPolygon.php | 16 ++++++++-------- src/Types/Point.php | 12 +++++++----- src/Types/Polygon.php | 14 +++++++------- tests/Unit/Eloquent/SpatialTraitTest.php | 9 ++++++--- tests/Unit/Types/GeometryCollectionTest.php | 6 ++++-- tests/Unit/Types/GeometryTest.php | 15 +++++++++------ tests/Unit/Types/LineStringTest.php | 6 ++++-- tests/Unit/Types/MultiLineStringTest.php | 6 ++++-- tests/Unit/Types/MultiPointTest.php | 6 ++++-- tests/Unit/Types/MultiPolygonTest.php | 10 ++++++---- tests/Unit/Types/PointTest.php | 6 ++++-- tests/Unit/Types/PolygonTest.php | 6 ++++-- 18 files changed, 98 insertions(+), 74 deletions(-) diff --git a/src/Exceptions/InvalidGeoJsonException.php b/src/Exceptions/InvalidGeoJsonException.php index f913cf04..3374fdb7 100644 --- a/src/Exceptions/InvalidGeoJsonException.php +++ b/src/Exceptions/InvalidGeoJsonException.php @@ -4,4 +4,4 @@ class InvalidGeoJsonException extends \RuntimeException { -} \ No newline at end of file +} diff --git a/src/Types/Geometry.php b/src/Types/Geometry.php index 0b0fded8..a46baed3 100644 --- a/src/Types/Geometry.php +++ b/src/Types/Geometry.php @@ -72,20 +72,22 @@ public static function fromWKT($wkt) return static::fromString($wktArgument); } - public static function fromJson($geoJson) { - if(is_string($geoJson)) { + public static function fromJson($geoJson) + { + if (is_string($geoJson)) { $geoJson = GeoJson::jsonUnserialize(json_decode($geoJson)); } - if($geoJson->getType() === 'FeatureCollection') { + if ($geoJson->getType() === 'FeatureCollection') { return GeometryCollection::fromJson($geoJson); } - if($geoJson->getType() === 'Feature') { + if ($geoJson->getType() === 'Feature') { $geoJson = $geoJson->getGeometry(); } $type = '\Grimzy\LaravelMysqlSpatial\Types\\'.$geoJson->getType(); + return $type::fromJson($geoJson); } diff --git a/src/Types/GeometryCollection.php b/src/Types/GeometryCollection.php index 26a4b439..2e1f0321 100755 --- a/src/Types/GeometryCollection.php +++ b/src/Types/GeometryCollection.php @@ -110,14 +110,14 @@ public function count() return count($this->items); } - public static function fromJson ($geoJson) + public static function fromJson($geoJson) { - if(is_string($geoJson)) { + if (is_string($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)) { + throw new InvalidGeoJsonException('Expected '.FeatureCollection::class.', got '.get_class($geoJson)); } $set = []; @@ -125,7 +125,7 @@ public static function fromJson ($geoJson) $set[] = parent::fromJson($feature); } - return new GeometryCollection($set); + return new self($set); } /** diff --git a/src/Types/LineString.php b/src/Types/LineString.php index 59cd3662..02097edd 100644 --- a/src/Types/LineString.php +++ b/src/Types/LineString.php @@ -35,22 +35,22 @@ public function __toString() return $this->toPairList(); } - public static function fromJson ($geoJson) + public static function fromJson($geoJson) { - if(is_string($geoJson)) { + if (is_string($geoJson)) { $geoJson = GeoJson::jsonUnserialize(json_decode($geoJson)); } - if(!is_a($geoJson, GeoJsonLineString::class)) { - throw new InvalidGeoJsonException('Expected ' . GeoJsonLineString::class . ', got ' . get_class($geoJson)); + if (!is_a($geoJson, GeoJsonLineString::class)) { + throw new InvalidGeoJsonException('Expected '.GeoJsonLineString::class.', got '.get_class($geoJson)); } $set = []; - foreach($geoJson->getCoordinates() as $coordinate) { + foreach ($geoJson->getCoordinates() as $coordinate) { $set[] = new Point($coordinate[1], $coordinate[0]); } - return new LineString($set); + return new self($set); } /** diff --git a/src/Types/MultiLineString.php b/src/Types/MultiLineString.php index 025fb4e3..dd3342fd 100644 --- a/src/Types/MultiLineString.php +++ b/src/Types/MultiLineString.php @@ -65,26 +65,26 @@ public function offsetSet($offset, $value) parent::offsetSet($offset, $value); } - public static function fromJson ($geoJson) + public static function fromJson($geoJson) { - if(is_string($geoJson)) { + if (is_string($geoJson)) { $geoJson = GeoJson::jsonUnserialize(json_decode($geoJson)); } - if(!is_a($geoJson, GeoJsonMultiLineString::class)) { - throw new InvalidGeoJsonException('Expected ' . GeoJsonMultiLineString::class . ', got ' . get_class($geoJson)); + if (!is_a($geoJson, GeoJsonMultiLineString::class)) { + throw new InvalidGeoJsonException('Expected '.GeoJsonMultiLineString::class.', got '.get_class($geoJson)); } $set = []; - foreach($geoJson->getCoordinates() as $coordinates) { + foreach ($geoJson->getCoordinates() as $coordinates) { $points = []; - foreach($coordinates as $coordinate) { + foreach ($coordinates as $coordinate) { $points[] = new Point($coordinate[1], $coordinate[0]); } $set[] = new LineString($points); } - return new MultiLineString($set); + return new self($set); } /** diff --git a/src/Types/MultiPoint.php b/src/Types/MultiPoint.php index 60fad9a5..fb55f9e8 100644 --- a/src/Types/MultiPoint.php +++ b/src/Types/MultiPoint.php @@ -39,22 +39,22 @@ public function __toString() }, $this->items)); } - public static function fromJson ($geoJson) + public static function fromJson($geoJson) { - if(is_string($geoJson)) { + if (is_string($geoJson)) { $geoJson = GeoJson::jsonUnserialize(json_decode($geoJson)); } - if(!is_a($geoJson, GeoJsonMultiPoint::class)) { - throw new InvalidGeoJsonException('Expected ' . GeoJsonMultiPoint::class . ', got ' . get_class($geoJson)); + if (!is_a($geoJson, GeoJsonMultiPoint::class)) { + throw new InvalidGeoJsonException('Expected '.GeoJsonMultiPoint::class.', got '.get_class($geoJson)); } $set = []; - foreach($geoJson->getCoordinates() as $coordinate) { + foreach ($geoJson->getCoordinates() as $coordinate) { $set[] = new Point($coordinate[1], $coordinate[0]); } - return new MultiPoint($set); + return new self($set); } /** diff --git a/src/Types/MultiPolygon.php b/src/Types/MultiPolygon.php index 927df911..aba2b6d1 100644 --- a/src/Types/MultiPolygon.php +++ b/src/Types/MultiPolygon.php @@ -100,22 +100,22 @@ public function offsetSet($offset, $value) parent::offsetSet($offset, $value); } - public static function fromJson ($geoJson) + public static function fromJson($geoJson) { - if(is_string($geoJson)) { + if (is_string($geoJson)) { $geoJson = GeoJson::jsonUnserialize(json_decode($geoJson)); } - if(!is_a($geoJson, GeoJsonMultiPolygon::class)) { - throw new InvalidGeoJsonException('Expected ' . GeoJsonMultiPolygon::class . ', got ' . get_class($geoJson)); + if (!is_a($geoJson, GeoJsonMultiPolygon::class)) { + throw new InvalidGeoJsonException('Expected '.GeoJsonMultiPolygon::class.', got '.get_class($geoJson)); } $set = []; - foreach($geoJson->getCoordinates() as $polygonCoordinates) { + foreach ($geoJson->getCoordinates() as $polygonCoordinates) { $lineStrings = []; - foreach($polygonCoordinates as $lineStringCoordinates) { + foreach ($polygonCoordinates as $lineStringCoordinates) { $points = []; - foreach($lineStringCoordinates as $lineStringCoordinate) { + foreach ($lineStringCoordinates as $lineStringCoordinate) { $points[] = new Point($lineStringCoordinate[1], $lineStringCoordinate[0]); } $lineStrings[] = new LineString($points); @@ -123,7 +123,7 @@ public static function fromJson ($geoJson) $set[] = new Polygon($lineStrings); } - return new MultiPolygon($set); + return new self($set); } /** diff --git a/src/Types/Point.php b/src/Types/Point.php index 07148355..2a149e80 100644 --- a/src/Types/Point.php +++ b/src/Types/Point.php @@ -67,20 +67,22 @@ public function __toString() /** * @param $geoJson \GeoJson\Feature\Feature|string + * * @return \Grimzy\LaravelMysqlSpatial\Types\Point */ - public static function fromJson ($geoJson) + public static function fromJson($geoJson) { - if(is_string($geoJson)) { + if (is_string($geoJson)) { $geoJson = GeoJson::jsonUnserialize(json_decode($geoJson)); } - if(!is_a($geoJson, GeoJsonPoint::class)) { - throw new InvalidGeoJsonException('Expected ' . GeoJsonPoint::class . ', got ' . get_class($geoJson)); + if (!is_a($geoJson, GeoJsonPoint::class)) { + throw new InvalidGeoJsonException('Expected '.GeoJsonPoint::class.', got '.get_class($geoJson)); } $coordinates = $geoJson->getCoordinates(); - return new Point($coordinates[1], $coordinates[0]); + + return new self($coordinates[1], $coordinates[0]); } /** diff --git a/src/Types/Polygon.php b/src/Types/Polygon.php index c9850c11..9c10cecc 100644 --- a/src/Types/Polygon.php +++ b/src/Types/Polygon.php @@ -13,26 +13,26 @@ public function toWKT() return sprintf('POLYGON(%s)', (string) $this); } - public static function fromJson ($geoJson) + public static function fromJson($geoJson) { - if(is_string($geoJson)) { + if (is_string($geoJson)) { $geoJson = GeoJson::jsonUnserialize(json_decode($geoJson)); } - if(!is_a($geoJson, GeoJsonPolygon::class)) { - throw new InvalidGeoJsonException('Expected ' . GeoJsonPolygon::class . ', got ' . get_class($geoJson)); + if (!is_a($geoJson, GeoJsonPolygon::class)) { + throw new InvalidGeoJsonException('Expected '.GeoJsonPolygon::class.', got '.get_class($geoJson)); } $set = []; - foreach($geoJson->getCoordinates() as $coordinates) { + foreach ($geoJson->getCoordinates() as $coordinates) { $points = []; - foreach($coordinates as $coordinate) { + foreach ($coordinates as $coordinate) { $points[] = new Point($coordinate[1], $coordinate[0]); } $set[] = new LineString($points); } - return new Polygon($set); + return new self($set); } /** diff --git a/tests/Unit/Eloquent/SpatialTraitTest.php b/tests/Unit/Eloquent/SpatialTraitTest.php index 16fea735..ef762408 100644 --- a/tests/Unit/Eloquent/SpatialTraitTest.php +++ b/tests/Unit/Eloquent/SpatialTraitTest.php @@ -192,14 +192,16 @@ public function testInsertUpdateGeometryCollectionHasCorrectSql() $this->assertContains("GeomFromText('GEOMETRYCOLLECTION(POINT(2 1),LINESTRING(3 2,3 3))')", $this->queries[1]); } - public function testSettingRawAttributes() { + public function testSettingRawAttributes() + { $attributes['point'] = '0101000000000000000000f03f0000000000000040'; $this->model->setRawAttributes($attributes); $this->assertInstanceOf(Point::class, ($this->model->point)); } - public function testSpatialFieldsNotDefinedException() { + public function testSpatialFieldsNotDefinedException() + { $model = new TestNoSpatialModel(); $this->setExpectedException(SpatialFieldsNotDefinedException::class); $model->getSpatialFields(); @@ -403,7 +405,8 @@ public function testmodels() } } -class TestNoSpatialModel extends Model { +class TestNoSpatialModel extends Model +{ use \Grimzy\LaravelMysqlSpatial\Eloquent\SpatialTrait; } diff --git a/tests/Unit/Types/GeometryCollectionTest.php b/tests/Unit/Types/GeometryCollectionTest.php index b8089c4f..07759c5f 100644 --- a/tests/Unit/Types/GeometryCollectionTest.php +++ b/tests/Unit/Types/GeometryCollectionTest.php @@ -89,7 +89,8 @@ public function testArrayAccess() $geometryCollection[] = 1; } - public function testFromJson() { + public function testFromJson() + { $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); $geometryCollectionPoints = $geometryCollection->getGeometries(); @@ -98,7 +99,8 @@ public function testFromJson() { $this->assertEquals(new Point(4, 3), $geometryCollectionPoints[1]); } - public function testInvalidGeoJsonException() { + public function testInvalidGeoJsonException() + { $this->setExpectedException(\Grimzy\LaravelMysqlSpatial\Exceptions\InvalidGeoJsonException::class); GeometryCollection::fromJson('{"type":"Point","coordinates":[3.4,1.2]}'); } diff --git a/tests/Unit/Types/GeometryTest.php b/tests/Unit/Types/GeometryTest.php index 3068864d..51b400ec 100644 --- a/tests/Unit/Types/GeometryTest.php +++ b/tests/Unit/Types/GeometryTest.php @@ -123,7 +123,8 @@ public function testFromJsonMultiLineString() $this->assertEquals(new Point(3, 2), $multiLineStringLineStrings[1][2]); } - public function testFromJsonMultiPolygon() { + public function testFromJsonMultiPolygon() + { $multiPolygon = Geometry::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->assertInstanceOf(MultiPolygon::class, $multiPolygon); $multiPolygonPolygons = $multiPolygon->getGeometries(); @@ -133,25 +134,27 @@ public function testFromJsonMultiPolygon() { new Point(2, 1), new Point(2, 2), new Point(1, 2), - new Point(1, 1) + new Point(1, 1), ])]), $multiPolygonPolygons[0]); $this->assertEquals(new Polygon([new LineString([ new Point(0, 0), new Point(1, 0), new Point(1, 1), new Point(0, 1), - new Point(0, 0) + new Point(0, 0), ])]), $multiPolygonPolygons[1]); } - public function testFromJsonPointFeature() { + public function testFromJsonPointFeature() + { $point = Geometry::fromJson('{"type":"Feature","properties":{},"geometry":{"type":"Point","coordinates":[3.4,1.2]}}'); $this->assertInstanceOf(Point::class, $point); $this->assertEquals(1.2, $point->getLat()); $this->assertEquals(3.4, $point->getLng()); } - - public function testFromJsonMultiPointFeatureCollection() { + + public function testFromJsonMultiPointFeatureCollection() + { $geometryCollection = Geometry::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); $geometryCollectionPoints = $geometryCollection->getGeometries(); diff --git a/tests/Unit/Types/LineStringTest.php b/tests/Unit/Types/LineStringTest.php index 063b967c..9300e3f1 100644 --- a/tests/Unit/Types/LineStringTest.php +++ b/tests/Unit/Types/LineStringTest.php @@ -34,7 +34,8 @@ public function testToString() $this->assertEquals('0 0,1 1,2 2', (string) $linestring); } - public function testFromJson() { + public function testFromJson() + { $lineString = LineString::fromJson('{"type": "LineString","coordinates":[[1,1],[2,2]]}'); $this->assertInstanceOf(LineString::class, $lineString); $lineStringPoints = $lineString->getGeometries(); @@ -42,7 +43,8 @@ public function testFromJson() { $this->assertEquals(new Point(2, 2), $lineStringPoints[1]); } - public function testInvalidGeoJsonException() { + public function testInvalidGeoJsonException() + { $this->setExpectedException(\Grimzy\LaravelMysqlSpatial\Exceptions\InvalidGeoJsonException::class); LineString::fromJson('{"type":"Point","coordinates":[3.4,1.2]}'); } diff --git a/tests/Unit/Types/MultiLineStringTest.php b/tests/Unit/Types/MultiLineStringTest.php index f67f137d..df7b42e4 100644 --- a/tests/Unit/Types/MultiLineStringTest.php +++ b/tests/Unit/Types/MultiLineStringTest.php @@ -29,7 +29,8 @@ public function testToWKT() $this->assertSame('MULTILINESTRING((0 0,1 0,1 1,0 1,0 0))', $multilinestring->toWKT()); } - public function testFromJson() { + public function testFromJson() + { $multiLineString = MultiLineString::fromJson('{"type":"MultiLineString","coordinates":[[[1,1],[1,2],[1,3]],[[2,1],[2,2],[2,3]]]}'); $this->assertInstanceOf(MultiLineString::class, $multiLineString); $multiLineStringLineStrings = $multiLineString->getGeometries(); @@ -42,7 +43,8 @@ public function testFromJson() { $this->assertEquals(new Point(3, 2), $multiLineStringLineStrings[1][2]); } - public function testInvalidGeoJsonException() { + public function testInvalidGeoJsonException() + { $this->setExpectedException(\Grimzy\LaravelMysqlSpatial\Exceptions\InvalidGeoJsonException::class); MultiLineString::fromJson('{"type":"Point","coordinates":[3.4,1.2]}'); } diff --git a/tests/Unit/Types/MultiPointTest.php b/tests/Unit/Types/MultiPointTest.php index 926ad06b..1e9bf7f4 100644 --- a/tests/Unit/Types/MultiPointTest.php +++ b/tests/Unit/Types/MultiPointTest.php @@ -29,7 +29,8 @@ public function testGetPoints() $this->assertInstanceOf(Point::class, $multipoint->getPoints()[0]); } - public function testFromJson() { + public function testFromJson() + { $multiPoint = MultiPoint::fromJson('{"type":"MultiPoint","coordinates":[[1,1],[2,1],[2,2]]}'); $this->assertInstanceOf(MultiPoint::class, $multiPoint); $multiPointPoints = $multiPoint->getGeometries(); @@ -39,7 +40,8 @@ public function testFromJson() { $this->assertEquals(new Point(2, 2), $multiPointPoints[2]); } - public function testInvalidGeoJsonException() { + public function testInvalidGeoJsonException() + { $this->setExpectedException(\Grimzy\LaravelMysqlSpatial\Exceptions\InvalidGeoJsonException::class); MultiPoint::fromJson('{"type":"Point","coordinates":[3.4,1.2]}'); } diff --git a/tests/Unit/Types/MultiPolygonTest.php b/tests/Unit/Types/MultiPolygonTest.php index 02a12f3d..adcac1e1 100644 --- a/tests/Unit/Types/MultiPolygonTest.php +++ b/tests/Unit/Types/MultiPolygonTest.php @@ -34,7 +34,8 @@ public function testIssue12() $this->assertInstanceOf(MultiPolygon::class, $polygon); } - public function testFromJson() { + public function testFromJson() + { $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->assertInstanceOf(MultiPolygon::class, $multiPolygon); $multiPolygonPolygons = $multiPolygon->getGeometries(); @@ -44,18 +45,19 @@ public function testFromJson() { new Point(2, 1), new Point(2, 2), new Point(1, 2), - new Point(1, 1) + new Point(1, 1), ])]), $multiPolygonPolygons[0]); $this->assertEquals(new Polygon([new LineString([ new Point(0, 0), new Point(1, 0), new Point(1, 1), new Point(0, 1), - new Point(0, 0) + new Point(0, 0), ])]), $multiPolygonPolygons[1]); } - public function testInvalidGeoJsonException() { + public function testInvalidGeoJsonException() + { $this->setExpectedException(\Grimzy\LaravelMysqlSpatial\Exceptions\InvalidGeoJsonException::class); MultiPolygon::fromJson('{"type":"Point","coordinates":[3.4,1.2]}'); } diff --git a/tests/Unit/Types/PointTest.php b/tests/Unit/Types/PointTest.php index 0c0c9502..257f6a45 100644 --- a/tests/Unit/Types/PointTest.php +++ b/tests/Unit/Types/PointTest.php @@ -53,14 +53,16 @@ public function testToString() $this->assertEquals('1.3 2', (string) $point); } - public function testFromJson() { + public function testFromJson() + { $point = Point::fromJson('{"type":"Point","coordinates":[3.4,1.2]}'); $this->assertInstanceOf(Point::class, $point); $this->assertEquals(1.2, $point->getLat()); $this->assertEquals(3.4, $point->getLng()); } - public function testInvalidGeoJsonException() { + public function testInvalidGeoJsonException() + { $this->setExpectedException(\Grimzy\LaravelMysqlSpatial\Exceptions\InvalidGeoJsonException::class); Point::fromJson('{"type": "LineString","coordinates":[[1,1],[2,2]]}'); } diff --git a/tests/Unit/Types/PolygonTest.php b/tests/Unit/Types/PolygonTest.php index 76320144..de923c2f 100644 --- a/tests/Unit/Types/PolygonTest.php +++ b/tests/Unit/Types/PolygonTest.php @@ -36,7 +36,8 @@ public function testToWKT() $this->assertEquals('POLYGON((0 0,1 0,1 1,0 1,0 0))', $this->polygon->toWKT()); } - public function testFromJson() { + public function testFromJson() + { $polygon = Polygon::fromJson('{"type":"Polygon","coordinates":[[[1,1],[2,1],[2,2],[1,2],[1,1]],[[1.2,1.2],[1.6,1.2],[1.6,1.8],[1.2,1.8],[1.2,1.2]]]}'); $this->assertInstanceOf(Polygon::class, $polygon); $polygonLineStrings = $polygon->getGeometries(); @@ -53,7 +54,8 @@ public function testFromJson() { $this->assertEquals(new Point(1.2, 1.2), $polygonLineStrings[1][4]); } - public function testInvalidGeoJsonException() { + public function testInvalidGeoJsonException() + { $this->setExpectedException(\Grimzy\LaravelMysqlSpatial\Exceptions\InvalidGeoJsonException::class); Polygon::fromJson('{"type":"Point","coordinates":[3.4,1.2]}'); } From 7e7ee8a668b5213cc65140b7d90a57e60a8b2ebe Mon Sep 17 00:00:00 2001 From: Chrissi2812 Date: Tue, 18 Dec 2018 17:18:56 +0100 Subject: [PATCH 3/4] fixed php 7.1 incompatibilities --- src/Schema/Blueprint.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Schema/Blueprint.php b/src/Schema/Blueprint.php index f0945f4d..9507e31e 100644 --- a/src/Schema/Blueprint.php +++ b/src/Schema/Blueprint.php @@ -21,13 +21,14 @@ public function geometry($column) /** * Add a point column on the table. * - * @param $column + * @param string $column + * @param null|int $srid * * @return \Illuminate\Support\Fluent */ - public function point($column) + public function point($column, $srid = null) { - return $this->addColumn('point', $column); + return $this->addColumn('point', $column, compact('srid')); } /** From df0ded97642dfb9623b6407ef5a35caf6e99eacb Mon Sep 17 00:00:00 2001 From: Mohammad Nourinik Date: Tue, 5 Jun 2018 14:20:17 +0200 Subject: [PATCH 4/4] Fix testPoint. (cherry picked from commit 254aa68b3440bd1236388df8e5ab90caecaad832) --- tests/Unit/Schema/BlueprintTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Unit/Schema/BlueprintTest.php b/tests/Unit/Schema/BlueprintTest.php index 41dac43d..da335520 100644 --- a/tests/Unit/Schema/BlueprintTest.php +++ b/tests/Unit/Schema/BlueprintTest.php @@ -32,7 +32,7 @@ public function testPoint() { $this->blueprint ->shouldReceive('addColumn') - ->with('point', 'col') + ->with('point', 'col', ['srid' => null]) ->once(); $this->blueprint->point('col');