@@ -58,6 +58,24 @@ trait SpatialTrait
58
58
'distance_sphere ' ,
59
59
];
60
60
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
+
61
79
/**
62
80
* Create a new Eloquent query builder for the model.
63
81
*
@@ -67,7 +85,7 @@ trait SpatialTrait
67
85
*/
68
86
public function newEloquentBuilder ($ query )
69
87
{
70
- return new Builder ($ query );
88
+ return ( new Builder ($ query))-> withWktOptions ( $ this -> getWktOptionsValue () );
71
89
}
72
90
73
91
protected function newBaseQueryBuilder ()
@@ -86,7 +104,7 @@ protected function performInsert(EloquentBuilder $query, array $options = [])
86
104
foreach ($ this ->attributes as $ key => $ value ) {
87
105
if ($ value instanceof GeometryInterface) {
88
106
$ 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 () );
90
108
}
91
109
}
92
110
@@ -121,6 +139,31 @@ public function getSpatialFields()
121
139
}
122
140
}
123
141
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
+
124
167
public function isColumnAllowed ($ geometryColumn )
125
168
{
126
169
if (!in_array ($ geometryColumn , $ this ->getSpatialFields ())) {
@@ -134,7 +177,7 @@ public function scopeDistance($query, $geometryColumn, $geometry, $distance)
134
177
{
135
178
$ this ->isColumnAllowed ($ geometryColumn );
136
179
137
- $ query ->whereRaw ("st_distance(` $ geometryColumn`, ST_GeomFromText(?, ?, 'axis-order=long-lat' )) <= ? " , [
180
+ $ query ->whereRaw ("st_distance(` $ geometryColumn`, ST_GeomFromText(?, ? { $ this -> getWktOptions ()} )) <= ? " , [
138
181
$ geometry ->toWkt (),
139
182
$ geometry ->getSrid (),
140
183
$ distance ,
@@ -149,7 +192,7 @@ public function scopeDistanceExcludingSelf($query, $geometryColumn, $geometry, $
149
192
150
193
$ query = $ this ->scopeDistance ($ query , $ geometryColumn , $ geometry , $ distance );
151
194
152
- $ query ->whereRaw ("st_distance(` $ geometryColumn`, ST_GeomFromText(?, ?, 'axis-order=long-lat' )) != 0 " , [
195
+ $ query ->whereRaw ("st_distance(` $ geometryColumn`, ST_GeomFromText(?, ? { $ this -> getWktOptions ()} )) != 0 " , [
153
196
$ geometry ->toWkt (),
154
197
$ geometry ->getSrid (),
155
198
]);
@@ -167,7 +210,7 @@ public function scopeDistanceValue($query, $geometryColumn, $geometry)
167
210
$ query ->select ('* ' );
168
211
}
169
212
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 " , [
171
214
$ geometry ->toWkt (),
172
215
$ geometry ->getSrid (),
173
216
]);
@@ -177,7 +220,7 @@ public function scopeDistanceSphere($query, $geometryColumn, $geometry, $distanc
177
220
{
178
221
$ this ->isColumnAllowed ($ geometryColumn );
179
222
180
- $ query ->whereRaw ("st_distance_sphere(` $ geometryColumn`, ST_GeomFromText(?, ?, 'axis-order=long-lat' )) <= ? " , [
223
+ $ query ->whereRaw ("st_distance_sphere(` $ geometryColumn`, ST_GeomFromText(?, ? { $ this -> getWktOptions ()} )) <= ? " , [
181
224
$ geometry ->toWkt (),
182
225
$ geometry ->getSrid (),
183
226
$ distance ,
@@ -192,7 +235,7 @@ public function scopeDistanceSphereExcludingSelf($query, $geometryColumn, $geome
192
235
193
236
$ query = $ this ->scopeDistanceSphere ($ query , $ geometryColumn , $ geometry , $ distance );
194
237
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 " , [
196
239
$ geometry ->toWkt (),
197
240
$ geometry ->getSrid (),
198
241
]);
@@ -209,7 +252,7 @@ public function scopeDistanceSphereValue($query, $geometryColumn, $geometry)
209
252
if (!$ columns ) {
210
253
$ query ->select ('* ' );
211
254
}
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 " , [
213
256
$ geometry ->toWkt (),
214
257
$ geometry ->getSrid (),
215
258
]);
@@ -223,7 +266,7 @@ public function scopeComparison($query, $geometryColumn, $geometry, $relationshi
223
266
throw new UnknownSpatialRelationFunction ($ relationship );
224
267
}
225
268
226
- $ query ->whereRaw ("st_ {$ relationship }(` $ geometryColumn`, ST_GeomFromText(?, ?, 'axis-order=long-lat' )) " , [
269
+ $ query ->whereRaw ("st_ {$ relationship }(` $ geometryColumn`, ST_GeomFromText(?, ? { $ this -> getWktOptions ()} )) " , [
227
270
$ geometry ->toWkt (),
228
271
$ geometry ->getSrid (),
229
272
]);
@@ -279,7 +322,7 @@ public function scopeOrderBySpatial($query, $geometryColumn, $geometry, $orderFu
279
322
throw new UnknownSpatialFunctionException ($ orderFunction );
280
323
}
281
324
282
- $ query ->orderByRaw ("st_ {$ orderFunction }(` $ geometryColumn`, ST_GeomFromText(?, ?, 'axis-order=long-lat' )) {$ direction }" , [
325
+ $ query ->orderByRaw ("st_ {$ orderFunction }(` $ geometryColumn`, ST_GeomFromText(?, ? { $ this -> getWktOptions ()} )) {$ direction }" , [
283
326
$ geometry ->toWkt (),
284
327
$ geometry ->getSrid (),
285
328
]);
0 commit comments