Skip to content

Commit 6b603bc

Browse files
MatanYadaevMatan Yadaev
and
Matan Yadaev
authored
Fix SRID (#49)
* Fix tests to use valid latitude and longitude * Add test to identify invalid latitude or longitude * Fix docs to use correct latitude and longitude * Use SRID in API.md * Improve SpatialBuilder tests to use SRID * Fix the code to use constant axis order * Fix API.md Co-authored-by: Matan Yadaev <[email protected]>
1 parent 199f2b1 commit 6b603bc

13 files changed

+454
-438
lines changed

API.md

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,20 @@ In addition, `GeometryCollection` also has these functions:
3333
$geometryCollection = new GeometryCollection([
3434
new Polygon([
3535
new LineString([
36-
new Point(180, 0),
37-
new Point(179, 1),
38-
new Point(178, 2),
39-
new Point(177, 3),
40-
new Point(180, 0),
36+
new Point(0, 180),
37+
new Point(1, 179),
38+
new Point(2, 178),
39+
new Point(3, 177),
40+
new Point(0, 180),
4141
]),
4242
]),
43-
new Point(180, 0),
43+
new Point(0, 180),
4444
]),
4545
]);
4646

47-
echo $geometryCollection->getGeometries()[1]->latitude; // 180
47+
echo $geometryCollection->getGeometries()[1]->latitude; // 0
4848
// or access as an array:
49-
echo $geometryCollection[1]->latitude; // 180
49+
echo $geometryCollection[1]->latitude; // 0
5050
```
5151

5252
## Available spatial scopes
@@ -79,20 +79,20 @@ Retrieves the distance between 2 geometry objects. Uses [ST_Distance](https://de
7979
<details><summary>Example</summary>
8080

8181
```php
82-
Place::create(['location' => new Point(0, 0)]);
82+
Place::create(['location' => new Point(0, 0, 4326)]);
8383

8484
$placeWithDistance = Place::query()
85-
->withDistance('location', new Point(1, 1))
85+
->withDistance('location', new Point(1, 1, 4326))
8686
->first();
8787

88-
echo $placeWithDistance->distance; // 1.4142135623731
88+
echo $placeWithDistance->distance; // 156897.79947260793
8989

9090
// when using alias:
9191
$placeWithDistance = Place::query()
92-
->withDistance('location', new Point(1, 1), 'distance_in_meters')
92+
->withDistance('location', new Point(1, 1, 4326), 'distance_in_meters')
9393
->first();
9494

95-
echo $placeWithDistance->distance_in_meters; // 1.4142135623731
95+
echo $placeWithDistance->distance_in_meters; // 156897.79947260793
9696
```
9797
</details>
9898

@@ -110,11 +110,11 @@ Filters records by distance. Uses [ST_Distance](https://dev.mysql.com/doc/refman
110110
<details><summary>Example</summary>
111111

112112
```php
113-
Place::create(['location' => new Point(0, 0)]);
114-
Place::create(['location' => new Point(100, 100)]);
113+
Place::create(['location' => new Point(0, 0, 4326)]);
114+
Place::create(['location' => new Point(50, 50, 4326)]);
115115

116116
$placesCountWithinDistance = Place::query()
117-
->whereDistance('location', new Point(1, 1), '<', 1.5)
117+
->whereDistance('location', new Point(1, 1, 4326), '<', 160000)
118118
->count();
119119

120120
echo $placesCountWithinDistance; // 1
@@ -136,15 +136,15 @@ Orders records by distance. Uses [ST_Distance](https://dev.mysql.com/doc/refman/
136136
```php
137137
Place::create([
138138
'name' => 'first',
139-
'location' => new Point(0, 0),
139+
'location' => new Point(0, 0, 4326),
140140
]);
141141
Place::create([
142142
'name' => 'second',
143-
'location' => new Point(100, 100),
143+
'location' => new Point(50, 50, 4326),
144144
]);
145145

146146
$places = Place::query()
147-
->orderByDistance('location', new Point(1, 1), 'desc')
147+
->orderByDistance('location', new Point(1, 1, 4326), 'desc')
148148
->get();
149149

150150
echo $places[0]->name; // second
@@ -165,20 +165,20 @@ Retrieves the spherical distance between 2 geometry objects. Uses [ST_Distance_S
165165
<details><summary>Example</summary>
166166

167167
```php
168-
Place::create(['location' => new Point(0, 0)]);
168+
Place::create(['location' => new Point(0, 0, 4326)]);
169169

170170
$placeWithDistance = Place::query()
171-
->withDistanceSphere('location', new Point(1, 1))
171+
->withDistanceSphere('location', new Point(1, 1, 4326))
172172
->first();
173173

174-
echo $placeWithDistance->distance; // 157249.0357231545
174+
echo $placeWithDistance->distance; // 157249.59776850493
175175

176176
// when using alias:
177177
$placeWithDistance = Place::query()
178-
->withDistanceSphere('location', new Point(1, 1), 'distance_in_meters')
178+
->withDistanceSphere('location', new Point(1, 1, 4326), 'distance_in_meters')
179179
->first();
180180

181-
echo $placeWithDistance->distance_in_meters; // 157249.0357231545
181+
echo $placeWithDistance->distance_in_meters; // 157249.59776850493
182182
```
183183
</details>
184184

@@ -196,11 +196,11 @@ Filters records by spherical distance. Uses [ST_Distance_Sphere](https://dev.mys
196196
<details><summary>Example</summary>
197197

198198
```php
199-
Place::create(['location' => new Point(0, 0)]);
200-
Place::create(['location' => new Point(100, 100)]);
199+
Place::create(['location' => new Point(0, 0, 4326)]);
200+
Place::create(['location' => new Point(50, 50, 4326)]);
201201

202202
$placesCountWithinDistance = Place::query()
203-
->whereDistanceSphere('location', new Point(1, 1), '<', 160000)
203+
->whereDistanceSphere('location', new Point(1, 1, 4326), '<', 160000)
204204
->count();
205205

206206
echo $placesCountWithinDistance; // 1
@@ -222,15 +222,15 @@ Orders records by spherical distance. Uses [ST_Distance_Sphere](https://dev.mysq
222222
```php
223223
Place::create([
224224
'name' => 'first',
225-
'location' => new Point(0, 0),
225+
'location' => new Point(0, 0, 4326),
226226
]);
227227
Place::create([
228228
'name' => 'second',
229-
'location' => new Point(100, 100),
229+
'location' => new Point(100, 100, 4326),
230230
]);
231231

232232
$places = Place::query()
233-
->orderByDistanceSphere('location', new Point(1, 1), 'desc')
233+
->orderByDistanceSphere('location', new Point(1, 1, 4326), 'desc')
234234
->get();
235235

236236
echo $places[0]->name; // second
@@ -250,7 +250,7 @@ Filters records by the [ST_Within](https://dev.mysql.com/doc/refman/8.0/en/spati
250250
<details><summary>Example</summary>
251251

252252
```php
253-
Place::create(['location' => new Point(0, 0)]);
253+
Place::create(['location' => new Point(0, 0, 4326)]);
254254

255255
Place::query()
256256
->whereWithin('location', Polygon::fromJson('{"type":"Polygon","coordinates":[[[-1,-1],[1,-1],[1,1],[-1,1],[-1,-1]]]}'))
@@ -273,7 +273,7 @@ Filters records by the [ST_Contains](https://dev.mysql.com/doc/refman/8.0/en/spa
273273
Place::create(['area' => Polygon::fromJson('{"type":"Polygon","coordinates":[[[-1,-1],[1,-1],[1,1],[-1,1],[-1,-1]]]}'),]);
274274

275275
Place::query()
276-
->whereContains('area', new Point(0, 0))
276+
->whereContains('area', new Point(0, 0, 4326))
277277
->exists(); // true
278278
```
279279
</details>
@@ -290,7 +290,7 @@ Filters records by the [ST_Touches](https://dev.mysql.com/doc/refman/8.0/en/spat
290290
<details><summary>Example</summary>
291291

292292
```php
293-
Place::create(['location' => new Point(0, 0)]);
293+
Place::create(['location' => new Point(0, 0, 4326)]);
294294

295295
Place::query()
296296
->whereTouches('location', Polygon::fromJson('{"type":"Polygon","coordinates":[[[-1,-1],[0,-1],[0,0],[-1,0],[-1,-1]]]}'))
@@ -310,7 +310,7 @@ Filters records by the [ST_Intersects](https://dev.mysql.com/doc/refman/8.0/en/s
310310
<details><summary>Example</summary>
311311

312312
```php
313-
Place::create(['location' => new Point(0, 0)]);
313+
Place::create(['location' => new Point(0, 0, 4326)]);
314314

315315
Place::query()
316316
->whereIntersects('location', Polygon::fromJson('{"type":"Polygon","coordinates":[[[-1,-1],[1,-1],[1,1],[-1,1],[-1,-1]]]}'))
@@ -350,7 +350,7 @@ Filters records by the [ST_Disjoint](https://dev.mysql.com/doc/refman/8.0/en/spa
350350
<details><summary>Example</summary>
351351

352352
```php
353-
Place::create(['location' => new Point(0, 0)]);
353+
Place::create(['location' => new Point(0, 0, 4326)]);
354354

355355
Place::query()
356356
->whereDisjoint('location', Polygon::fromJson('{"type":"Polygon","coordinates":[[[-1,-1],[-0.5,-1],[-0.5,-0.5],[-1,-0.5],[-1,-1]]]}'))
@@ -370,10 +370,10 @@ Filters records by the [ST_Equal](https://dev.mysql.com/doc/refman/8.0/en/spatia
370370
<details><summary>Example</summary>
371371

372372
```php
373-
Place::create(['location' => new Point(0, 0)]);
373+
Place::create(['location' => new Point(0, 0, 4326)]);
374374

375375
Place::query()
376-
->whereEquals('location', new Point(0, 0))
376+
->whereEquals('location', new Point(0, 0, 4326))
377377
->exists(); // true
378378
```
379379
</details>

src/GeometryCast.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,19 +71,19 @@ public function set($model, string $key, $value, array $attributes): Expression|
7171

7272
$wkt = $value->toWkt();
7373

74-
return DB::raw("ST_GeomFromText('{$wkt}', {$value->srid})");
74+
return DB::raw("ST_GeomFromText('{$wkt}', {$value->srid}, 'axis-order=long-lat')");
7575
}
7676

7777
private function extractWktFromExpression(Expression $expression): string
7878
{
79-
preg_match('/ST_GeomFromText\(\'(.+)\', .+\)/', (string) $expression, $match);
79+
preg_match('/ST_GeomFromText\(\'(.+)\', .+, .+\)/', (string) $expression, $match);
8080

8181
return $match[1];
8282
}
8383

8484
private function extractSridFromExpression(Expression $expression): int
8585
{
86-
preg_match('/ST_GeomFromText\(\'.+\', (.+)\)/', (string) $expression, $match);
86+
preg_match('/ST_GeomFromText\(\'.+\', (.+), .+\)/', (string) $expression, $match);
8787

8888
return (int) $match[1];
8989
}

src/SpatialBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ protected function toExpression(Geometry|string $geometryOrColumn): Expression
265265
if ($geometryOrColumn instanceof Geometry) {
266266
$wkt = $geometryOrColumn->toWkt();
267267

268-
return DB::raw("ST_GeomFromText('{$wkt}')");
268+
return DB::raw("ST_GeomFromText('{$wkt}', {$geometryOrColumn->srid}, 'axis-order=long-lat')");
269269
}
270270

271271
return DB::raw("`{$geometryOrColumn}`");

tests/GeometryCastTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
});
1717

1818
it('updates a model record', function (): void {
19-
$point = new Point(180, 0);
19+
$point = new Point(0, 180);
2020
$point2 = new Point(0, 0);
2121
/** @var TestPlace $testPlace */
2222
$testPlace = TestPlace::factory()->create(['point' => $point]);
@@ -28,7 +28,7 @@
2828
});
2929

3030
it('updates a model record with null geometry', function (): void {
31-
$point = new Point(180, 0);
31+
$point = new Point(0, 180);
3232
/** @var TestPlace $testPlace */
3333
$testPlace = TestPlace::factory()->create(['point' => $point]);
3434

@@ -38,7 +38,7 @@
3838
});
3939

4040
it('gets original geometry field', function (): void {
41-
$point = new Point(180, 0, 4326);
41+
$point = new Point(0, 180, 4326);
4242
$point2 = new Point(0, 0, 4326);
4343
/** @var TestPlace $testPlace */
4444
$testPlace = TestPlace::factory()->create(['point' => $point]);
@@ -51,7 +51,7 @@
5151
});
5252

5353
it('serializes a model record to array with geometry', function (): void {
54-
$point = new Point(180, 0);
54+
$point = new Point(0, 180);
5555
/** @var TestPlace $testPlace */
5656
$testPlace = TestPlace::factory()->create(['point' => $point]);
5757

@@ -62,7 +62,7 @@
6262
});
6363

6464
it('serializes a model record to json with geometry', function (): void {
65-
$point = new Point(180, 0);
65+
$point = new Point(0, 180);
6666
/** @var TestPlace $testPlace */
6767
$testPlace = TestPlace::factory()->create(['point' => $point]);
6868

@@ -78,8 +78,8 @@
7878
expect(function (): void {
7979
TestPlace::factory()->make([
8080
'point' => new LineString([
81-
new Point(180, 0),
82-
new Point(179, 1),
81+
new Point(0, 180),
82+
new Point(1, 179),
8383
]),
8484
]);
8585
})->toThrow(InvalidArgumentException::class);

0 commit comments

Comments
 (0)