Skip to content

Commit b197f2f

Browse files
committed
Changed scopeDistance() to scopeDistanceSphere(). Added scopeDistance, scopeDistanceValue, scopeDistanceSphereValue. Changed scopeBounding() to use st_intersects() instead of MBRIntersects(). Fixed code around dropping spatial index.
1 parent 03a5c43 commit b197f2f

File tree

5 files changed

+31
-27
lines changed

5 files changed

+31
-27
lines changed

src/Connectors/ConnectionFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class ConnectionFactory extends \Illuminate\Database\Connectors\ConnectionFactor
1818
protected function createConnection($driver, $connection, $database, $prefix = '', array $config = [])
1919
{
2020
if ($this->container->bound($key = "db.connection.{$driver}")) {
21-
return $this->container->make($key, [$connection, $database, $prefix, $config]);
21+
return $this->container->make($key, [$connection, $database, $prefix, $config]); // @codeCoverageIgnore
2222
}
2323

2424
if ($driver === 'mysql') {

src/Eloquent/Builder.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,6 @@ public function update(array $values)
1717
return parent::update($values);
1818
}
1919

20-
// protected function getSpatialFields()
21-
// {
22-
// return $this->getModel()->getSpatialFields();
23-
// }
24-
25-
2620
protected function asWKT(GeometryInterface $geometry)
2721
{
2822
return $this->getQuery()->raw("GeomFromText('" . $geometry->toWKT() . "')");

src/Eloquent/SpatialTrait.php

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function setRawAttributes(array $attributes, $sync = false)
6060
}
6161
}
6262

63-
parent::setRawAttributes($attributes, $sync);
63+
return parent::setRawAttributes($attributes, $sync);
6464
}
6565

6666
public function getSpatialFields()
@@ -73,7 +73,15 @@ public function getSpatialFields()
7373
}
7474

7575
public function scopeDistance($query, $distance, $geometry, $column_name, $exclude_self = false) {
76-
// TODO: what about mysql 5.5?
76+
$query->whereRaw("st_distance(`{$column_name}`, GeomFromText('{$geometry->toWkt()}')) <= {$distance}");
77+
78+
if($exclude_self) {
79+
$query->whereRaw("st_distance(`{$column_name}`, GeomFromText('{$geometry->toWkt()}')) != 0");
80+
}
81+
return $query;
82+
}
83+
84+
public function scopeDistanceSphere($query, $distance, $geometry, $column_name, $exclude_self = false) {
7785
$query->whereRaw("st_distance_sphere(`{$column_name}`, GeomFromText('{$geometry->toWkt()}')) <= {$distance}");
7886

7987
if($exclude_self) {
@@ -82,7 +90,25 @@ public function scopeDistance($query, $distance, $geometry, $column_name, $exclu
8290
return $query;
8391
}
8492

93+
public function scopeDistanceValue($query, $geometry, $column_name) {
94+
$columns = $query->getQuery()->columns;
95+
96+
if(!$columns) {
97+
$query->select('*');
98+
}
99+
$query->selectRaw("st_distance(`{$column_name}`, GeomFromText('{$geometry->toWkt()}')) as distance");
100+
}
101+
102+
public function scopeDistanceSphereValue($query, $geometry, $column_name) {
103+
$columns = $query->getQuery()->columns;
104+
105+
if(!$columns) {
106+
$query->select('*');
107+
}
108+
$query->selectRaw("st_distance_sphere(`{$column_name}`, GeomFromText('{$geometry->toWkt()}')) as distance");
109+
}
110+
85111
public function scopeBounding($query, Geometry $bounds, $column_name) {
86-
return $query->whereRaw("MBRIntersects(GeomFromText('{$bounds->toWkt()}'), `{$column_name}`)");
112+
return $query->whereRaw("st_intersects(GeomFromText('{$bounds->toWkt()}'), `{$column_name}`)");
87113
}
88114
}

src/Schema/Blueprint.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public function spatialIndex($columns, $name = null) {
108108
* @param string|array $index
109109
* @return \Illuminate\Support\Fluent
110110
*/
111-
public function dropSpatial($index)
111+
public function dropSpatialIndex($index)
112112
{
113113
return $this->dropIndexCommand('dropIndex', 'spatial', $index);
114114
}

src/Schema/Grammars/MySqlGrammar.php

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -105,20 +105,4 @@ public function compileSpatial(Blueprint $blueprint, Fluent $command)
105105
{
106106
return $this->compileKey($blueprint, $command, 'spatial');
107107
}
108-
109-
/**
110-
* Compile a drop index command.
111-
*
112-
* @param \Grimzy\LaravelSpatial\Schema\Blueprint $blueprint
113-
* @param \Illuminate\Support\Fluent $command
114-
* @return string
115-
*/
116-
public function compileDropSpatial(Blueprint $blueprint, Fluent $command)
117-
{
118-
$table = $this->wrapTable($blueprint);
119-
120-
$index = $this->wrap($command->index);
121-
122-
return "alter table {$table} drop index {$index}";
123-
}
124108
}

0 commit comments

Comments
 (0)