Skip to content

Commit 43edde6

Browse files
committed
allow setting default srid
1 parent 1da5fd6 commit 43edde6

12 files changed

+241
-32
lines changed

API.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@
22

33
## Available geometry classes
44

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

1313
Geometry classes can be also created by these static methods:
1414

15-
* `fromArray(array $geometry, int|Srid $srid = 0)` - Creates a geometry object from a [GeoJSON](https://en.wikipedia.org/wiki/GeoJSON) array.
16-
* `fromJson(string $geoJson, int|Srid $srid = 0)` - Creates a geometry object from a [GeoJSON](https://en.wikipedia.org/wiki/GeoJSON) string.
17-
* `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).
15+
* `fromArray(array $geometry, int|Srid|null $srid = null)` - Creates a geometry object from a [GeoJSON](https://en.wikipedia.org/wiki/GeoJSON) array.
16+
* `fromJson(string $geoJson, int|Srid|null $srid = null)` - Creates a geometry object from a [GeoJSON](https://en.wikipedia.org/wiki/GeoJSON) string.
17+
* `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).
1818
* `fromWkb(string $wkb)` - Creates a geometry object from a [WKB](https://en.wikipedia.org/wiki/Well-known_text_representation_of_geometry#Well-known_binary).
1919

2020
## Available geometry class methods

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,25 @@ $place = Place::create([
256256
echo $place->coordinates->toCustomArray(); // ['longitude' => -0.1217424, 'latitude' => 51.5032973]
257257
```
258258

259+
## Set default SRID
260+
261+
By default, the SRID is set to 0 (EPSG:0).
262+
You can set the default SRID for your application by setting the `SRID` constant in a service provider's `boot` method:
263+
264+
```php
265+
use MatanYadaev\EloquentSpatial\Enums\Srid;
266+
use Illuminate\Support\ServiceProvider;
267+
268+
class AppServiceProvider extends ServiceProvider
269+
{
270+
public function boot(): void
271+
{
272+
// Set the default SRID to WGS84 (EPSG:4326)
273+
EloquentSpatial::setDefaultSrid(Srid::WGS84);
274+
}
275+
}
276+
```
277+
259278
## Development
260279

261280
Here are some useful commands for development:

src/EloquentSpatial.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace MatanYadaev\EloquentSpatial;
66

7+
use MatanYadaev\EloquentSpatial\Enums\Srid;
78
use MatanYadaev\EloquentSpatial\Objects\GeometryCollection;
89
use MatanYadaev\EloquentSpatial\Objects\LineString;
910
use MatanYadaev\EloquentSpatial\Objects\MultiLineString;
@@ -35,6 +36,8 @@ class EloquentSpatial
3536
/** @var class-string<Polygon> */
3637
public static string $polygon = Polygon::class;
3738

39+
public static int $defaultSrid = 0;
40+
3841
/**
3942
* @param class-string<GeometryCollection> $class
4043
*/
@@ -104,4 +107,9 @@ public static function usePolygon(string $class): string
104107

105108
return static::$polygon;
106109
}
110+
111+
public static function setDefaultSrid(Srid|int $srid): void
112+
{
113+
static::$defaultSrid = $srid instanceof Srid ? $srid->value : $srid;
114+
}
107115
}

src/Helper.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MatanYadaev\EloquentSpatial;
6+
7+
use MatanYadaev\EloquentSpatial\Enums\Srid;
8+
9+
class Helper
10+
{
11+
public static function getSrid(Srid|int|null $srid = null): int
12+
{
13+
if ($srid instanceof Srid) {
14+
return $srid->value;
15+
}
16+
17+
if (is_int($srid)) {
18+
return $srid;
19+
}
20+
21+
return EloquentSpatial::$defaultSrid;
22+
}
23+
}

src/Objects/Geometry.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,15 @@
2121
use MatanYadaev\EloquentSpatial\Factory;
2222
use MatanYadaev\EloquentSpatial\GeometryCast;
2323
use MatanYadaev\EloquentSpatial\GeometryExpression;
24+
use MatanYadaev\EloquentSpatial\Helper;
2425
use Stringable;
2526
use WKB as geoPHPWkb;
2627

2728
abstract class Geometry implements Arrayable, Castable, Jsonable, JsonSerializable, Stringable
2829
{
2930
use Macroable;
3031

31-
public int $srid = 0;
32+
public int $srid;
3233

3334
abstract public function toWkt(): string;
3435

@@ -90,10 +91,10 @@ public static function fromWkb(string $wkb): static
9091
/**
9192
* @throws InvalidArgumentException
9293
*/
93-
public static function fromWkt(string $wkt, int|Srid $srid = 0): static
94+
public static function fromWkt(string $wkt, int|Srid|null $srid = null): static
9495
{
9596
$geometry = Factory::parse($wkt);
96-
$geometry->srid = $srid instanceof Srid ? $srid->value : $srid;
97+
$geometry->srid = Helper::getSrid($srid);
9798

9899
if (! ($geometry instanceof static)) {
99100
throw new InvalidArgumentException(
@@ -107,10 +108,10 @@ public static function fromWkt(string $wkt, int|Srid $srid = 0): static
107108
/**
108109
* @throws InvalidArgumentException
109110
*/
110-
public static function fromJson(string $geoJson, int|Srid $srid = 0): static
111+
public static function fromJson(string $geoJson, int|Srid|null $srid = null): static
111112
{
112113
$geometry = Factory::parse($geoJson);
113-
$geometry->srid = $srid instanceof Srid ? $srid->value : $srid;
114+
$geometry->srid = Helper::getSrid($srid);
114115

115116
if (! ($geometry instanceof static)) {
116117
throw new InvalidArgumentException(
@@ -126,7 +127,7 @@ public static function fromJson(string $geoJson, int|Srid $srid = 0): static
126127
*
127128
* @throws JsonException
128129
*/
129-
public static function fromArray(array $geometry, int|Srid $srid = 0): static
130+
public static function fromArray(array $geometry, int|Srid|null $srid = null): static
130131
{
131132
$geoJson = json_encode($geometry, JSON_THROW_ON_ERROR);
132133

src/Objects/GeometryCollection.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Illuminate\Support\Str;
1010
use InvalidArgumentException;
1111
use MatanYadaev\EloquentSpatial\Enums\Srid;
12+
use MatanYadaev\EloquentSpatial\Helper;
1213

1314
class GeometryCollection extends Geometry implements ArrayAccess
1415
{
@@ -24,14 +25,14 @@ class GeometryCollection extends Geometry implements ArrayAccess
2425
*
2526
* @throws InvalidArgumentException
2627
*/
27-
public function __construct(Collection|array $geometries, int|Srid $srid = 0)
28+
public function __construct(Collection|array $geometries, int|Srid|null $srid = null)
2829
{
2930
if (is_array($geometries)) {
3031
$geometries = collect($geometries);
3132
}
3233

3334
$this->geometries = $geometries;
34-
$this->srid = $srid instanceof Srid ? $srid->value : $srid;
35+
$this->srid = Helper::getSrid($srid);
3536

3637
$this->validateGeometriesType();
3738
$this->validateGeometriesCount();

src/Objects/MultiLineString.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ class MultiLineString extends GeometryCollection
2626
*
2727
* @throws InvalidArgumentException
2828
*/
29-
public function __construct(Collection|array $geometries, int|Srid $srid = 0)
29+
public function __construct(Collection|array $geometries, int|Srid|null $srid = null)
3030
{
3131
// @phpstan-ignore-next-line
32-
parent::__construct($geometries, $this->srid = $srid instanceof Srid ? $srid->value : $srid);
32+
parent::__construct($geometries, $srid);
3333
}
3434

3535
public function toWkt(): string

src/Objects/MultiPolygon.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ class MultiPolygon extends GeometryCollection
2626
*
2727
* @throws InvalidArgumentException
2828
*/
29-
public function __construct(Collection|array $geometries, int|Srid $srid = 0)
29+
public function __construct(Collection|array $geometries, int|Srid|null $srid = null)
3030
{
3131
// @phpstan-ignore-next-line
32-
parent::__construct($geometries, $this->srid = $srid instanceof Srid ? $srid->value : $srid);
32+
parent::__construct($geometries, $srid);
3333
}
3434

3535
public function toWkt(): string

src/Objects/Point.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,19 @@
55
namespace MatanYadaev\EloquentSpatial\Objects;
66

77
use MatanYadaev\EloquentSpatial\Enums\Srid;
8+
use MatanYadaev\EloquentSpatial\Helper;
89

910
class Point extends Geometry
1011
{
1112
public float $latitude;
1213

1314
public float $longitude;
1415

15-
public function __construct(float $latitude, float $longitude, int|Srid $srid = 0)
16+
public function __construct(float $latitude, float $longitude, int|Srid|null $srid = null)
1617
{
1718
$this->latitude = $latitude;
1819
$this->longitude = $longitude;
19-
$this->srid = $srid instanceof Srid ? $srid->value : $srid;
20+
$this->srid = Helper::getSrid($srid);
2021
}
2122

2223
public function toWkt(): string

src/Objects/PointCollection.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ abstract class PointCollection extends GeometryCollection
2424
*
2525
* @throws InvalidArgumentException
2626
*/
27-
public function __construct(Collection|array $geometries, int|Srid $srid = 0)
27+
public function __construct(Collection|array $geometries, int|Srid|null $srid = null)
2828
{
2929
// @phpstan-ignore-next-line
30-
parent::__construct($geometries, $this->srid = $srid instanceof Srid ? $srid->value : $srid);
30+
parent::__construct($geometries, $srid);
3131
}
3232
}

0 commit comments

Comments
 (0)