Skip to content

Commit

Permalink
allow setting default srid
Browse files Browse the repository at this point in the history
  • Loading branch information
MatanYadaev committed Nov 23, 2024
1 parent 1da5fd6 commit 43edde6
Show file tree
Hide file tree
Showing 12 changed files with 241 additions and 32 deletions.
20 changes: 10 additions & 10 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@

## Available geometry classes

* `Point(float $latitude, float $longitude, int|Srid $srid = 0)` - [MySQL Point](https://dev.mysql.com/doc/refman/8.0/en/gis-class-point.html)
* `MultiPoint(Point[] | Collection<Point> $geometries, int|Srid $srid = 0)` - [MySQL MultiPoint](https://dev.mysql.com/doc/refman/8.0/en/gis-class-multipoint.html)
* `LineString(Point[] | Collection<Point> $geometries, int|Srid $srid = 0)` - [MySQL LineString](https://dev.mysql.com/doc/refman/8.0/en/gis-class-linestring.html)
* `MultiLineString(LineString[] | Collection<LineString> $geometries, int|Srid $srid = 0)` - [MySQL MultiLineString](https://dev.mysql.com/doc/refman/8.0/en/gis-class-multilinestring.html)
* `Polygon(LineString[] | Collection<LineString> $geometries, int|Srid $srid = 0)` - [MySQL Polygon](https://dev.mysql.com/doc/refman/8.0/en/gis-class-polygon.html)
* `MultiPolygon(Polygon[] | Collection<Polygon> $geometries, int|Srid $srid = 0)` - [MySQL MultiPolygon](https://dev.mysql.com/doc/refman/8.0/en/gis-class-multipolygon.html)
* `GeometryCollection(Geometry[] | Collection<Geometry> $geometries, int|Srid $srid = 0)` - [MySQL GeometryCollection](https://dev.mysql.com/doc/refman/8.0/en/gis-class-geometrycollection.html)
* `Point(float $latitude, float $longitude, int|Srid|null $srid = null)` - [MySQL Point](https://dev.mysql.com/doc/refman/8.0/en/gis-class-point.html)
* `MultiPoint(Point[] | Collection<Point> $geometries, int|Srid|null $srid = null)` - [MySQL MultiPoint](https://dev.mysql.com/doc/refman/8.0/en/gis-class-multipoint.html)
* `LineString(Point[] | Collection<Point> $geometries, int|Srid|null $srid = null)` - [MySQL LineString](https://dev.mysql.com/doc/refman/8.0/en/gis-class-linestring.html)
* `MultiLineString(LineString[] | Collection<LineString> $geometries, int|Srid|null $srid = null)` - [MySQL MultiLineString](https://dev.mysql.com/doc/refman/8.0/en/gis-class-multilinestring.html)
* `Polygon(LineString[] | Collection<LineString> $geometries, int|Srid|null $srid = null)` - [MySQL Polygon](https://dev.mysql.com/doc/refman/8.0/en/gis-class-polygon.html)
* `MultiPolygon(Polygon[] | Collection<Polygon> $geometries, int|Srid|null $srid = null)` - [MySQL MultiPolygon](https://dev.mysql.com/doc/refman/8.0/en/gis-class-multipolygon.html)
* `GeometryCollection(Geometry[] | Collection<Geometry> $geometries, int|Srid|null $srid = null)` - [MySQL GeometryCollection](https://dev.mysql.com/doc/refman/8.0/en/gis-class-geometrycollection.html)

Geometry classes can be also created by these static methods:

* `fromArray(array $geometry, int|Srid $srid = 0)` - Creates a geometry object from a [GeoJSON](https://en.wikipedia.org/wiki/GeoJSON) array.
* `fromJson(string $geoJson, int|Srid $srid = 0)` - Creates a geometry object from a [GeoJSON](https://en.wikipedia.org/wiki/GeoJSON) string.
* `fromWkt(string $wkt, int|Srid $srid = 0)` - Creates a geometry object from a [WKT](https://en.wikipedia.org/wiki/Well-known_text_representation_of_geometry).
* `fromArray(array $geometry, int|Srid|null $srid = null)` - Creates a geometry object from a [GeoJSON](https://en.wikipedia.org/wiki/GeoJSON) array.
* `fromJson(string $geoJson, int|Srid|null $srid = null)` - Creates a geometry object from a [GeoJSON](https://en.wikipedia.org/wiki/GeoJSON) string.
* `fromWkt(string $wkt, int|Srid|null $srid = null)` - Creates a geometry object from a [WKT](https://en.wikipedia.org/wiki/Well-known_text_representation_of_geometry).
* `fromWkb(string $wkb)` - Creates a geometry object from a [WKB](https://en.wikipedia.org/wiki/Well-known_text_representation_of_geometry#Well-known_binary).

## Available geometry class methods
Expand Down
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,25 @@ $place = Place::create([
echo $place->coordinates->toCustomArray(); // ['longitude' => -0.1217424, 'latitude' => 51.5032973]
```
## Set default SRID
By default, the SRID is set to 0 (EPSG:0).
You can set the default SRID for your application by setting the `SRID` constant in a service provider's `boot` method:
```php
use MatanYadaev\EloquentSpatial\Enums\Srid;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
public function boot(): void
{
// Set the default SRID to WGS84 (EPSG:4326)
EloquentSpatial::setDefaultSrid(Srid::WGS84);
}
}
```
## Development
Here are some useful commands for development:
Expand Down
8 changes: 8 additions & 0 deletions src/EloquentSpatial.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace MatanYadaev\EloquentSpatial;

use MatanYadaev\EloquentSpatial\Enums\Srid;
use MatanYadaev\EloquentSpatial\Objects\GeometryCollection;
use MatanYadaev\EloquentSpatial\Objects\LineString;
use MatanYadaev\EloquentSpatial\Objects\MultiLineString;
Expand Down Expand Up @@ -35,6 +36,8 @@ class EloquentSpatial
/** @var class-string<Polygon> */
public static string $polygon = Polygon::class;

public static int $defaultSrid = 0;

/**
* @param class-string<GeometryCollection> $class
*/
Expand Down Expand Up @@ -104,4 +107,9 @@ public static function usePolygon(string $class): string

return static::$polygon;
}

public static function setDefaultSrid(Srid|int $srid): void
{
static::$defaultSrid = $srid instanceof Srid ? $srid->value : $srid;
}
}
23 changes: 23 additions & 0 deletions src/Helper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace MatanYadaev\EloquentSpatial;

use MatanYadaev\EloquentSpatial\Enums\Srid;

class Helper
{
public static function getSrid(Srid|int|null $srid = null): int
{
if ($srid instanceof Srid) {
return $srid->value;
}

if (is_int($srid)) {
return $srid;
}

return EloquentSpatial::$defaultSrid;
}
}
13 changes: 7 additions & 6 deletions src/Objects/Geometry.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@
use MatanYadaev\EloquentSpatial\Factory;
use MatanYadaev\EloquentSpatial\GeometryCast;
use MatanYadaev\EloquentSpatial\GeometryExpression;
use MatanYadaev\EloquentSpatial\Helper;
use Stringable;
use WKB as geoPHPWkb;

abstract class Geometry implements Arrayable, Castable, Jsonable, JsonSerializable, Stringable
{
use Macroable;

public int $srid = 0;
public int $srid;

abstract public function toWkt(): string;

Expand Down Expand Up @@ -90,10 +91,10 @@ public static function fromWkb(string $wkb): static
/**
* @throws InvalidArgumentException
*/
public static function fromWkt(string $wkt, int|Srid $srid = 0): static
public static function fromWkt(string $wkt, int|Srid|null $srid = null): static
{
$geometry = Factory::parse($wkt);
$geometry->srid = $srid instanceof Srid ? $srid->value : $srid;
$geometry->srid = Helper::getSrid($srid);

if (! ($geometry instanceof static)) {
throw new InvalidArgumentException(
Expand All @@ -107,10 +108,10 @@ public static function fromWkt(string $wkt, int|Srid $srid = 0): static
/**
* @throws InvalidArgumentException
*/
public static function fromJson(string $geoJson, int|Srid $srid = 0): static
public static function fromJson(string $geoJson, int|Srid|null $srid = null): static
{
$geometry = Factory::parse($geoJson);
$geometry->srid = $srid instanceof Srid ? $srid->value : $srid;
$geometry->srid = Helper::getSrid($srid);

if (! ($geometry instanceof static)) {
throw new InvalidArgumentException(
Expand All @@ -126,7 +127,7 @@ public static function fromJson(string $geoJson, int|Srid $srid = 0): static
*
* @throws JsonException
*/
public static function fromArray(array $geometry, int|Srid $srid = 0): static
public static function fromArray(array $geometry, int|Srid|null $srid = null): static
{
$geoJson = json_encode($geometry, JSON_THROW_ON_ERROR);

Expand Down
5 changes: 3 additions & 2 deletions src/Objects/GeometryCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Support\Str;
use InvalidArgumentException;
use MatanYadaev\EloquentSpatial\Enums\Srid;
use MatanYadaev\EloquentSpatial\Helper;

class GeometryCollection extends Geometry implements ArrayAccess
{
Expand All @@ -24,14 +25,14 @@ class GeometryCollection extends Geometry implements ArrayAccess
*
* @throws InvalidArgumentException
*/
public function __construct(Collection|array $geometries, int|Srid $srid = 0)
public function __construct(Collection|array $geometries, int|Srid|null $srid = null)
{
if (is_array($geometries)) {
$geometries = collect($geometries);
}

$this->geometries = $geometries;
$this->srid = $srid instanceof Srid ? $srid->value : $srid;
$this->srid = Helper::getSrid($srid);

$this->validateGeometriesType();
$this->validateGeometriesCount();
Expand Down
4 changes: 2 additions & 2 deletions src/Objects/MultiLineString.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ class MultiLineString extends GeometryCollection
*
* @throws InvalidArgumentException
*/
public function __construct(Collection|array $geometries, int|Srid $srid = 0)
public function __construct(Collection|array $geometries, int|Srid|null $srid = null)
{
// @phpstan-ignore-next-line
parent::__construct($geometries, $this->srid = $srid instanceof Srid ? $srid->value : $srid);
parent::__construct($geometries, $srid);
}

public function toWkt(): string
Expand Down
4 changes: 2 additions & 2 deletions src/Objects/MultiPolygon.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ class MultiPolygon extends GeometryCollection
*
* @throws InvalidArgumentException
*/
public function __construct(Collection|array $geometries, int|Srid $srid = 0)
public function __construct(Collection|array $geometries, int|Srid|null $srid = null)
{
// @phpstan-ignore-next-line
parent::__construct($geometries, $this->srid = $srid instanceof Srid ? $srid->value : $srid);
parent::__construct($geometries, $srid);
}

public function toWkt(): string
Expand Down
5 changes: 3 additions & 2 deletions src/Objects/Point.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@
namespace MatanYadaev\EloquentSpatial\Objects;

use MatanYadaev\EloquentSpatial\Enums\Srid;
use MatanYadaev\EloquentSpatial\Helper;

class Point extends Geometry
{
public float $latitude;

public float $longitude;

public function __construct(float $latitude, float $longitude, int|Srid $srid = 0)
public function __construct(float $latitude, float $longitude, int|Srid|null $srid = null)
{
$this->latitude = $latitude;
$this->longitude = $longitude;
$this->srid = $srid instanceof Srid ? $srid->value : $srid;
$this->srid = Helper::getSrid($srid);
}

public function toWkt(): string
Expand Down
4 changes: 2 additions & 2 deletions src/Objects/PointCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ abstract class PointCollection extends GeometryCollection
*
* @throws InvalidArgumentException
*/
public function __construct(Collection|array $geometries, int|Srid $srid = 0)
public function __construct(Collection|array $geometries, int|Srid|null $srid = null)
{
// @phpstan-ignore-next-line
parent::__construct($geometries, $this->srid = $srid instanceof Srid ? $srid->value : $srid);
parent::__construct($geometries, $srid);
}
}
101 changes: 98 additions & 3 deletions tests/Objects/GeometryCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@
expect($testPlace->geometry_collection->srid)->toBe(Srid::WGS84->value);
});

it('creates geometry collection from JSON', function (): void {
it('creates geometry collection with default 0 SRID from JSON', function (): void {
// Arrange
EloquentSpatial::setDefaultSrid(0);
$geometryCollection = new GeometryCollection([
new Polygon([
new LineString([
Expand All @@ -86,9 +88,39 @@
new Point(0, 180),
]);

// Act
$geometryCollectionFromJson = GeometryCollection::fromJson('{"type":"GeometryCollection","geometries":[{"type":"Polygon","coordinates":[[[180,0],[179,1],[178,2],[177,3],[180,0]]]},{"type":"Point","coordinates":[180,0]}]}');

// Assert
expect($geometryCollectionFromJson)->toEqual($geometryCollection);
expect($geometryCollectionFromJson->srid)->toBe(0);
});

it('creates geometry collection with default 4326 SRID from JSON', function (): void {
// Arrange
EloquentSpatial::setDefaultSrid(Srid::WGS84);
$geometryCollection = new GeometryCollection([
new Polygon([
new LineString([
new Point(0, 180),
new Point(1, 179),
new Point(2, 178),
new Point(3, 177),
new Point(0, 180),
]),
]),
new Point(0, 180),
]);

// Act
$geometryCollectionFromJson = GeometryCollection::fromJson('{"type":"GeometryCollection","geometries":[{"type":"Polygon","coordinates":[[[180,0],[179,1],[178,2],[177,3],[180,0]]]},{"type":"Point","coordinates":[180,0]}]}');

// Assert
expect($geometryCollectionFromJson->toWkt())->toBe($geometryCollection->toWkt());
expect($geometryCollectionFromJson->srid)->toBe(Srid::WGS84->value);

// Cleanup
EloquentSpatial::setDefaultSrid(0);
});

it('creates geometry collection with SRID from JSON', function (): void {
Expand All @@ -110,7 +142,9 @@
expect($geometryCollectionFromJson)->toEqual($geometryCollection);
});

it('creates geometry collection from array', function (): void {
it('creates geometry collection with default 0 SRID from array', function (): void {
// Arrange
EloquentSpatial::setDefaultSrid(0);
$geometryCollection = new GeometryCollection([
new Polygon([
new LineString([
Expand All @@ -124,9 +158,39 @@
new Point(0, 180),
]);

// Act
$geometryCollectionFromJson = GeometryCollection::fromArray(json_decode('{"type":"GeometryCollection","geometries":[{"type":"Polygon","coordinates":[[[180,0],[179,1],[178,2],[177,3],[180,0]]]},{"type":"Point","coordinates":[180,0]}]}', true));

// Assert
expect($geometryCollectionFromJson)->toEqual($geometryCollection);
expect($geometryCollectionFromJson->srid)->toBe(0);
});

it('creates geometry collection with default 4326 SRID from array', function (): void {
// Arrange
EloquentSpatial::setDefaultSrid(Srid::WGS84);
$geometryCollection = new GeometryCollection([
new Polygon([
new LineString([
new Point(0, 180),
new Point(1, 179),
new Point(2, 178),
new Point(3, 177),
new Point(0, 180),
]),
]),
new Point(0, 180),
]);

// Act
$geometryCollectionFromJson = GeometryCollection::fromArray(json_decode('{"type":"GeometryCollection","geometries":[{"type":"Polygon","coordinates":[[[180,0],[179,1],[178,2],[177,3],[180,0]]]},{"type":"Point","coordinates":[180,0]}]}', true));

// Assert
expect($geometryCollectionFromJson->toWkt())->toBe($geometryCollection->toWkt());
expect($geometryCollectionFromJson->srid)->toBe(Srid::WGS84->value);

// Cleanup
EloquentSpatial::setDefaultSrid(0);
});

it('creates geometry collection with SRID from array', function (): void {
Expand Down Expand Up @@ -264,7 +328,9 @@
expect($featureCollectionJson)->toBe($expectedFeatureCollectionJson);
});

it('creates geometry collection from WKT', function (): void {
it('creates geometry collection with default 0 SRID from WKT', function (): void {
// Arrange
EloquentSpatial::setDefaultSrid(0);
$geometryCollection = new GeometryCollection([
new Polygon([
new LineString([
Expand All @@ -278,11 +344,40 @@
new Point(0, 180),
]);

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

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

it('creates geometry collection with default 4326 SRID from WKT', function (): void {
// Arrange
EloquentSpatial::setDefaultSrid(Srid::WGS84);
$geometryCollection = new GeometryCollection([
new Polygon([
new LineString([
new Point(0, 180),
new Point(1, 179),
new Point(2, 178),
new Point(3, 177),
new Point(0, 180),
]),
]),
new Point(0, 180),
]);

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

// Assert
expect($geometryCollectionFromWkt->toWkt())->toBe($geometryCollection->toWkt());
expect($geometryCollectionFromWkt->srid)->toBe(Srid::WGS84->value);

// Cleanup
EloquentSpatial::setDefaultSrid(0);
});

it('creates geometry collection with SRID from WKT', function (): void {
$geometryCollection = new GeometryCollection([
new Polygon([
Expand Down
Loading

0 comments on commit 43edde6

Please sign in to comment.