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

Commit

Permalink
Merge pull request #153 from opengis/master
Browse files Browse the repository at this point in the history
Fixes MultiPoint::fromWKT() so it works with alternate no nested parenthesis wkt
  • Loading branch information
mstaack authored Mar 3, 2022
2 parents 6dbeb62 + 413f155 commit 9a30600
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/Geometries/MultiPoint.php
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

namespace MStaack\LaravelPostgis\Geometries;
use InvalidArgumentException;

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

public static function fromString($wktArgument)
{
if (!strpos(trim($wktArgument), '(')) {
$points = explode(',', $wktArgument);
$wktArgument = implode(',', array_map(function ($pair) {
return '(' . trim($pair) . ')';
}, $points));
};

$matches = [];
preg_match_all('/\(\s*(\d+\s+\d+(\s+\d+)?)\s*\)/', trim($wktArgument), $matches);
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);

if (count($matches) < 2) {
return new static([]);
Expand Down
24 changes: 24 additions & 0 deletions tests/Geometries/MultiPointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,22 @@ public function testFromWKT()
$this->assertEquals(3, $multipoint->count());
}

public function testFromWKTWithFloatingPoint()
{
$multipoint = MultiPoint::fromWKT('MULTIPOINT((1.0 1.0),(2.0 1.0),(2.0 2.0))');
$this->assertInstanceOf(MultiPoint::class, $multipoint);

$this->assertEquals(3, $multipoint->count());
}

public function testFromWKTWithoutNestedParentesis()
{
$multipoint = MultiPoint::fromWKT('MULTIPOINT(1 1, 2 1, 2 2)');
$this->assertInstanceOf(MultiPoint::class, $multipoint);

$this->assertEquals(3, $multipoint->count());
}

public function testFromWKT3d()
{
$multipoint = MultiPoint::fromWKT('MULTIPOINT Z((1 1 1),(2 1 3),(2 2 2))');
Expand All @@ -24,6 +40,14 @@ public function testFromWKT3d()
$this->assertEquals(3, $multipoint->count());
}

public function testFromWKT3dWithoutNestedParentesis()
{
$multipoint = MultiPoint::fromWKT('MULTIPOINT Z(1 1 1, 2 1 3, 2 2 2)');
$this->assertInstanceOf(MultiPoint::class, $multipoint);

$this->assertEquals(3, $multipoint->count());
}

public function testToWKT()
{
$collection = [new Point(1, 1), new Point(1, 2), new Point(2, 2)];
Expand Down

0 comments on commit 9a30600

Please sign in to comment.