@@ -58,6 +58,24 @@ trait SpatialTrait
5858 'distance_sphere ' ,
5959 ];
6060
61+ /**
62+ * The options to be passed to the ST_GeomFromText() function.
63+ * If not set, it defaults to 'axis-order=long-lat'. May be set to false
64+ * to not pass the options parameter.
65+ *
66+ * @var string
67+ *
68+ * protected $wktOptions = 'axis-order=long-lat';
69+ */
70+
71+ /**
72+ * The default options passed to the ST_GeomFromText() function if
73+ * $wktOptions is not set.
74+ *
75+ * @var string
76+ */
77+ protected $ wktOptionsDefault = 'axis-order=long-lat ' ;
78+
6179 /**
6280 * Create a new Eloquent query builder for the model.
6381 *
@@ -67,7 +85,7 @@ trait SpatialTrait
6785 */
6886 public function newEloquentBuilder ($ query )
6987 {
70- return new Builder ($ query );
88+ return ( new Builder ($ query))-> withWktOptions ( $ this -> getWktOptionsValue () );
7189 }
7290
7391 protected function newBaseQueryBuilder ()
@@ -86,7 +104,7 @@ protected function performInsert(EloquentBuilder $query, array $options = [])
86104 foreach ($ this ->attributes as $ key => $ value ) {
87105 if ($ value instanceof GeometryInterface) {
88106 $ this ->geometries [$ key ] = $ value ; //Preserve the geometry objects prior to the insert
89- $ this ->attributes [$ key ] = new SpatialExpression ($ value );
107+ $ this ->attributes [$ key ] = ( new SpatialExpression ($ value))-> withWktOptions ( $ this -> getWktOptionsValue () );
90108 }
91109 }
92110
@@ -121,6 +139,31 @@ public function getSpatialFields()
121139 }
122140 }
123141
142+ /**
143+ * Get the options argument (with comma prepended) for use in
144+ * ST_GeomFromText().
145+ *
146+ * @return string
147+ */
148+ protected function getWktOptions ()
149+ {
150+ if ($ wktOptions = $ this ->getWktOptionsValue ()) {
151+ return ", ' $ wktOptions' " ;
152+ }
153+
154+ return '' ;
155+ }
156+
157+ /**
158+ * Get the options value for use in ST_GeomFromText().
159+ *
160+ * @return string
161+ */
162+ protected function getWktOptionsValue ()
163+ {
164+ return $ this ->wktOptions ?? $ this ->wktOptionsDefault ;
165+ }
166+
124167 public function isColumnAllowed ($ geometryColumn )
125168 {
126169 if (!in_array ($ geometryColumn , $ this ->getSpatialFields ())) {
@@ -134,7 +177,7 @@ public function scopeDistance($query, $geometryColumn, $geometry, $distance)
134177 {
135178 $ this ->isColumnAllowed ($ geometryColumn );
136179
137- $ query ->whereRaw ("st_distance(` $ geometryColumn`, ST_GeomFromText(?, ?, 'axis-order=long-lat' )) <= ? " , [
180+ $ query ->whereRaw ("st_distance(` $ geometryColumn`, ST_GeomFromText(?, ? { $ this -> getWktOptions ()} )) <= ? " , [
138181 $ geometry ->toWkt (),
139182 $ geometry ->getSrid (),
140183 $ distance ,
@@ -149,7 +192,7 @@ public function scopeDistanceExcludingSelf($query, $geometryColumn, $geometry, $
149192
150193 $ query = $ this ->scopeDistance ($ query , $ geometryColumn , $ geometry , $ distance );
151194
152- $ query ->whereRaw ("st_distance(` $ geometryColumn`, ST_GeomFromText(?, ?, 'axis-order=long-lat' )) != 0 " , [
195+ $ query ->whereRaw ("st_distance(` $ geometryColumn`, ST_GeomFromText(?, ? { $ this -> getWktOptions ()} )) != 0 " , [
153196 $ geometry ->toWkt (),
154197 $ geometry ->getSrid (),
155198 ]);
@@ -167,7 +210,7 @@ public function scopeDistanceValue($query, $geometryColumn, $geometry)
167210 $ query ->select ('* ' );
168211 }
169212
170- $ query ->selectRaw ("st_distance(` $ geometryColumn`, ST_GeomFromText(?, ?, 'axis-order=long-lat' )) as distance " , [
213+ $ query ->selectRaw ("st_distance(` $ geometryColumn`, ST_GeomFromText(?, ? { $ this -> getWktOptions ()} )) as distance " , [
171214 $ geometry ->toWkt (),
172215 $ geometry ->getSrid (),
173216 ]);
@@ -177,7 +220,7 @@ public function scopeDistanceSphere($query, $geometryColumn, $geometry, $distanc
177220 {
178221 $ this ->isColumnAllowed ($ geometryColumn );
179222
180- $ query ->whereRaw ("st_distance_sphere(` $ geometryColumn`, ST_GeomFromText(?, ?, 'axis-order=long-lat' )) <= ? " , [
223+ $ query ->whereRaw ("st_distance_sphere(` $ geometryColumn`, ST_GeomFromText(?, ? { $ this -> getWktOptions ()} )) <= ? " , [
181224 $ geometry ->toWkt (),
182225 $ geometry ->getSrid (),
183226 $ distance ,
@@ -192,7 +235,7 @@ public function scopeDistanceSphereExcludingSelf($query, $geometryColumn, $geome
192235
193236 $ query = $ this ->scopeDistanceSphere ($ query , $ geometryColumn , $ geometry , $ distance );
194237
195- $ query ->whereRaw ("st_distance_sphere( $ geometryColumn, ST_GeomFromText(?, ?, 'axis-order=long-lat' )) != 0 " , [
238+ $ query ->whereRaw ("st_distance_sphere( $ geometryColumn, ST_GeomFromText(?, ? { $ this -> getWktOptions ()} )) != 0 " , [
196239 $ geometry ->toWkt (),
197240 $ geometry ->getSrid (),
198241 ]);
@@ -209,7 +252,7 @@ public function scopeDistanceSphereValue($query, $geometryColumn, $geometry)
209252 if (!$ columns ) {
210253 $ query ->select ('* ' );
211254 }
212- $ query ->selectRaw ("st_distance_sphere(` $ geometryColumn`, ST_GeomFromText(?, ?, 'axis-order=long-lat' )) as distance " , [
255+ $ query ->selectRaw ("st_distance_sphere(` $ geometryColumn`, ST_GeomFromText(?, ? { $ this -> getWktOptions ()} )) as distance " , [
213256 $ geometry ->toWkt (),
214257 $ geometry ->getSrid (),
215258 ]);
@@ -223,7 +266,7 @@ public function scopeComparison($query, $geometryColumn, $geometry, $relationshi
223266 throw new UnknownSpatialRelationFunction ($ relationship );
224267 }
225268
226- $ query ->whereRaw ("st_ {$ relationship }(` $ geometryColumn`, ST_GeomFromText(?, ?, 'axis-order=long-lat' )) " , [
269+ $ query ->whereRaw ("st_ {$ relationship }(` $ geometryColumn`, ST_GeomFromText(?, ? { $ this -> getWktOptions ()} )) " , [
227270 $ geometry ->toWkt (),
228271 $ geometry ->getSrid (),
229272 ]);
@@ -279,7 +322,7 @@ public function scopeOrderBySpatial($query, $geometryColumn, $geometry, $orderFu
279322 throw new UnknownSpatialFunctionException ($ orderFunction );
280323 }
281324
282- $ query ->orderByRaw ("st_ {$ orderFunction }(` $ geometryColumn`, ST_GeomFromText(?, ?, 'axis-order=long-lat' )) {$ direction }" , [
325+ $ query ->orderByRaw ("st_ {$ orderFunction }(` $ geometryColumn`, ST_GeomFromText(?, ? { $ this -> getWktOptions ()} )) {$ direction }" , [
283326 $ geometry ->toWkt (),
284327 $ geometry ->getSrid (),
285328 ]);
0 commit comments