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
}

tests/Objects/GeometryCollectionTest.php

Lines changed: 98 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@
7272
expect($testPlace->geometry_collection->srid)->toBe(Srid::WGS84->value);
7373
});
7474

75-
it('creates geometry collection from JSON', function (): void {
75+
it('creates geometry collection with default 0 SRID from JSON', function (): void {
76+
// Arrange
77+
EloquentSpatial::setDefaultSrid(0);
7678
$geometryCollection = new GeometryCollection([
7779
new Polygon([
7880
new LineString([
@@ -86,9 +88,39 @@
8688
new Point(0, 180),
8789
]);
8890

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

94+
// Assert
9195
expect($geometryCollectionFromJson)->toEqual($geometryCollection);
96+
expect($geometryCollectionFromJson->srid)->toBe(0);
97+
});
98+
99+
it('creates geometry collection with default 4326 SRID from JSON', function (): void {
100+
// Arrange
101+
EloquentSpatial::setDefaultSrid(Srid::WGS84);
102+
$geometryCollection = new GeometryCollection([
103+
new Polygon([
104+
new LineString([
105+
new Point(0, 180),
106+
new Point(1, 179),
107+
new Point(2, 178),
108+
new Point(3, 177),
109+
new Point(0, 180),
110+
]),
111+
]),
112+
new Point(0, 180),
113+
]);
114+
115+
// Act
116+
$geometryCollectionFromJson = GeometryCollection::fromJson('{"type":"GeometryCollection","geometries":[{"type":"Polygon","coordinates":[[[180,0],[179,1],[178,2],[177,3],[180,0]]]},{"type":"Point","coordinates":[180,0]}]}');
117+
118+
// Assert
119+
expect($geometryCollectionFromJson->toWkt())->toBe($geometryCollection->toWkt());
120+
expect($geometryCollectionFromJson->srid)->toBe(Srid::WGS84->value);
121+
122+
// Cleanup
123+
EloquentSpatial::setDefaultSrid(0);
92124
});
93125

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

113-
it('creates geometry collection from array', function (): void {
145+
it('creates geometry collection with default 0 SRID from array', function (): void {
146+
// Arrange
147+
EloquentSpatial::setDefaultSrid(0);
114148
$geometryCollection = new GeometryCollection([
115149
new Polygon([
116150
new LineString([
@@ -124,9 +158,39 @@
124158
new Point(0, 180),
125159
]);
126160

161+
// Act
127162
$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));
128163

164+
// Assert
129165
expect($geometryCollectionFromJson)->toEqual($geometryCollection);
166+
expect($geometryCollectionFromJson->srid)->toBe(0);
167+
});
168+
169+
it('creates geometry collection with default 4326 SRID from array', function (): void {
170+
// Arrange
171+
EloquentSpatial::setDefaultSrid(Srid::WGS84);
172+
$geometryCollection = new GeometryCollection([
173+
new Polygon([
174+
new LineString([
175+
new Point(0, 180),
176+
new Point(1, 179),
177+
new Point(2, 178),
178+
new Point(3, 177),
179+
new Point(0, 180),
180+
]),
181+
]),
182+
new Point(0, 180),
183+
]);
184+
185+
// Act
186+
$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));
187+
188+
// Assert
189+
expect($geometryCollectionFromJson->toWkt())->toBe($geometryCollection->toWkt());
190+
expect($geometryCollectionFromJson->srid)->toBe(Srid::WGS84->value);
191+
192+
// Cleanup
193+
EloquentSpatial::setDefaultSrid(0);
130194
});
131195

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

267-
it('creates geometry collection from WKT', function (): void {
331+
it('creates geometry collection with default 0 SRID from WKT', function (): void {
332+
// Arrange
333+
EloquentSpatial::setDefaultSrid(0);
268334
$geometryCollection = new GeometryCollection([
269335
new Polygon([
270336
new LineString([
@@ -278,11 +344,40 @@
278344
new Point(0, 180),
279345
]);
280346

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

350+
// Assert
283351
expect($geometryCollectionFromWkt)->toEqual($geometryCollection);
284352
});
285353

354+
it('creates geometry collection with default 4326 SRID from WKT', function (): void {
355+
// Arrange
356+
EloquentSpatial::setDefaultSrid(Srid::WGS84);
357+
$geometryCollection = new GeometryCollection([
358+
new Polygon([
359+
new LineString([
360+
new Point(0, 180),
361+
new Point(1, 179),
362+
new Point(2, 178),
363+
new Point(3, 177),
364+
new Point(0, 180),
365+
]),
366+
]),
367+
new Point(0, 180),
368+
]);
369+
370+
// Act
371+
$geometryCollectionFromWkt = GeometryCollection::fromWkt('GEOMETRYCOLLECTION(POLYGON((180 0, 179 1, 178 2, 177 3, 180 0)), POINT(180 0))');
372+
373+
// Assert
374+
expect($geometryCollectionFromWkt->toWkt())->toBe($geometryCollection->toWkt());
375+
expect($geometryCollectionFromWkt->srid)->toBe(Srid::WGS84->value);
376+
377+
// Cleanup
378+
EloquentSpatial::setDefaultSrid(0);
379+
});
380+
286381
it('creates geometry collection with SRID from WKT', function (): void {
287382
$geometryCollection = new GeometryCollection([
288383
new Polygon([

0 commit comments

Comments
 (0)