Skip to content

Commit fe7d6bf

Browse files
committed
Merge branch 'srid'
2 parents 01caece + e056dc3 commit fe7d6bf

File tree

4 files changed

+72
-64
lines changed

4 files changed

+72
-64
lines changed

src/Eloquent/SpatialExpression.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class SpatialExpression extends Expression
88
{
99
public function getValue()
1010
{
11-
return 'ST_GeomFromText(?, ?)';
11+
return "ST_GeomFromText(?, ?, 'axis-order=long-lat')";
1212
}
1313

1414
public function getSpatialValue()

src/Eloquent/SpatialTrait.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,9 @@ public function scopeDistance($query, $geometryColumn, $geometry, $distance)
132132
{
133133
$this->isColumnAllowed($geometryColumn);
134134

135-
$query->whereRaw("st_distance(`$geometryColumn`, ST_GeomFromText(?)) <= ?", [
135+
$query->whereRaw("st_distance(`$geometryColumn`, ST_GeomFromText(?, ?, 'axis-order=long-lat')) <= ?", [
136136
$geometry->toWkt(),
137+
$geometry->getSrid(),
137138
$distance,
138139
]);
139140

@@ -146,8 +147,9 @@ public function scopeDistanceExcludingSelf($query, $geometryColumn, $geometry, $
146147

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

149-
$query->whereRaw("st_distance(`$geometryColumn`, ST_GeomFromText(?)) != 0", [
150+
$query->whereRaw("st_distance(`$geometryColumn`, ST_GeomFromText(?, ?, 'axis-order=long-lat')) != 0", [
150151
$geometry->toWkt(),
152+
$geometry->getSrid(),
151153
]);
152154

153155
return $query;
@@ -163,17 +165,19 @@ public function scopeDistanceValue($query, $geometryColumn, $geometry)
163165
$query->select('*');
164166
}
165167

166-
$query->selectRaw("st_distance(`$geometryColumn`, ST_GeomFromText(?)) as distance", [
168+
$query->selectRaw("st_distance(`$geometryColumn`, ST_GeomFromText(?, ?, 'axis-order=long-lat')) as distance", [
167169
$geometry->toWkt(),
170+
$geometry->getSrid(),
168171
]);
169172
}
170173

171174
public function scopeDistanceSphere($query, $geometryColumn, $geometry, $distance)
172175
{
173176
$this->isColumnAllowed($geometryColumn);
174177

175-
$query->whereRaw("st_distance_sphere(`$geometryColumn`, ST_GeomFromText(?)) <= ?", [
178+
$query->whereRaw("st_distance_sphere(`$geometryColumn`, ST_GeomFromText(?, ?, 'axis-order=long-lat')) <= ?", [
176179
$geometry->toWkt(),
180+
$geometry->getSrid(),
177181
$distance,
178182
]);
179183

@@ -186,8 +190,9 @@ public function scopeDistanceSphereExcludingSelf($query, $geometryColumn, $geome
186190

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

189-
$query->whereRaw("st_distance_sphere($geometryColumn, ST_GeomFromText(?)) != 0", [
193+
$query->whereRaw("st_distance_sphere($geometryColumn, ST_GeomFromText(?, ?, 'axis-order=long-lat')) != 0", [
190194
$geometry->toWkt(),
195+
$geometry->getSrid(),
191196
]);
192197

193198
return $query;
@@ -202,8 +207,9 @@ public function scopeDistanceSphereValue($query, $geometryColumn, $geometry)
202207
if (!$columns) {
203208
$query->select('*');
204209
}
205-
$query->selectRaw("st_distance_sphere(`$geometryColumn`, ST_GeomFromText(?)) as distance", [
210+
$query->selectRaw("st_distance_sphere(`$geometryColumn`, ST_GeomFromText(?, ?, 'axis-order=long-lat')) as distance", [
206211
$geometry->toWkt(),
212+
$geometry->getSrid(),
207213
]);
208214
}
209215

@@ -215,8 +221,9 @@ public function scopeComparison($query, $geometryColumn, $geometry, $relationshi
215221
throw new UnknownSpatialRelationFunction($relationship);
216222
}
217223

218-
$query->whereRaw("st_{$relationship}(`$geometryColumn`, ST_GeomFromText(?))", [
224+
$query->whereRaw("st_{$relationship}(`$geometryColumn`, ST_GeomFromText(?, ?, 'axis-order=long-lat'))", [
219225
$geometry->toWkt(),
226+
$geometry->getSrid(),
220227
]);
221228

222229
return $query;
@@ -270,8 +277,9 @@ public function scopeOrderBySpatial($query, $geometryColumn, $geometry, $orderFu
270277
throw new UnknownSpatialFunctionException($orderFunction);
271278
}
272279

273-
$query->orderByRaw("st_{$orderFunction}(`$geometryColumn`, ST_GeomFromText(?)) {$direction}", [
280+
$query->orderByRaw("st_{$orderFunction}(`$geometryColumn`, ST_GeomFromText(?, ?, 'axis-order=long-lat')) {$direction}", [
274281
$geometry->toWkt(),
282+
$geometry->getSrid(),
275283
]);
276284

277285
return $query;

tests/Integration/SridSpatialTest.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -113,22 +113,22 @@ public function testInsertPointWithWrongSrid()
113113
'of the geometry is 0, but the SRID of the column is 3857. ' .
114114
'Consider changing the SRID of the geometry or the SRID property ' .
115115
'of the column. (SQL: insert into `with_srid` (`location`) values ' .
116-
'(ST_GeomFromText(POINT(2 1), 0)))'
116+
'(ST_GeomFromText(POINT(2 1), 0, \'axis-order=long-lat\')))'
117117
);
118118
$geo->save();
119119
}
120120

121-
// public function testGeometryInsertedHasRightSrid () {
122-
// $geo = new WithSridModel();
123-
// $geo->location = new Point(1, 2, 3857);
124-
// $geo->save();
125-
//
126-
// $srid = \DB::selectOne('select ST_SRID(location) as srid from with_srid');
127-
// $this->assertEquals(3857, $srid->srid);
128-
//
129-
// $result = WithSridModel::first();
130-
//
131-
// $this->assertEquals($geo->location->getSrid(), $result->location->getSrid());
132-
// $a = 1;
133-
// }
121+
public function testGeometryInsertedHasRightSrid () {
122+
$geo = new WithSridModel();
123+
$geo->location = new Point(1, 2, 3857);
124+
$geo->save();
125+
126+
$srid = \DB::selectOne('select ST_SRID(location) as srid from with_srid');
127+
$this->assertEquals(3857, $srid->srid);
128+
129+
$result = WithSridModel::first();
130+
131+
$this->assertEquals($geo->location->getSrid(), $result->location->getSrid());
132+
$a = 1;
133+
}
134134
}

0 commit comments

Comments
 (0)