Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 480ac59

Browse files
committed
Allow setting different WKT options.
1 parent f70a82e commit 480ac59

File tree

3 files changed

+99
-12
lines changed

3 files changed

+99
-12
lines changed

‎src/Eloquent/Builder.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@
77

88
class Builder extends EloquentBuilder
99
{
10+
/**
11+
* The options to be passed to the ST_GeomFromText() function.
12+
* If set to false, the options argument will not be passed.
13+
*
14+
* @var string
15+
*/
16+
protected $wktOptions = 'axis-order=long-lat';
17+
1018
public function update(array $values)
1119
{
1220
foreach ($values as $key => &$value) {
@@ -20,6 +28,19 @@ public function update(array $values)
2028

2129
protected function asWKT(GeometryInterface $geometry)
2230
{
23-
return new SpatialExpression($geometry);
31+
return (new SpatialExpression($geometry))->withWktOptions($this->wktOptions);
32+
}
33+
34+
/**
35+
* Set the WKT options.
36+
*
37+
* @param string $wktOptions
38+
* @return self
39+
*/
40+
public function withWktOptions($wktOptions)
41+
{
42+
$this->wktOptions = $wktOptions;
43+
44+
return $this;
2445
}
2546
}

‎src/Eloquent/SpatialExpression.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,19 @@
66

77
class SpatialExpression extends Expression
88
{
9+
/**
10+
* The options to be passed to the ST_GeomFromText() function.
11+
* If set to false, the options argument will not be passed.
12+
*
13+
* @var string
14+
*/
15+
protected $wktOptions = 'axis-order=long-lat';
16+
917
public function getValue()
1018
{
11-
return "ST_GeomFromText(?, ?, 'axis-order=long-lat')";
19+
$thirdArgument = $this->wktOptions ? ", '$this->wktOptions'" : '';
20+
21+
return "ST_GeomFromText(?, ?$thirdArgument)";
1222
}
1323

1424
public function getSpatialValue()
@@ -20,4 +30,17 @@ public function getSrid()
2030
{
2131
return $this->value->getSrid();
2232
}
33+
34+
/**
35+
* Set the WKT options.
36+
*
37+
* @param string $wktOptions
38+
* @return self
39+
*/
40+
public function withWktOptions($wktOptions)
41+
{
42+
$this->wktOptions = $wktOptions;
43+
44+
return $this;
45+
}
2346
}

‎src/Eloquent/SpatialTrait.php

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /