Skip to content

Commit 1215c85

Browse files
authored
Merge pull request #10 from tarfin-labs/filtering-zero-points
Zero locations excluded before filtering locations by distance.
2 parents 67239f3 + dc8b33d commit 1215c85

File tree

4 files changed

+10
-3
lines changed

4 files changed

+10
-3
lines changed

CHANGELOG.md

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

33
All notable changes to `laravel-spatial` will be documented in this file
44

5+
## 1.4.0 - 2022-07-24
6+
- Locations that have zero points are excluded while getting the distance to a given location in `withingDistanceTo` scope.
7+
58
## 1.3.0 - 2022-06-24
69
- `toWkt()` method added to `Point` class for getting the coordinates as well known text.
710
- `toPair()` method added to `Point` class for getting the coordinates as pair.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ The migration above creates an `addresses` table with a `location` spatial colum
5959

6060
> Spatial columns with no SRID attribute are not SRID-restricted and accept values with any SRID. However, the optimizer cannot use SPATIAL indexes on them until the column definition is modified to include an SRID attribute, which may require that the column contents first be modified so that all values have the same SRID.
6161
62-
So you should give an SRID attribute to use spatial indexes in the migrations:
62+
So you should give an SRID attribute to use spatial indexes in the migrations and indexed columns must be NOT NULL:
6363

6464
```php
6565
Schema::create('addresses', function (Blueprint $table) {

src/Traits/HasSpatial.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ public function scopeSelectDistanceTo(Builder $query, string $column, Point $poi
3131

3232
public function scopeWithinDistanceTo(Builder $query, string $column, Point $point, int $distance): void
3333
{
34-
$query->whereRaw(
34+
$query
35+
->whereRaw("ST_AsText({$column}) != ?", [
36+
'POINT(0 0)',
37+
])
38+
->whereRaw(
3539
"ST_Distance(ST_SRID({$column}, ?), ST_SRID(Point(?, ?), ?)) <= ?",
3640
[
3741
...[

tests/HasSpatialTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function it_generates_sql_query_for_withinDistanceTo_scope(): void
3636

3737
// 3. Assert
3838
$this->assertEquals(
39-
expected: "select *, CONCAT(ST_AsText(addresses.$castedAttr, 'axis-order=long-lat'), ',', ST_SRID(addresses.$castedAttr)) as $castedAttr from `addresses` where ST_Distance(ST_SRID($castedAttr, ?), ST_SRID(Point(?, ?), ?)) <= ?",
39+
expected: "select *, CONCAT(ST_AsText(addresses.$castedAttr, 'axis-order=long-lat'), ',', ST_SRID(addresses.$castedAttr)) as $castedAttr from `addresses` where ST_AsText(location) != ? and ST_Distance(ST_SRID($castedAttr, ?), ST_SRID(Point(?, ?), ?)) <= ?",
4040
actual: $query->toSql()
4141
);
4242
}

0 commit comments

Comments
 (0)