Skip to content

Commit

Permalink
Add multi line string spatial type
Browse files Browse the repository at this point in the history
  • Loading branch information
ilijastuden committed Apr 23, 2022
1 parent 217e6f5 commit 03d2e30
Show file tree
Hide file tree
Showing 6 changed files with 188 additions and 3 deletions.
49 changes: 49 additions & 0 deletions src/Spatial/MultiLineString/MultiLineString.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/*
* This file is part of the Active Collab DatabaseConnection project.
*
* (c) A51 doo <[email protected]>. All rights reserved.
*/

declare(strict_types=1);

namespace ActiveCollab\DatabaseConnection\Spatial\MultiLineString;

use ActiveCollab\DatabaseConnection\Spatial\LineString\LineStringInterface;

class MultiLineString implements MultiLineStringInterface
{
/**
* @var LineStringInterface[]
*/
private array $lines;

public function __construct(LineStringInterface ...$lines)
{
$this->lines = $lines;
}

public function getLines(): array
{
return $this->lines;
}

public function toWkt(): string
{
return sprintf('MULTILINESTRING(%s)', $this);
}

public function __toString(): string
{
return implode(
',',
array_map(
function (LineStringInterface $line_string) {
return sprintf('(%s)', $line_string);
},
$this->getLines()
)
);
}
}
18 changes: 18 additions & 0 deletions src/Spatial/MultiLineString/MultiLineStringInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

/*
* This file is part of the Active Collab DatabaseConnection project.
*
* (c) A51 doo <[email protected]>. All rights reserved.
*/

declare(strict_types=1);

namespace ActiveCollab\DatabaseConnection\Spatial\MultiLineString;

use ActiveCollab\DatabaseConnection\Spatial\GeometricObjectInterface;

interface MultiLineStringInterface extends GeometricObjectInterface
{
public function getLines(): array;
}
4 changes: 2 additions & 2 deletions src/Spatial/MultiPolygon/MultiPolygon.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ public function __toString(): string
',',
array_map(
function (PolygonInterface $polygon) {
return sprintf('(%s)', (string) $polygon);
return sprintf('(%s)', $polygon);
},
$this->polygons
$this->getPolygons()
)
);
}
Expand Down
9 changes: 8 additions & 1 deletion src/Spatial/WktParser/WktParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

use ActiveCollab\DatabaseConnection\Spatial\LineString\LineString;
use ActiveCollab\DatabaseConnection\Spatial\LineString\LineStringInterface;
use ActiveCollab\DatabaseConnection\Spatial\MultiLineString\MultiLineString;
use ActiveCollab\DatabaseConnection\Spatial\MultiLineString\MultiLineStringInterface;
use ActiveCollab\DatabaseConnection\Spatial\MultiPoint\MultiPoint;
use ActiveCollab\DatabaseConnection\Spatial\MultiPoint\MultiPointInterface;
use ActiveCollab\DatabaseConnection\Spatial\MultiPolygon\MultiPolygon;
Expand Down Expand Up @@ -79,7 +81,7 @@ public function geomFromText($text) {
throw new InvalidWktException($text, $e);
}

if (in_array($type, [self::POINT, self::MULTI_POINT, self::LINE_STRING, self::POLYGON, self::MULTI_POLYGON])) {
if (in_array($type, [self::POINT, self::MULTI_POINT, self::LINE_STRING, self::MULTI_LINE_STRING, self::POLYGON, self::MULTI_POLYGON])) {
return $components;
}

Expand Down Expand Up @@ -118,6 +120,11 @@ protected function parseLineString(string $text): LineStringInterface
return new LineString(...$points);
}

private function parseMultiLineString(string $text): MultiLineStringInterface
{
return new MultiLineString(...$this->parseCollection($text, self::LINE_STRING));
}

private function parseLinearRing(string $text): LinearRingInterface
{
return new LinearRing(...$this->parseLineString($text)->getPoints());
Expand Down
42 changes: 42 additions & 0 deletions test/src/Spatial/MultiLineStringTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/*
* This file is part of the Active Collab DatabaseStructure project.
*
* (c) A51 doo <[email protected]>. All rights reserved.
*/

declare(strict_types=1);

namespace ActiveCollab\DatabaseConnection\Test\Spatial;

use ActiveCollab\DatabaseConnection\Spatial\Coordinate\Coordinate;
use ActiveCollab\DatabaseConnection\Spatial\LineString\LineString;
use ActiveCollab\DatabaseConnection\Spatial\MultiLineString\MultiLineString;
use ActiveCollab\DatabaseConnection\Spatial\Point\Point;
use ActiveCollab\DatabaseConnection\Test\Base\TestCase;

class MultiLineStringTest extends TestCase
{
public function testWillRenderWtk(): void
{
$this->assertSame(
'MULTILINESTRING((45.60317644 19.27315063,45.60312479 19.27319189,45.60473116 19.27750116,45.60478264 19.27745963),(45.60449426 19.27769178,45.60431683 19.27783455,45.60270942 19.27352285,45.60288728 19.27338113))',
(new MultiLineString(
new LineString(
new Point(new Coordinate(45.60317644), new Coordinate(19.27315063)),
new Point(new Coordinate(45.60312479), new Coordinate(19.27319189)),
new Point(new Coordinate(45.60473116), new Coordinate(19.27750116)),
new Point(new Coordinate(45.60478264), new Coordinate(19.27745963)),
),

new LineString(
new Point(new Coordinate(45.60449426), new Coordinate(19.27769178)),
new Point(new Coordinate(45.60431683), new Coordinate(19.27783455)),
new Point(new Coordinate(45.60270942), new Coordinate(19.27352285)),
new Point(new Coordinate(45.60288728), new Coordinate(19.27338113)),
),
))->toWkt()
);
}
}
69 changes: 69 additions & 0 deletions test/src/Spatial/SpatialColumnsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use ActiveCollab\DatabaseConnection\Result\ResultInterface;
use ActiveCollab\DatabaseConnection\Spatial\LineString\LineString;
use ActiveCollab\DatabaseConnection\Spatial\LineString\LineStringInterface;
use ActiveCollab\DatabaseConnection\Spatial\MultiLineString\MultiLineString;
use ActiveCollab\DatabaseConnection\Spatial\MultiLineString\MultiLineStringInterface;
use ActiveCollab\DatabaseConnection\Spatial\MultiPoint\MultiPoint;
use ActiveCollab\DatabaseConnection\Spatial\MultiPoint\MultiPointInterface;
use ActiveCollab\DatabaseConnection\Spatial\MultiPolygon\MultiPolygon;
Expand All @@ -34,6 +36,7 @@ public function tearDown(): void
$this->connection->dropTable('points');
$this->connection->dropTable('multi_points');
$this->connection->dropTable('line_strings');
$this->connection->dropTable('multi_line_strings');
$this->connection->dropTable('polygons');
$this->connection->dropTable('multi_polygons');

Expand Down Expand Up @@ -186,6 +189,72 @@ public function testWillReadAndWriteLineString(): void
}
}

public function testWillReadAndWriteMultiLineString(): void
{
$create_table = $this->connection->execute("CREATE TABLE IF NOT EXISTS `multi_line_strings` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`multi_line_string` MULTILINESTRING NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;");

$this->assertTrue($create_table);

$multi_line_string_to_write = new MultiLineString(
new LineString(
new Point(new Coordinate(45.60317644), new Coordinate(19.27315063)),
new Point(new Coordinate(45.60312479), new Coordinate(19.27319189)),
new Point(new Coordinate(45.60473116), new Coordinate(19.27750116)),
new Point(new Coordinate(45.60478264), new Coordinate(19.27745963)),
),

new LineString(
new Point(new Coordinate(45.60449426), new Coordinate(19.27769178)),
new Point(new Coordinate(45.60431683), new Coordinate(19.27783455)),
new Point(new Coordinate(45.60270942), new Coordinate(19.27352285)),
new Point(new Coordinate(45.60288728), new Coordinate(19.27338113)),
),
);

$inserted = $this->connection->insert(
'multi_line_strings',
[
'multi_line_string' => $multi_line_string_to_write,
]
);

$this->assertSame(1, $inserted);

$rows = $this->connection->execute(
'SELECT `id`, ST_AsText(`multi_line_string`) AS "multi_line_string" FROM `multi_line_strings`'
);
$this->assertInstanceOf(ResultInterface::class, $rows);

$rows->setValueCaster(
new ValueCaster(
[
'multi_line_string' => ValueCasterInterface::CAST_SPATIAL,
]
)
);

$first_row = $rows[0];

$this->assertInstanceOf(MultiLineStringInterface::class, $first_row['multi_line_string']);

/** @var MultiLineStringInterface $read_multi_line_strings */
$read_multi_line_strings = $first_row['multi_line_string'];

foreach ($read_multi_line_strings as $k => $read_multi_line_string) {
foreach ($read_multi_line_string->getPoints() as $j => $read_point) {
$this->assertTrue(
$read_point->isSame(
$multi_line_string_to_write->getLines()[$k]->getPoints()[$j]
)
);
}
}
}

public function testWillReadAndWritePolygon(): void
{
$create_table = $this->connection->execute("CREATE TABLE IF NOT EXISTS `polygons` (
Expand Down

0 comments on commit 03d2e30

Please sign in to comment.