Skip to content

Commit 5b039a5

Browse files
authored
Add SRID Enum (#84)
* Add SRID Enum * update enum definitions * Change namespace * Rename to Srid * Remove doc block
1 parent b9f6c6d commit 5b039a5

13 files changed

+163
-132
lines changed

API.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,17 @@ echo $geometryCollection->getGeometries()[1]->latitude; // 0
5050
echo $geometryCollection[1]->latitude; // 0
5151
```
5252

53+
## Available Enums
54+
55+
Spatial reference identifiers (SRID) identify the type of coordinate system to use.
56+
57+
An enum is provided with the following values:
58+
59+
| Identifier | Value | Description |
60+
|--------------------|-------|-------------------------------------------------------------------------------------|
61+
| SRID::WGS84 | 4326 | [Geographic coordinate system](https://epsg.org/crs_4326/WGS-84.html) |
62+
| SRID::WEB_MERCATOR | 3857 | [Mercator coordinate system](https://epsg.org/crs_3857/WGS-84-Pseudo-Mercator.html) |
63+
5364
## Available spatial scopes
5465

5566
* [withDistance](#withDistance)

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ use App\Models\Place;
9898
use MatanYadaev\EloquentSpatial\Objects\Polygon;
9999
use MatanYadaev\EloquentSpatial\Objects\LineString;
100100
use MatanYadaev\EloquentSpatial\Objects\Point;
101+
use MatanYadaev\EloquentSpatial\Enums\Srid;
101102

102103
$londonEye = Place::create([
103104
'name' => 'London Eye',
@@ -106,7 +107,7 @@ $londonEye = Place::create([
106107

107108
$whiteHouse = Place::create([
108109
'name' => 'White House',
109-
'location' => new Point(38.8976763, -77.0365298, 4326), // with SRID
110+
'location' => new Point(38.8976763, -77.0365298, Srid::WGS84->value), // with SRID
110111
]);
111112

112113
$vaticanCity = Place::create([

src/Enums/Srid.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace MatanYadaev\EloquentSpatial\Enums;
4+
5+
enum Srid: int
6+
{
7+
case WGS84 = 4326; // https://epsg.org/crs_4326/WGS-84.html
8+
case WEB_MERCATOR = 3857; //https://epsg.org/crs_3857/WGS-84-Pseudo-Mercator.html
9+
}

tests/GeometryCastTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use Illuminate\Foundation\Testing\DatabaseMigrations;
44
use Illuminate\Support\Facades\DB;
5+
use MatanYadaev\EloquentSpatial\Enums\Srid;
56
use MatanYadaev\EloquentSpatial\Objects\LineString;
67
use MatanYadaev\EloquentSpatial\Objects\Point;
78
use MatanYadaev\EloquentSpatial\Tests\TestModels\TestPlace;
@@ -50,8 +51,8 @@
5051
});
5152

5253
it('gets original geometry field', function (): void {
53-
$point = new Point(0, 180, 4326);
54-
$point2 = new Point(0, 0, 4326);
54+
$point = new Point(0, 180, Srid::WGS84->value);
55+
$point2 = new Point(0, 0, Srid::WGS84->value);
5556
/** @var TestPlace $testPlace */
5657
$testPlace = TestPlace::factory()->create(['point' => $point]);
5758

tests/Objects/GeometryCollectionTest.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use Illuminate\Foundation\Testing\DatabaseMigrations;
4+
use MatanYadaev\EloquentSpatial\Enums\Srid;
45
use MatanYadaev\EloquentSpatial\Objects\Geometry;
56
use MatanYadaev\EloquentSpatial\Objects\GeometryCollection;
67
use MatanYadaev\EloquentSpatial\Objects\LineString;
@@ -43,12 +44,12 @@
4344
]),
4445
]),
4546
new Point(0, 180),
46-
], 4326);
47+
], Srid::WGS84->value);
4748

4849
/** @var TestPlace $testPlace */
4950
$testPlace = TestPlace::factory()->create(['geometry_collection' => $geometryCollection]);
5051

51-
expect($testPlace->geometry_collection->srid)->toBe(4326);
52+
expect($testPlace->geometry_collection->srid)->toBe(Srid::WGS84->value);
5253
});
5354

5455
it('creates geometry collection from JSON', function (): void {
@@ -82,9 +83,9 @@
8283
]),
8384
]),
8485
new Point(0, 180),
85-
], 4326);
86+
], Srid::WGS84->value);
8687

87-
$geometryCollectionFromJson = GeometryCollection::fromJson('{"type":"GeometryCollection","geometries":[{"type":"Polygon","coordinates":[[[180,0],[179,1],[178,2],[177,3],[180,0]]]},{"type":"Point","coordinates":[180,0]}]}', 4326);
88+
$geometryCollectionFromJson = GeometryCollection::fromJson('{"type":"GeometryCollection","geometries":[{"type":"Polygon","coordinates":[[[180,0],[179,1],[178,2],[177,3],[180,0]]]},{"type":"Point","coordinates":[180,0]}]}', Srid::WGS84->value);
8889

8990
expect($geometryCollectionFromJson)->toEqual($geometryCollection);
9091
});
@@ -179,9 +180,9 @@
179180
]),
180181
]),
181182
new Point(0, 180),
182-
], 4326);
183+
], Srid::WGS84->value);
183184

184-
$geometryCollectionFromWkt = GeometryCollection::fromWkt('GEOMETRYCOLLECTION(POLYGON((180 0, 179 1, 178 2, 177 3, 180 0)), POINT(180 0))', 4326);
185+
$geometryCollectionFromWkt = GeometryCollection::fromWkt('GEOMETRYCOLLECTION(POLYGON((180 0, 179 1, 178 2, 177 3, 180 0)), POINT(180 0))', Srid::WGS84->value);
185186

186187
expect($geometryCollectionFromWkt)->toEqual($geometryCollection);
187188
});
@@ -237,7 +238,7 @@
237238
]),
238239
]),
239240
new Point(0, 180),
240-
], 4326);
241+
], Srid::WGS84->value);
241242

242243
$geometryCollectionFromWkb = GeometryCollection::fromWkb($geometryCollection->toWkb());
243244

tests/Objects/GeometryTest.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use Illuminate\Database\QueryException;
44
use Illuminate\Support\Facades\DB;
55
use MatanYadaev\EloquentSpatial\AxisOrder;
6+
use MatanYadaev\EloquentSpatial\Enums\Srid;
67
use MatanYadaev\EloquentSpatial\Objects\Geometry;
78
use MatanYadaev\EloquentSpatial\Objects\LineString;
89
use MatanYadaev\EloquentSpatial\Objects\Point;
@@ -18,36 +19,36 @@
1819

1920
it('throws exception when generating geometry with invalid latitude', function (): void {
2021
expect(function (): void {
21-
$point = (new Point(91, 0, 4326));
22+
$point = (new Point(91, 0, Srid::WGS84->value));
2223
TestPlace::factory()->create(['point' => $point]);
2324
})->toThrow(QueryException::class);
2425
})->skip(fn () => ! (new AxisOrder)->supported(DB::connection()));
2526

2627
it('throws exception when generating geometry with invalid latitude - without axis-order', function (): void {
2728
expect(function (): void {
28-
$point = (new Point(91, 0, 4326));
29+
$point = (new Point(91, 0, Srid::WGS84->value));
2930
TestPlace::factory()->create(['point' => $point]);
3031

3132
TestPlace::query()
32-
->withDistanceSphere('point', new Point(1, 1, 4326))
33+
->withDistanceSphere('point', new Point(1, 1, Srid::WGS84->value))
3334
->firstOrFail();
3435
})->toThrow(QueryException::class);
3536
})->skip(fn () => (new AxisOrder)->supported(DB::connection()));
3637

3738
it('throws exception when generating geometry with invalid longitude', function (): void {
3839
expect(function (): void {
39-
$point = (new Point(0, 181, 4326));
40+
$point = (new Point(0, 181, Srid::WGS84->value));
4041
TestPlace::factory()->create(['point' => $point]);
4142
})->toThrow(QueryException::class);
4243
})->skip(fn () => ! (new AxisOrder)->supported(DB::connection()));
4344

4445
it('throws exception when generating geometry with invalid longitude - without axis-order', function (): void {
4546
expect(function (): void {
46-
$point = (new Point(0, 181, 4326));
47+
$point = (new Point(0, 181, Srid::WGS84->value));
4748
TestPlace::factory()->create(['point' => $point]);
4849

4950
TestPlace::query()
50-
->withDistanceSphere('point', new Point(1, 1, 4326))
51+
->withDistanceSphere('point', new Point(1, 1, Srid::WGS84->value))
5152
->firstOrFail();
5253
})->toThrow(QueryException::class);
5354
})->skip(fn () => (new AxisOrder)->supported(DB::connection()));
@@ -81,7 +82,7 @@
8182
});
8283

8384
it('creates an SQL expression from a geometry', function (): void {
84-
$point = new Point(0, 180, 4326);
85+
$point = new Point(0, 180, Srid::WGS84->value);
8586

8687
$expression = $point->toSqlExpression(DB::connection());
8788

@@ -91,7 +92,7 @@
9192
})->skip(fn () => ! (new AxisOrder)->supported(DB::connection()));
9293

9394
it('creates an SQL expression from a geometry - without axis-order', function (): void {
94-
$point = new Point(0, 180, 4326);
95+
$point = new Point(0, 180, Srid::WGS84->value);
9596

9697
$expression = $point->toSqlExpression(DB::connection());
9798

tests/Objects/LineStringTest.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use Illuminate\Foundation\Testing\DatabaseMigrations;
4+
use MatanYadaev\EloquentSpatial\Enums\Srid;
45
use MatanYadaev\EloquentSpatial\Objects\Geometry;
56
use MatanYadaev\EloquentSpatial\Objects\LineString;
67
use MatanYadaev\EloquentSpatial\Objects\Point;
@@ -26,12 +27,12 @@
2627
$lineString = new LineString([
2728
new Point(0, 180),
2829
new Point(1, 179),
29-
], 4326);
30+
], Srid::WGS84->value);
3031

3132
/** @var TestPlace $testPlace */
3233
$testPlace = TestPlace::factory()->create(['line_string' => $lineString]);
3334

34-
expect($testPlace->line_string->srid)->toBe(4326);
35+
expect($testPlace->line_string->srid)->toBe(Srid::WGS84->value);
3536
});
3637

3738
it('creates line string from JSON', function (): void {
@@ -49,9 +50,9 @@
4950
$lineString = new LineString([
5051
new Point(0, 180),
5152
new Point(1, 179),
52-
], 4326);
53+
], Srid::WGS84->value);
5354

54-
$lineStringFromJson = LineString::fromJson('{"type":"LineString","coordinates":[[180,0],[179,1]]}', 4326);
55+
$lineStringFromJson = LineString::fromJson('{"type":"LineString","coordinates":[[180,0],[179,1]]}', Srid::WGS84->value);
5556

5657
expect($lineStringFromJson)->toEqual($lineString);
5758
});
@@ -95,9 +96,9 @@
9596
$lineString = new LineString([
9697
new Point(0, 180),
9798
new Point(1, 179),
98-
], 4326);
99+
], Srid::WGS84->value);
99100

100-
$lineStringFromWkt = LineString::fromWkt('LINESTRING(180 0, 179 1)', 4326);
101+
$lineStringFromWkt = LineString::fromWkt('LINESTRING(180 0, 179 1)', Srid::WGS84->value);
101102

102103
expect($lineStringFromWkt)->toEqual($lineString);
103104
});
@@ -129,7 +130,7 @@
129130
$lineString = new LineString([
130131
new Point(0, 180),
131132
new Point(1, 179),
132-
], 4326);
133+
], Srid::WGS84->value);
133134

134135
$lineStringFromWkb = LineString::fromWkb($lineString->toWkb());
135136

tests/Objects/MultiLineStringTest.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use Illuminate\Foundation\Testing\DatabaseMigrations;
4+
use MatanYadaev\EloquentSpatial\Enums\Srid;
45
use MatanYadaev\EloquentSpatial\Objects\Geometry;
56
use MatanYadaev\EloquentSpatial\Objects\LineString;
67
use MatanYadaev\EloquentSpatial\Objects\MultiLineString;
@@ -30,12 +31,12 @@
3031
new Point(0, 180),
3132
new Point(1, 179),
3233
]),
33-
], 4326);
34+
], Srid::WGS84->value);
3435

3536
/** @var TestPlace $testPlace */
3637
$testPlace = TestPlace::factory()->create(['multi_line_string' => $multiLineString]);
3738

38-
expect($testPlace->multi_line_string->srid)->toBe(4326);
39+
expect($testPlace->multi_line_string->srid)->toBe(Srid::WGS84->value);
3940
});
4041

4142
it('creates multi line string from JSON', function (): void {
@@ -57,9 +58,9 @@
5758
new Point(0, 180),
5859
new Point(1, 179),
5960
]),
60-
], 4326);
61+
], Srid::WGS84->value);
6162

62-
$multiLineStringFromJson = MultiLineString::fromJson('{"type":"MultiLineString","coordinates":[[[180,0],[179,1]]]}', 4326);
63+
$multiLineStringFromJson = MultiLineString::fromJson('{"type":"MultiLineString","coordinates":[[[180,0],[179,1]]]}', Srid::WGS84->value);
6364

6465
expect($multiLineStringFromJson)->toEqual($multiLineString);
6566
});
@@ -111,9 +112,9 @@
111112
new Point(0, 180),
112113
new Point(1, 179),
113114
]),
114-
], 4326);
115+
], Srid::WGS84->value);
115116

116-
$multiLineStringFromWkt = MultiLineString::fromWkt('MULTILINESTRING((180 0, 179 1))', 4326);
117+
$multiLineStringFromWkt = MultiLineString::fromWkt('MULTILINESTRING((180 0, 179 1))', Srid::WGS84->value);
117118

118119
expect($multiLineStringFromWkt)->toEqual($multiLineString);
119120
});
@@ -151,7 +152,7 @@
151152
new Point(0, 180),
152153
new Point(1, 179),
153154
]),
154-
], 4326);
155+
], Srid::WGS84->value);
155156

156157
$multiLineStringFromWkb = MultiLineString::fromWkb($multiLineString->toWkb());
157158

tests/Objects/MultiPointTest.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use Illuminate\Foundation\Testing\DatabaseMigrations;
4+
use MatanYadaev\EloquentSpatial\Enums\Srid;
45
use MatanYadaev\EloquentSpatial\Objects\Geometry;
56
use MatanYadaev\EloquentSpatial\Objects\MultiPoint;
67
use MatanYadaev\EloquentSpatial\Objects\Point;
@@ -24,12 +25,12 @@
2425
it('creates a model record with multi point with SRID', function (): void {
2526
$multiPoint = new MultiPoint([
2627
new Point(0, 180),
27-
], 4326);
28+
], Srid::WGS84->value);
2829

2930
/** @var TestPlace $testPlace */
3031
$testPlace = TestPlace::factory()->create(['multi_point' => $multiPoint]);
3132

32-
expect($testPlace->multi_point->srid)->toBe(4326);
33+
expect($testPlace->multi_point->srid)->toBe(Srid::WGS84->value);
3334
});
3435

3536
it('creates multi point from JSON', function (): void {
@@ -45,9 +46,9 @@
4546
it('creates multi point with SRID from JSON', function (): void {
4647
$multiPoint = new MultiPoint([
4748
new Point(0, 180),
48-
], 4326);
49+
], Srid::WGS84->value);
4950

50-
$multiPointFromJson = MultiPoint::fromJson('{"type":"MultiPoint","coordinates":[[180,0]]}', 4326);
51+
$multiPointFromJson = MultiPoint::fromJson('{"type":"MultiPoint","coordinates":[[180,0]]}', Srid::WGS84->value);
5152

5253
expect($multiPointFromJson)->toEqual($multiPoint);
5354
});
@@ -87,9 +88,9 @@
8788
it('creates multi point with SRID from WKT', function (): void {
8889
$multiPoint = new MultiPoint([
8990
new Point(0, 180),
90-
], 4326);
91+
], Srid::WGS84->value);
9192

92-
$multiPointFromWkt = MultiPoint::fromWkt('MULTIPOINT(180 0)', 4326);
93+
$multiPointFromWkt = MultiPoint::fromWkt('MULTIPOINT(180 0)', Srid::WGS84->value);
9394

9495
expect($multiPointFromWkt)->toEqual($multiPoint);
9596
});
@@ -118,7 +119,7 @@
118119
it('creates multi point with SRID from WKB', function (): void {
119120
$multiPoint = new MultiPoint([
120121
new Point(0, 180),
121-
], 4326);
122+
], Srid::WGS84->value);
122123

123124
$multiPointFromWkb = MultiPoint::fromWkb($multiPoint->toWkb());
124125

0 commit comments

Comments
 (0)