Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
yassir3wad committed Oct 9, 2018
1 parent 74ecce0 commit 54f370a
Showing 1 changed file with 39 additions and 21 deletions.
60 changes: 39 additions & 21 deletions src/Postgis.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,25 @@ public function scopeWithDistance(Builder $query, Point $location = null)
$query->select([$classQuery->from . '.*']);
}

$division = 1;

if (property_exists(static::class, 'unit') && static::$unit == "mile") {
$division = 0.000621371;
} elseif (property_exists(static::class, 'unit') && static::$unit == "km") {
$division = 1000;
}

$longitude = $location->getLng() ?? null;
$latitude = $location->getLat() ?? null;
if ($location) {
$longitude = $location->getLng();
$latitude = $location->getLat();
$division = $this->getDivisionFactor();

$q = "ST_Distance({$this->getLocationColumn()},ST_GeomFromText('POINT({$longitude} {$latitude})',4326))/{$division}";
$q = "ST_Distance({$this->getLocationColumn()},ST_Point({$longitude},{$latitude}))/{$division}";
} else {
$q = "0";
}

return $query->selectSub($q, 'distance');
}

/**
* @param Builder $query
* @param Point $location
* @param float $inner_radius
* @param float $outer_radius
* @param $inner_radius
* @param $outer_radius
* @return Builder
*/
public function scopeWithGeofence(Builder $query, Point $location = null, $inner_radius = 0, $outer_radius = 0)
Expand All @@ -60,28 +58,48 @@ public function scopeWithGeofence(Builder $query, Point $location = null, $inner
* @param float $units
* @return Builder
*/
public function scopeWhereDistance(Builder $query, Point $location = null, $operator = '>', $units = 0)
public function scopeWhereDistance(Builder $query, Point $location, $operator, $units)
{
$classQuerry = $query->getQuery();
$classQuery = $query->getQuery();

if ($classQuerry && !$classQuerry->columns) {
$query->select([$classQuerry->from . '.*']);
if ($classQuery && !$classQuery->columns) {
$query->select([$classQuery->from . '.*']);
}

$longitude = $location->getLng() ?? null;
$latitude = $location->getLat() ?? null;
$longitude = $location ? $location->getLng() : null;
$latitude = $location ? $location->getLat() : null;

$q = "ST_Distance({$this->getLocationColumn()},ST_GeomFromText('POINT({$longitude} {$latitude})',4326))";
if ($longitude && $latitude) {
$q = "ST_Distance({$this->getLocationColumn()},ST_Point({$longitude},{$latitude}))";
} else {
$q = "0";
}

return $query->whereRaw("$q {$operator} {$units}");
}


public function getLocationColumn()
{
$column = defined('static::LOCATION') ? static::LOCATION : 'location';
$column = 'location';

if (property_exists($this, 'location') && $this->location) {
$column = $this->location;
}

return $this->getTable() . '.' . $column;
}

private function getDivisionFactor()
{
$division = 1;

if (property_exists($this, 'unit') && $this->unit == "mile") {
$division = 0.000621371;
} elseif (property_exists($this, 'unit') && $this->unit == "km") {
$division = 1000;
}

return $division;
}

}

0 comments on commit 54f370a

Please sign in to comment.