Skip to content
This repository was archived by the owner on Oct 1, 2024. It is now read-only.

Commit 9a30600

Browse files
authored
Merge pull request #153 from opengis/master
Fixes MultiPoint::fromWKT() so it works with alternate no nested parenthesis wkt
2 parents 6dbeb62 + 413f155 commit 9a30600

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

src/Geometries/MultiPoint.php

100644100755
+9-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
namespace MStaack\LaravelPostgis\Geometries;
4+
use InvalidArgumentException;
45

56
class MultiPoint extends PointCollection implements GeometryInterface, \JsonSerializable
67
{
@@ -43,8 +44,15 @@ public static function fromWKT($wkt)
4344

4445
public static function fromString($wktArgument)
4546
{
47+
if (!strpos(trim($wktArgument), '(')) {
48+
$points = explode(',', $wktArgument);
49+
$wktArgument = implode(',', array_map(function ($pair) {
50+
return '(' . trim($pair) . ')';
51+
}, $points));
52+
};
53+
4654
$matches = [];
47-
preg_match_all('/\(\s*(\d+\s+\d+(\s+\d+)?)\s*\)/', trim($wktArgument), $matches);
55+
preg_match_all('/\(\s*([+-]?([0-9]+([.][0-9]*)?|[.][0-9]+)+\s+[+-]?([0-9]+([.][0-9]*)?|[.][0-9]+)+(\s+[+-]?([0-9]+([.][0-9]*)?|[.][0-9]+)+)?)\s*\)/', trim($wktArgument), $matches);
4856

4957
if (count($matches) < 2) {
5058
return new static([]);

tests/Geometries/MultiPointTest.php

+24
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,22 @@ public function testFromWKT()
1616
$this->assertEquals(3, $multipoint->count());
1717
}
1818

19+
public function testFromWKTWithFloatingPoint()
20+
{
21+
$multipoint = MultiPoint::fromWKT('MULTIPOINT((1.0 1.0),(2.0 1.0),(2.0 2.0))');
22+
$this->assertInstanceOf(MultiPoint::class, $multipoint);
23+
24+
$this->assertEquals(3, $multipoint->count());
25+
}
26+
27+
public function testFromWKTWithoutNestedParentesis()
28+
{
29+
$multipoint = MultiPoint::fromWKT('MULTIPOINT(1 1, 2 1, 2 2)');
30+
$this->assertInstanceOf(MultiPoint::class, $multipoint);
31+
32+
$this->assertEquals(3, $multipoint->count());
33+
}
34+
1935
public function testFromWKT3d()
2036
{
2137
$multipoint = MultiPoint::fromWKT('MULTIPOINT Z((1 1 1),(2 1 3),(2 2 2))');
@@ -24,6 +40,14 @@ public function testFromWKT3d()
2440
$this->assertEquals(3, $multipoint->count());
2541
}
2642

43+
public function testFromWKT3dWithoutNestedParentesis()
44+
{
45+
$multipoint = MultiPoint::fromWKT('MULTIPOINT Z(1 1 1, 2 1 3, 2 2 2)');
46+
$this->assertInstanceOf(MultiPoint::class, $multipoint);
47+
48+
$this->assertEquals(3, $multipoint->count());
49+
}
50+
2751
public function testToWKT()
2852
{
2953
$collection = [new Point(1, 1), new Point(1, 2), new Point(2, 2)];

0 commit comments

Comments
 (0)