From 480ac59dff0209a0a3e33c1ad964926a5f40d349 Mon Sep 17 00:00:00 2001 From: nihilsen <32091869+nihilsen@users.noreply.github.com> Date: Fri, 2 Oct 2020 21:16:50 +0000 Subject: [PATCH 1/2] Allow setting different WKT options. --- src/Eloquent/Builder.php | 23 ++++++++++- src/Eloquent/SpatialExpression.php | 25 +++++++++++- src/Eloquent/SpatialTrait.php | 63 +++++++++++++++++++++++++----- 3 files changed, 99 insertions(+), 12 deletions(-) diff --git a/src/Eloquent/Builder.php b/src/Eloquent/Builder.php index 6230ed17..f1148dd8 100755 --- a/src/Eloquent/Builder.php +++ b/src/Eloquent/Builder.php @@ -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) { @@ -20,6 +28,19 @@ 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; } } diff --git a/src/Eloquent/SpatialExpression.php b/src/Eloquent/SpatialExpression.php index 9224af0f..654b7d1c 100644 --- a/src/Eloquent/SpatialExpression.php +++ b/src/Eloquent/SpatialExpression.php @@ -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() @@ -20,4 +30,17 @@ 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; + } } diff --git a/src/Eloquent/SpatialTrait.php b/src/Eloquent/SpatialTrait.php index 5cc3f4b1..4ea92176 100755 --- a/src/Eloquent/SpatialTrait.php +++ b/src/Eloquent/SpatialTrait.php @@ -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. * @@ -67,7 +85,7 @@ trait SpatialTrait */ public function newEloquentBuilder($query) { - return new Builder($query); + return (new Builder($query))->withWktOptions($this->getWktOptionsValue()); } protected function newBaseQueryBuilder() @@ -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()); } } @@ -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())) { @@ -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, @@ -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(), ]); @@ -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(), ]); @@ -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, @@ -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(), ]); @@ -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(), ]); @@ -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(), ]); @@ -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(), ]); From 47ed99474ea9f8713b4a8150969bdcdb113397e9 Mon Sep 17 00:00:00 2001 From: nihilsen <32091869+nihilsen@users.noreply.github.com> Date: Fri, 2 Oct 2020 22:14:05 +0000 Subject: [PATCH 2/2] StyleCI fixes --- src/Eloquent/Builder.php | 3 ++- src/Eloquent/SpatialExpression.php | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Eloquent/Builder.php b/src/Eloquent/Builder.php index f1148dd8..a2d0e3ca 100755 --- a/src/Eloquent/Builder.php +++ b/src/Eloquent/Builder.php @@ -34,7 +34,8 @@ protected function asWKT(GeometryInterface $geometry) /** * Set the WKT options. * - * @param string $wktOptions + * @param string $wktOptions + * * @return self */ public function withWktOptions($wktOptions) diff --git a/src/Eloquent/SpatialExpression.php b/src/Eloquent/SpatialExpression.php index 654b7d1c..0540ad94 100644 --- a/src/Eloquent/SpatialExpression.php +++ b/src/Eloquent/SpatialExpression.php @@ -34,7 +34,8 @@ public function getSrid() /** * Set the WKT options. * - * @param string $wktOptions + * @param string $wktOptions + * * @return self */ public function withWktOptions($wktOptions)