Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 37 additions & 37 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,20 @@ In addition, `GeometryCollection` also has these functions:
$geometryCollection = new GeometryCollection([
new Polygon([
new LineString([
new Point(180, 0),
new Point(179, 1),
new Point(178, 2),
new Point(177, 3),
new Point(180, 0),
new Point(0, 180),
new Point(1, 179),
new Point(2, 178),
new Point(3, 177),
new Point(0, 180),
]),
]),
new Point(180, 0),
new Point(0, 180),
]),
]);

echo $geometryCollection->getGeometries()[1]->latitude; // 180
echo $geometryCollection->getGeometries()[1]->latitude; // 0
// or access as an array:
echo $geometryCollection[1]->latitude; // 180
echo $geometryCollection[1]->latitude; // 0
```

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

```php
Place::create(['location' => new Point(0, 0)]);
Place::create(['location' => new Point(0, 0, 4326)]);

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

echo $placeWithDistance->distance; // 1.4142135623731
echo $placeWithDistance->distance; // 156897.79947260793

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

echo $placeWithDistance->distance_in_meters; // 1.4142135623731
echo $placeWithDistance->distance_in_meters; // 156897.79947260793
```
</details>

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

```php
Place::create(['location' => new Point(0, 0)]);
Place::create(['location' => new Point(100, 100)]);
Place::create(['location' => new Point(0, 0, 4326)]);
Place::create(['location' => new Point(50, 50, 4326)]);

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

echo $placesCountWithinDistance; // 1
Expand All @@ -136,15 +136,15 @@ Orders records by distance. Uses [ST_Distance](https://dev.mysql.com/doc/refman/
```php
Place::create([
'name' => 'first',
'location' => new Point(0, 0),
'location' => new Point(0, 0, 4326),
]);
Place::create([
'name' => 'second',
'location' => new Point(100, 100),
'location' => new Point(50, 50, 4326),
]);

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

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

```php
Place::create(['location' => new Point(0, 0)]);
Place::create(['location' => new Point(0, 0, 4326)]);

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

echo $placeWithDistance->distance; // 157249.0357231545
echo $placeWithDistance->distance; // 157249.59776850493

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

echo $placeWithDistance->distance_in_meters; // 157249.0357231545
echo $placeWithDistance->distance_in_meters; // 157249.59776850493
```
</details>

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

```php
Place::create(['location' => new Point(0, 0)]);
Place::create(['location' => new Point(100, 100)]);
Place::create(['location' => new Point(0, 0, 4326)]);
Place::create(['location' => new Point(50, 50, 4326)]);

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

echo $placesCountWithinDistance; // 1
Expand All @@ -222,15 +222,15 @@ Orders records by spherical distance. Uses [ST_Distance_Sphere](https://dev.mysq
```php
Place::create([
'name' => 'first',
'location' => new Point(0, 0),
'location' => new Point(0, 0, 4326),
]);
Place::create([
'name' => 'second',
'location' => new Point(100, 100),
'location' => new Point(100, 100, 4326),
]);

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

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

```php
Place::create(['location' => new Point(0, 0)]);
Place::create(['location' => new Point(0, 0, 4326)]);

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

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

```php
Place::create(['location' => new Point(0, 0)]);
Place::create(['location' => new Point(0, 0, 4326)]);

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

```php
Place::create(['location' => new Point(0, 0)]);
Place::create(['location' => new Point(0, 0, 4326)]);

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

```php
Place::create(['location' => new Point(0, 0)]);
Place::create(['location' => new Point(0, 0, 4326)]);

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

```php
Place::create(['location' => new Point(0, 0)]);
Place::create(['location' => new Point(0, 0, 4326)]);

Place::query()
->whereEquals('location', new Point(0, 0))
->whereEquals('location', new Point(0, 0, 4326))
->exists(); // true
```
</details>
Expand Down
6 changes: 3 additions & 3 deletions src/GeometryCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,19 @@ public function set($model, string $key, $value, array $attributes): Expression|

$wkt = $value->toWkt();

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

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

return $match[1];
}

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

return (int) $match[1];
}
Expand Down
2 changes: 1 addition & 1 deletion src/SpatialBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ protected function toExpression(Geometry|string $geometryOrColumn): Expression
if ($geometryOrColumn instanceof Geometry) {
$wkt = $geometryOrColumn->toWkt();

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

return DB::raw("`{$geometryOrColumn}`");
Expand Down
14 changes: 7 additions & 7 deletions tests/GeometryCastTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
});

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

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

Expand All @@ -38,7 +38,7 @@
});

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

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

Expand All @@ -62,7 +62,7 @@
});

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

Expand All @@ -78,8 +78,8 @@
expect(function (): void {
TestPlace::factory()->make([
'point' => new LineString([
new Point(180, 0),
new Point(179, 1),
new Point(0, 180),
new Point(1, 179),
]),
]);
})->toThrow(InvalidArgumentException::class);
Expand Down
Loading