From 4d3e0da415522d5961e39dccc8115bad0b823a25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Hallet?= Date: Fri, 17 Nov 2017 12:43:16 +0100 Subject: [PATCH 1/3] add failing tests --- tests/Geometries/UnderLocaleTest.php | 155 +++++++++++++++++++++++++++ 1 file changed, 155 insertions(+) create mode 100644 tests/Geometries/UnderLocaleTest.php diff --git a/tests/Geometries/UnderLocaleTest.php b/tests/Geometries/UnderLocaleTest.php new file mode 100644 index 0000000..edcf178 --- /dev/null +++ b/tests/Geometries/UnderLocaleTest.php @@ -0,0 +1,155 @@ +markTestSkipped('The locale is not available for testing float output formatting'); + } + } + + public function testPointToWKT() + { + $point = new Point(1.5, 2.5); + $this->assertEquals('POINT(2.5 1.5)', $point->toWKT()); + } + + public function testMultiPointToWKT() + { + $multipoint = new MultiPoint([new Point(1.5, 1.5), new Point(1.5, 2.5), new Point(2.5, 2.5)]); + + $this->assertEquals('MULTIPOINT((1.5 1.5),(2.5 1.5),(2.5 2.5))', $multipoint->toWKT()); + } + + public function testLineStringToWKT() + { + $linestring = new LineString([new Point(1.5, 1.5), new Point(2.5, 2.5), new Point(3.5, 3.5)]); + + $this->assertEquals('LINESTRING(1.5 1.5,2.5 2.5,3.5 3.5)', $linestring->toWKT()); + } + + public function testMultiLineStringToWKT() + { + $collection = new LineString( + [ + new Point(1.5, 1.5), + new Point(1.5, 2.5), + new Point(2.5, 2.5), + new Point(2.5, 1.5), + new Point(1.5, 1.5) + ] + ); + + $multilinestring = new MultiLineString([$collection]); + + $this->assertSame('MULTILINESTRING((1.5 1.5,2.5 1.5,2.5 2.5,1.5 2.5,1.5 1.5))', $multilinestring->toWKT()); + } + + public function testPolygonToWKT() + { + $collection = new LineString( + [ + new Point(1.5, 1.5), + new Point(1.5, 2.5), + new Point(2.5, 2.5), + new Point(2.5, 1.5), + new Point(1.5, 1.5) + ] + ); + + $polygon = new Polygon([$collection]); + + $this->assertEquals('POLYGON((1.5 1.5,2.5 1.5,2.5 2.5,1.5 2.5,1.5 1.5))', $polygon->toWKT()); + } + + public function testMultiPolygonToWKT() + { + $collection1 = new LineString( + [ + new Point(1.5, 1.5), + new Point(1.5, 2.5), + new Point(2.5, 2.5), + new Point(2.5, 1.5), + new Point(1.5, 1.5) + ] + ); + + $collection2 = new LineString( + [ + new Point(10.5, 10.5), + new Point(10.5, 20.5), + new Point(20.5, 20.5), + new Point(20.5, 10.5), + new Point(10.5, 10.5) + ] + ); + + $polygon1 = new Polygon([$collection1, $collection2]); + + $collection3 = new LineString( + [ + new Point(100.5, 100.5), + new Point(100.5, 200.5), + new Point(200.5, 200.5), + new Point(200.5, 100.5), + new Point(100.5, 100.5) + ] + ); + + + $polygon2 = new Polygon([$collection3]); + + $multiPolygon = new MultiPolygon([$polygon1, $polygon2]); + + + $this->assertEquals( + 'MULTIPOLYGON(((1.5 1.5,2.5 1.5,2.5 2.5,1.5 2.5,1.5 1.5),(10.5 10.5,20.5 10.5,20.5 20.5,10.5 20.5,10.5 10.5)),((100.5 100.5,200.5 100.5,200.5 200.5,100.5 200.5,100.5 100.5)))', + $multiPolygon->toWKT() + ); + } + + public function testGeometryCollectionToWKT() + { + $collection = new LineString( + [ + new Point(1.5, 1.5), + new Point(1.5, 2.5), + new Point(2.5, 2.5), + new Point(2.5, 1.5), + new Point(1.5, 1.5) + ] + ); + + $point = new Point(100.5, 200.5); + + $geo_collection = new GeometryCollection([$collection, $point]); + + + $this->assertEquals( + 'GEOMETRYCOLLECTION(LINESTRING(1.5 1.5,2.5 1.5,2.5 2.5,1.5 2.5,1.5 1.5),POINT(200.5 100.5))', + $geo_collection->toWKT() + ); + } + + +} From 3645ef3b4f36f3740016e9e0b29d3d7681c38e30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Hallet?= Date: Fri, 17 Nov 2017 14:12:16 +0100 Subject: [PATCH 2/3] normalize float output as tring --- src/Geometries/Point.php | 12 +++++++++--- tests/Geometries/UnderLocaleTest.php | 6 +----- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/Geometries/Point.php b/src/Geometries/Point.php index dc5d76a..d5a7f77 100644 --- a/src/Geometries/Point.php +++ b/src/Geometries/Point.php @@ -35,9 +35,15 @@ public function setLng($lng) public function toPair() { - return $this->getLng() . ' ' . $this->getLat(); + return self::stringifyFloat($this->getLng()) . ' ' . self::stringifyFloat($this->getLat()); } - + + // normalized output among locales + private static function stringifyFloat($float) + { + return rtrim(rtrim(sprintf('%F', $float), '0'), '.'); + } + public static function fromPair($pair) { $pair = preg_replace('/^[a-zA-Z\(\)]+/', '', trim($pair)); @@ -58,7 +64,7 @@ public static function fromString($wktArgument) public function __toString() { - return $this->getLng() . ' ' . $this->getLat(); + return $this->toPair(); } /** diff --git a/tests/Geometries/UnderLocaleTest.php b/tests/Geometries/UnderLocaleTest.php index edcf178..8809241 100644 --- a/tests/Geometries/UnderLocaleTest.php +++ b/tests/Geometries/UnderLocaleTest.php @@ -116,12 +116,10 @@ public function testMultiPolygonToWKT() ] ); - $polygon2 = new Polygon([$collection3]); $multiPolygon = new MultiPolygon([$polygon1, $polygon2]); - $this->assertEquals( 'MULTIPOLYGON(((1.5 1.5,2.5 1.5,2.5 2.5,1.5 2.5,1.5 1.5),(10.5 10.5,20.5 10.5,20.5 20.5,10.5 20.5,10.5 10.5)),((100.5 100.5,200.5 100.5,200.5 200.5,100.5 200.5,100.5 100.5)))', $multiPolygon->toWKT() @@ -143,13 +141,11 @@ public function testGeometryCollectionToWKT() $point = new Point(100.5, 200.5); $geo_collection = new GeometryCollection([$collection, $point]); - - + $this->assertEquals( 'GEOMETRYCOLLECTION(LINESTRING(1.5 1.5,2.5 1.5,2.5 2.5,1.5 2.5,1.5 1.5),POINT(200.5 100.5))', $geo_collection->toWKT() ); } - } From e86060d775b564312d460de72271bbe1e3f0b449 Mon Sep 17 00:00:00 2001 From: Nick Barrett Date: Mon, 20 Nov 2017 09:10:39 +0800 Subject: [PATCH 3/3] Update Point.php --- src/Geometries/Point.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Geometries/Point.php b/src/Geometries/Point.php index d5a7f77..559e3c0 100644 --- a/src/Geometries/Point.php +++ b/src/Geometries/Point.php @@ -38,9 +38,9 @@ public function toPair() return self::stringifyFloat($this->getLng()) . ' ' . self::stringifyFloat($this->getLat()); } - // normalized output among locales private static function stringifyFloat($float) { + // normalized output among locales return rtrim(rtrim(sprintf('%F', $float), '0'), '.'); }