Skip to content

Commit

Permalink
Implement makeValid() TODO README
Browse files Browse the repository at this point in the history
  • Loading branch information
BenMorel committed Jan 22, 2024
1 parent fbac12c commit 9cccdc5
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Engine/DatabaseEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,11 @@ public function isRing(Curve $curve) : bool
}
}

public function makeValid(Geometry $g) : Geometry
{
return $this->queryGeometry('ST_MakeValid', $g);
}

public function equals(Geometry $a, Geometry $b) : bool
{
return $this->queryBoolean('ST_Equals', $a, $b);
Expand Down
5 changes: 5 additions & 0 deletions src/Engine/GEOSEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,11 @@ public function isRing(Curve $curve) : bool
}
}

public function makeValid(Geometry $g): Geometry
{
throw GeometryEngineException::unimplementedMethod(__METHOD__);
}

public function equals(Geometry $a, Geometry $b) : bool
{
try {
Expand Down
9 changes: 9 additions & 0 deletions src/Engine/GeometryEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,15 @@ public function isSimple(Geometry $g) : bool;
*/
public function isRing(Curve $curve) : bool;

/**
* Attempts to create a valid representation of a given invalid geometry without losing any of the input vertices.
*
* Valid geometries are returned unchanged.
*
* @throws GeometryEngineException If the operation is not supported by the engine.
*/
public function makeValid(Geometry $g) : Geometry;

/**
* Returns true if the given geometries are spatially equal.
*
Expand Down
29 changes: 29 additions & 0 deletions tests/GeometryEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,35 @@ public function providerIsValid() : array
['POLYGON ((0 0, 0 1, 1 0, 1 1, 0 0))', false],
];
}

/**
* @dataProvider providerMakeValid
*
* @param string $geometry The WKT of the geometry to test.
* @param string $validGeometry The WKT of the expected geometry.
*/
public function testMakeValid(string $geometry, string $validGeometry) : void
{
$geometryEngine = $this->getGeometryEngine();

$this->requireEngine(['SpatiaLite', 'PostGIS']);

$geometry = Geometry::fromText($geometry);
$validGeometry = Geometry::fromText($validGeometry);

$this->assertGeometryEquals($validGeometry, $geometryEngine->makeValid($geometry));
}

public function providerMakeValid() : array
{
return [
['POINT (1 2)', 'POINT (1 2)'],
['LINESTRING (1 2, 3 4)', 'LINESTRING (1 2, 3 4)'],
['MULTIPOLYGON(((0 2, 10 12, 12 10, 2 0, 0 2)),((0 6, 6 12, 12 6, 6 0, 0 6)))', 'MULTIPOLYGON(((2 0,0 2,2 4,4 2,2 0)),((0 6,6 12,8 10,2 4,0 6)),((12 6,6 0,4 2,10 8,12 6)),((8 10,10 12,12 10,10 8,8 10)))'],
['MULTIPOLYGON(((0 2, 10 12, 12 10, 2 0, 0 2)),((0 6, 6 12, 12 6, 6 0, 0 6)))', 'MULTIPOLYGON(((0 2, 2 4, 4 2, 2 0, 0 2)), ((0 6, 6 12, 8 10, 2 4, 0 6)), ((10 12, 12 10, 10 8, 8 10, 10 12)), ((12 6, 6 0, 4 2, 10 8, 12 6)))'],
];
}

/**
* @dataProvider providerIsClosed
*
Expand Down

0 comments on commit 9cccdc5

Please sign in to comment.