Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow setting WKT options or disabling WKT options entirely. #152

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion src/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@

class Builder extends EloquentBuilder
{
/**
* The options to be passed to the ST_GeomFromText() function.
* If set to false, the options argument will not be passed.
*
* @var string
*/
protected $wktOptions = 'axis-order=long-lat';

public function update(array $values)
{
foreach ($values as $key => &$value) {
Expand All @@ -20,6 +28,20 @@ public function update(array $values)

protected function asWKT(GeometryInterface $geometry)
{
return new SpatialExpression($geometry);
return (new SpatialExpression($geometry))->withWktOptions($this->wktOptions);
}

/**
* Set the WKT options.
*
* @param string $wktOptions
*
* @return self
*/
public function withWktOptions($wktOptions)
{
$this->wktOptions = $wktOptions;

return $this;
}
}
26 changes: 25 additions & 1 deletion src/Eloquent/SpatialExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,19 @@

class SpatialExpression extends Expression
{
/**
* The options to be passed to the ST_GeomFromText() function.
* If set to false, the options argument will not be passed.
*
* @var string
*/
protected $wktOptions = 'axis-order=long-lat';

public function getValue()
{
return "ST_GeomFromText(?, ?, 'axis-order=long-lat')";
$thirdArgument = $this->wktOptions ? ", '$this->wktOptions'" : '';

return "ST_GeomFromText(?, ?$thirdArgument)";
}

public function getSpatialValue()
Expand All @@ -20,4 +30,18 @@ public function getSrid()
{
return $this->value->getSrid();
}

/**
* Set the WKT options.
*
* @param string $wktOptions
*
* @return self
*/
public function withWktOptions($wktOptions)
{
$this->wktOptions = $wktOptions;

return $this;
}
}
63 changes: 53 additions & 10 deletions src/Eloquent/SpatialTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,24 @@ trait SpatialTrait
'distance_sphere',
];

/**
* The options to be passed to the ST_GeomFromText() function.
* If not set, it defaults to 'axis-order=long-lat'. May be set to false
* to not pass the options parameter.
*
* @var string
*
* protected $wktOptions = 'axis-order=long-lat';
*/

/**
* The default options passed to the ST_GeomFromText() function if
* $wktOptions is not set.
*
* @var string
*/
protected $wktOptionsDefault = 'axis-order=long-lat';

/**
* Create a new Eloquent query builder for the model.
*
Expand All @@ -67,7 +85,7 @@ trait SpatialTrait
*/
public function newEloquentBuilder($query)
{
return new Builder($query);
return (new Builder($query))->withWktOptions($this->getWktOptionsValue());
}

protected function newBaseQueryBuilder()
Expand All @@ -86,7 +104,7 @@ protected function performInsert(EloquentBuilder $query, array $options = [])
foreach ($this->attributes as $key => $value) {
if ($value instanceof GeometryInterface) {
$this->geometries[$key] = $value; //Preserve the geometry objects prior to the insert
$this->attributes[$key] = new SpatialExpression($value);
$this->attributes[$key] = (new SpatialExpression($value))->withWktOptions($this->getWktOptionsValue());
}
}

Expand Down Expand Up @@ -121,6 +139,31 @@ public function getSpatialFields()
}
}

/**
* Get the options argument (with comma prepended) for use in
* ST_GeomFromText().
*
* @return string
*/
protected function getWktOptions()
{
if ($wktOptions = $this->getWktOptionsValue()) {
return ", '$wktOptions'";
}

return '';
}

/**
* Get the options value for use in ST_GeomFromText().
*
* @return string
*/
protected function getWktOptionsValue()
{
return $this->wktOptions ?? $this->wktOptionsDefault;
}

public function isColumnAllowed($geometryColumn)
{
if (!in_array($geometryColumn, $this->getSpatialFields())) {
Expand All @@ -134,7 +177,7 @@ public function scopeDistance($query, $geometryColumn, $geometry, $distance)
{
$this->isColumnAllowed($geometryColumn);

$query->whereRaw("st_distance(`$geometryColumn`, ST_GeomFromText(?, ?, 'axis-order=long-lat')) <= ?", [
$query->whereRaw("st_distance(`$geometryColumn`, ST_GeomFromText(?, ?{$this->getWktOptions()})) <= ?", [
$geometry->toWkt(),
$geometry->getSrid(),
$distance,
Expand All @@ -149,7 +192,7 @@ public function scopeDistanceExcludingSelf($query, $geometryColumn, $geometry, $

$query = $this->scopeDistance($query, $geometryColumn, $geometry, $distance);

$query->whereRaw("st_distance(`$geometryColumn`, ST_GeomFromText(?, ?, 'axis-order=long-lat')) != 0", [
$query->whereRaw("st_distance(`$geometryColumn`, ST_GeomFromText(?, ?{$this->getWktOptions()})) != 0", [
$geometry->toWkt(),
$geometry->getSrid(),
]);
Expand All @@ -167,7 +210,7 @@ public function scopeDistanceValue($query, $geometryColumn, $geometry)
$query->select('*');
}

$query->selectRaw("st_distance(`$geometryColumn`, ST_GeomFromText(?, ?, 'axis-order=long-lat')) as distance", [
$query->selectRaw("st_distance(`$geometryColumn`, ST_GeomFromText(?, ?{$this->getWktOptions()})) as distance", [
$geometry->toWkt(),
$geometry->getSrid(),
]);
Expand All @@ -177,7 +220,7 @@ public function scopeDistanceSphere($query, $geometryColumn, $geometry, $distanc
{
$this->isColumnAllowed($geometryColumn);

$query->whereRaw("st_distance_sphere(`$geometryColumn`, ST_GeomFromText(?, ?, 'axis-order=long-lat')) <= ?", [
$query->whereRaw("st_distance_sphere(`$geometryColumn`, ST_GeomFromText(?, ?{$this->getWktOptions()})) <= ?", [
$geometry->toWkt(),
$geometry->getSrid(),
$distance,
Expand All @@ -192,7 +235,7 @@ public function scopeDistanceSphereExcludingSelf($query, $geometryColumn, $geome

$query = $this->scopeDistanceSphere($query, $geometryColumn, $geometry, $distance);

$query->whereRaw("st_distance_sphere($geometryColumn, ST_GeomFromText(?, ?, 'axis-order=long-lat')) != 0", [
$query->whereRaw("st_distance_sphere($geometryColumn, ST_GeomFromText(?, ?{$this->getWktOptions()})) != 0", [
$geometry->toWkt(),
$geometry->getSrid(),
]);
Expand All @@ -209,7 +252,7 @@ public function scopeDistanceSphereValue($query, $geometryColumn, $geometry)
if (!$columns) {
$query->select('*');
}
$query->selectRaw("st_distance_sphere(`$geometryColumn`, ST_GeomFromText(?, ?, 'axis-order=long-lat')) as distance", [
$query->selectRaw("st_distance_sphere(`$geometryColumn`, ST_GeomFromText(?, ?{$this->getWktOptions()})) as distance", [
$geometry->toWkt(),
$geometry->getSrid(),
]);
Expand All @@ -223,7 +266,7 @@ public function scopeComparison($query, $geometryColumn, $geometry, $relationshi
throw new UnknownSpatialRelationFunction($relationship);
}

$query->whereRaw("st_{$relationship}(`$geometryColumn`, ST_GeomFromText(?, ?, 'axis-order=long-lat'))", [
$query->whereRaw("st_{$relationship}(`$geometryColumn`, ST_GeomFromText(?, ?{$this->getWktOptions()}))", [
$geometry->toWkt(),
$geometry->getSrid(),
]);
Expand Down Expand Up @@ -279,7 +322,7 @@ public function scopeOrderBySpatial($query, $geometryColumn, $geometry, $orderFu
throw new UnknownSpatialFunctionException($orderFunction);
}

$query->orderByRaw("st_{$orderFunction}(`$geometryColumn`, ST_GeomFromText(?, ?, 'axis-order=long-lat')) {$direction}", [
$query->orderByRaw("st_{$orderFunction}(`$geometryColumn`, ST_GeomFromText(?, ?{$this->getWktOptions()})) {$direction}", [
$geometry->toWkt(),
$geometry->getSrid(),
]);
Expand Down