Skip to content

Commit

Permalink
Merge pull request #10 from tarfin-labs/filtering-zero-points
Browse files Browse the repository at this point in the history
Zero locations excluded before filtering locations by distance.
  • Loading branch information
tkaratug committed Sep 5, 2022
2 parents 67239f3 + dc8b33d commit 1215c85
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

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

## 1.4.0 - 2022-07-24
- Locations that have zero points are excluded while getting the distance to a given location in `withingDistanceTo` scope.

## 1.3.0 - 2022-06-24
- `toWkt()` method added to `Point` class for getting the coordinates as well known text.
- `toPair()` method added to `Point` class for getting the coordinates as pair.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ The migration above creates an `addresses` table with a `location` spatial colum

> 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.
So you should give an SRID attribute to use spatial indexes in the migrations:
So you should give an SRID attribute to use spatial indexes in the migrations and indexed columns must be NOT NULL:

```php
Schema::create('addresses', function (Blueprint $table) {
Expand Down
6 changes: 5 additions & 1 deletion src/Traits/HasSpatial.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ public function scopeSelectDistanceTo(Builder $query, string $column, Point $poi

public function scopeWithinDistanceTo(Builder $query, string $column, Point $point, int $distance): void
{
$query->whereRaw(
$query
->whereRaw("ST_AsText({$column}) != ?", [
'POINT(0 0)',
])
->whereRaw(
"ST_Distance(ST_SRID({$column}, ?), ST_SRID(Point(?, ?), ?)) <= ?",
[
...[
Expand Down
2 changes: 1 addition & 1 deletion tests/HasSpatialTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function it_generates_sql_query_for_withinDistanceTo_scope(): void

// 3. Assert
$this->assertEquals(
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(?, ?), ?)) <= ?",
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(?, ?), ?)) <= ?",
actual: $query->toSql()
);
}
Expand Down

0 comments on commit 1215c85

Please sign in to comment.