Skip to content

Commit a197d13

Browse files
committed
Added $exclude_self param to distance scope. Added functional test for distance.
1 parent 321bf9e commit a197d13

File tree

3 files changed

+47
-11
lines changed

3 files changed

+47
-11
lines changed

src/Eloquent/SpatialTrait.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use Grimzy\LaravelSpatial\Exceptions\SpatialFieldsNotDefinedException;
55
use Grimzy\LaravelSpatial\Types\Geometry;
66
use Grimzy\LaravelSpatial\Types\GeometryInterface;
7+
use Grimzy\LaravelSpatial\Types\Point;
78
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
89

910
trait SpatialTrait
@@ -71,12 +72,14 @@ public function getSpatialFields()
7172
}
7273
}
7374

74-
public function scopeDistance($query, $distance, $point, $column_name) {
75-
// TODO: check if array, and transform to string delimited by ,
76-
// TODO: what about mysql 5.5? distance_sphere? need test
77-
return $query
78-
->whereRaw("st_distance_sphere(`{$column_name}`, POINT({$point->getLng()}, {$point->getLat()})) <= {$distance}")
79-
->whereRaw("st_distance_sphere(`{$column_name}`, POINT({$point->getLng()}, {$point->getLat()})) != 0");
75+
public function scopeDistance($query, $distance, $geometry, $column_name, $exclude_self = false) {
76+
// TODO: what about mysql 5.5?
77+
$query->whereRaw("st_distance_sphere(`{$column_name}`, GeomFromText('{$geometry->toWkt()}')) <= {$distance}");
78+
79+
if($exclude_self) {
80+
$query->whereRaw("st_distance_sphere(`{$column_name}`, GeomFromText('{$geometry->toWkt()}')) != 0");
81+
}
82+
return $query;
8083
}
8184

8285
public function scopeBounding($query, Geometry $bounds, $column_name) {

tests/Feature/SpatialTest.php

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,11 @@ public function tearDown()
5252

5353
protected function assertDatabaseHas($table, array $data, $connection = null)
5454
{
55-
if(method_exists($this, 'seeInDatabase')) {
55+
if (method_exists($this, 'seeInDatabase')) {
5656
$this->seeInDatabase($table, $data, $connection);
57+
} else {
58+
parent::assertDatabaseHas($table, $data, $connection);
5759
}
58-
parent::assertDatabaseHas($table, $data, $connection);
5960
}
6061

6162
private function onMigrations(\Closure $closure)
@@ -101,4 +102,37 @@ public function testUpdate()
101102
$this->assertEquals(2, $updated->location->getLat());
102103
$this->assertEquals(3, $updated->location->getLng());
103104
}
105+
106+
public function testDistance()
107+
{
108+
$loc1 = new GeometryModel();
109+
$loc1->location = new Point(40.767864, -73.971732);
110+
$loc1->save();
111+
112+
$loc2 = new GeometryModel();
113+
$loc2->location = new Point(40.767664, -73.971271); // Distance from loc1: 44.7414064845878
114+
$loc2->save();
115+
116+
$loc3 = new GeometryModel();
117+
$loc3->location = new Point(40.761434, -73.977619);
118+
$loc3->save();
119+
120+
$a = GeometryModel::distance(45, $loc1->location, 'location')->get();
121+
$this->assertCount(2, $a);
122+
$this->assertTrue($a->contains($loc1));
123+
$this->assertTrue($a->contains($loc2));
124+
$this->assertFalse($a->contains($loc3));
125+
126+
$b = GeometryModel::distance(45, $loc1->location, 'location', true)->get();
127+
$this->assertCount(1, $b);
128+
$this->assertFalse($b->contains($loc1));
129+
$this->assertTrue($b->contains($loc2));
130+
$this->assertFalse($b->contains($loc3));
131+
132+
$c = GeometryModel::distance(44.741406484587, $loc1->location, 'location')->get();
133+
$this->assertCount(1, $c);
134+
$this->assertTrue($c->contains($loc1));
135+
$this->assertFalse($c->contains($loc2));
136+
$this->assertFalse($c->contains($loc3));
137+
}
104138
}

tests/Unit/Eloquent/SpatialTraitTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,10 @@ class TestModel extends Model
5050
{
5151
use \Grimzy\LaravelSpatial\Eloquent\SpatialTrait;
5252

53-
protected $postgisFields = [
53+
protected $spatialFields = [
5454
'point' => Point::class
5555
];
5656

57-
5857
public static $pdo;
5958

6059
public static function resolveConnection($connection = null)
@@ -96,7 +95,7 @@ class TestPDO extends PDO
9695
public $queries = [];
9796
public $counter = 1;
9897

99-
public function prepare($statement, $driver_options = null)
98+
public function prepare($statement, $driver_options = array())
10099
{
101100
$this->queries[] = $statement;
102101

0 commit comments

Comments
 (0)