Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/Objects/Geometry.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ abstract class Geometry implements Castable, Arrayable, Jsonable, JsonSerializab
{
public int $srid = 0;

abstract public function toWkt(bool $withFunction = true): string;
abstract public function toWkt(): string;

abstract public function getWktData(): string;

/**
* @param int $options
Expand Down
24 changes: 12 additions & 12 deletions src/Objects/GeometryCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,20 @@ public function __construct(Collection|array $geometries, int $srid = 0)
$this->validateGeometriesCount();
}

public function toWkt(bool $withFunction = true): string
public function toWkt(): string
{
$wkt = $this->toCollectionWkt(withFunction: true);
$wktData = $this->getWktData();

return "GEOMETRYCOLLECTION({$wkt})";
return "GEOMETRYCOLLECTION({$wktData})";
}

public function getWktData(): string
{
return $this->geometries
->map(static function (Geometry $geometry): string {
return $geometry->toWkt();
})
->join(', ');
}

/**
Expand Down Expand Up @@ -152,15 +161,6 @@ protected function validateGeometriesType(): void
});
}

protected function toCollectionWkt(bool $withFunction): string
{
return $this->geometries
->map(static function (Geometry $geometry) use ($withFunction): string {
return $geometry->toWkt($withFunction);
})
->join(', ');
}

/**
* Checks whether the class is used directly or via a sub-class.
*
Expand Down
17 changes: 11 additions & 6 deletions src/Objects/LineString.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,19 @@ class LineString extends PointCollection
{
protected int $minimumGeometries = 2;

public function toWkt(bool $withFunction = true): string
public function toWkt(): string
{
$wkt = $this->toCollectionWkt(withFunction: false);
$wktData = $this->getWktData();

if ($withFunction) {
return "LINESTRING(${wkt})";
}
return "LINESTRING({$wktData})";
}

return "(${wkt})";
public function getWktData(): string
{
return $this->geometries
->map(static function (Point $point): string {
return $point->getWktData();
})
->join(', ');
}
}
17 changes: 14 additions & 3 deletions src/Objects/MultiLineString.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,21 @@ public function __construct(Collection|array $geometries, int $srid = 0)
parent::__construct($geometries, $srid);
}

public function toWkt(bool $withFunction = true): string
public function toWkt(): string
{
$wkt = $this->toCollectionWkt(withFunction: false);
$wktData = $this->getWktData();

return "MULTILINESTRING({$wkt})";
return "MULTILINESTRING({$wktData})";
}

public function getWktData(): string
{
return $this->geometries
->map(static function (LineString $lineString): string {
$wktData = $lineString->getWktData();

return "({$wktData})";
})
->join(', ');
}
}
15 changes: 12 additions & 3 deletions src/Objects/MultiPoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,19 @@ class MultiPoint extends PointCollection
{
protected int $minimumGeometries = 1;

public function toWkt(bool $withFunction = true): string
public function toWkt(): string
{
$wkt = $this->toCollectionWkt(withFunction: false);
$wktData = $this->getWktData();

return "MULTIPOINT({$wkt})";
return "MULTIPOINT({$wktData})";
}

public function getWktData(): string
{
return $this->geometries
->map(static function (Point $point): string {
return $point->getWktData();
})
->join(', ');
}
}
17 changes: 14 additions & 3 deletions src/Objects/MultiPolygon.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,21 @@ public function __construct(Collection|array $geometries, int $srid = 0)
parent::__construct($geometries, $srid);
}

public function toWkt(bool $withFunction = true): string
public function toWkt(): string
{
$wkt = $this->toCollectionWkt(withFunction: false);
$wktData = $this->getWktData();

return "MULTIPOLYGON({$wkt})";
return "MULTIPOLYGON({$wktData})";
}

public function getWktData(): string
{
return $this->geometries
->map(static function (Polygon $polygon): string {
$wktData = $polygon->getWktData();

return "({$wktData})";
})
->join(', ');
}
}
13 changes: 7 additions & 6 deletions src/Objects/Point.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@ public function __construct(float $latitude, float $longitude, int $srid = 0)
$this->srid = $srid;
}

public function toWkt(bool $withFunction = true): string
public function toWkt(): string
{
$wkt = "{$this->longitude} {$this->latitude}";
$wktData = $this->getWktData();

if ($withFunction) {
return "POINT({$wkt})";
}
return "POINT({$wktData})";
}

return $wkt;
public function getWktData(): string
{
return "{$this->longitude} {$this->latitude}";
}

/**
Expand Down
19 changes: 13 additions & 6 deletions src/Objects/Polygon.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,21 @@

class Polygon extends MultiLineString
{
public function toWkt(bool $withFunction = true): string
public function toWkt(): string
{
$wkt = $this->toCollectionWkt(withFunction: false);
$wktData = $this->getWktData();

if ($withFunction) {
return "POLYGON({$wkt})";
}
return "POLYGON({$wktData})";
}

public function getWktData(): string
{
return $this->geometries
->map(static function (LineString $lineString): string {
$wktData = $lineString->getWktData();

return "(${wkt})";
return "({$wktData})";
})
->join(', ');
}
}
2 changes: 1 addition & 1 deletion src/SpatialBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ public function whereSrid(
protected function toExpression(Geometry|string $geometryOrColumn): Expression
{
if ($geometryOrColumn instanceof Geometry) {
$wkt = $geometryOrColumn->toWkt(withFunction: true);
$wkt = $geometryOrColumn->toWkt();

return DB::raw("ST_GeomFromText('{$wkt}')");
}
Expand Down
24 changes: 22 additions & 2 deletions tests/Objects/GeometryCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@
new Point(180, 0),
]);

$geometryCollectionFromWkt = GeometryCollection::fromWkt('GEOMETRYCOLLECTION(POLYGON((0 180, 1 179, 2 178, 3 177, 0 180)),POINT(0 180))');
$geometryCollectionFromWkt = GeometryCollection::fromWkt('GEOMETRYCOLLECTION(POLYGON((0 180, 1 179, 2 178, 3 177, 0 180)), POINT(0 180))');

expect($geometryCollectionFromWkt)->toEqual($geometryCollection);
});
Expand All @@ -180,11 +180,31 @@
new Point(180, 0),
], 4326);

$geometryCollectionFromWkt = GeometryCollection::fromWkt('GEOMETRYCOLLECTION(POLYGON((0 180, 1 179, 2 178, 3 177, 0 180)),POINT(0 180))', 4326);
$geometryCollectionFromWkt = GeometryCollection::fromWkt('GEOMETRYCOLLECTION(POLYGON((0 180, 1 179, 2 178, 3 177, 0 180)), POINT(0 180))', 4326);

expect($geometryCollectionFromWkt)->toEqual($geometryCollection);
});

it('generates geometry collection WKT', function (): void {
$geometryCollection = new GeometryCollection([
new Polygon([
new LineString([
new Point(180, 0),
new Point(179, 1),
new Point(178, 2),
new Point(177, 3),
new Point(180, 0),
]),
]),
new Point(180, 0),
]);

$wkt = $geometryCollection->toWkt();

$expectedWkt = 'GEOMETRYCOLLECTION(POLYGON((0 180, 1 179, 2 178, 3 177, 0 180)), POINT(0 180))';
expect($wkt)->toBe($expectedWkt);
});

it('creates geometry collection from WKB', function (): void {
$geometryCollection = new GeometryCollection([
new Polygon([
Expand Down
12 changes: 12 additions & 0 deletions tests/Objects/LineStringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,18 @@
expect($lineStringFromWkt)->toEqual($lineString);
});

it('generates line string WKT', function (): void {
$lineString = new LineString([
new Point(180, 0),
new Point(179, 1),
]);

$wkt = $lineString->toWkt();

$expectedWkt = 'LINESTRING(0 180, 1 179)';
expect($wkt)->toBe($expectedWkt);
});

it('creates line string from WKB', function (): void {
$lineString = new LineString([
new Point(180, 0),
Expand Down
14 changes: 14 additions & 0 deletions tests/Objects/MultiLineStringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,20 @@
expect($multiLineStringFromWkt)->toEqual($multiLineString);
});

it('generates multi line string WKT', function (): void {
$multiLineString = new MultiLineString([
new LineString([
new Point(180, 0),
new Point(179, 1),
]),
]);

$wkt = $multiLineString->toWkt();

$expectedWkt = 'MULTILINESTRING((0 180, 1 179))';
expect($wkt)->toBe($expectedWkt);
});

it('creates multi line string from WKB', function (): void {
$multiLineString = new MultiLineString([
new LineString([
Expand Down
11 changes: 11 additions & 0 deletions tests/Objects/MultiPointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,17 @@
expect($multiPointFromWkt)->toEqual($multiPoint);
});

it('generates multi point WKT', function (): void {
$multiPoint = new MultiPoint([
new Point(180, 0),
]);

$wkt = $multiPoint->toWkt();

$expectedWkt = 'MULTIPOINT(0 180)';
expect($wkt)->toBe($expectedWkt);
});

it('creates multi point from WKB', function (): void {
$multiPoint = new MultiPoint([
new Point(180, 0),
Expand Down
19 changes: 19 additions & 0 deletions tests/Objects/MultiPolygonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,25 @@
expect($multiPolygonFromWkt)->toEqual($multiPolygon);
});

it('generates multi polygon WKT', function (): void {
$multiPolygon = new MultiPolygon([
new Polygon([
new LineString([
new Point(180, 0),
new Point(179, 1),
new Point(178, 2),
new Point(177, 3),
new Point(180, 0),
]),
]),
]);

$wkt = $multiPolygon->toWkt();

$expectedWkt = 'MULTIPOLYGON(((0 180, 1 179, 2 178, 3 177, 0 180)))';
expect($wkt)->toBe($expectedWkt);
});

it('creates multi polygon from WKB', function (): void {
$multiPolygon = new MultiPolygon([
new Polygon([
Expand Down
11 changes: 10 additions & 1 deletion tests/Objects/PointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
expect($pointFromJson)->toEqual($point);
});

it('generates point json', function (): void {
it('generates point JSON', function (): void {
$point = new Point(180, 0);

$json = $point->toJson();
Expand Down Expand Up @@ -72,6 +72,15 @@
expect($pointFromWkt)->toEqual($point);
});

it('generates point WKT', function (): void {
$point = new Point(180, 0);

$wkt = $point->toWkt();

$expectedWkt = 'POINT(0 180)';
expect($wkt)->toBe($expectedWkt);
});

it('creates point from WKB', function (): void {
$point = new Point(180, 0);

Expand Down
17 changes: 17 additions & 0 deletions tests/Objects/PolygonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,23 @@
expect($polygonFromWkt)->toEqual($polygon);
});

it('generates polygon WKT', function (): void {
$polygon = new Polygon([
new LineString([
new Point(180, 0),
new Point(179, 1),
new Point(178, 2),
new Point(177, 3),
new Point(180, 0),
]),
]);

$wkt = $polygon->toWkt();

$expectedWkt = 'POLYGON((0 180, 1 179, 2 178, 3 177, 0 180))';
expect($wkt)->toBe($expectedWkt);
});

it('creates polygon from WKB', function (): void {
$polygon = new Polygon([
new LineString([
Expand Down