Skip to content

Commit 0c9f864

Browse files
updated tests
1 parent cbabdbf commit 0c9f864

3 files changed

Lines changed: 48 additions & 22 deletions

File tree

src/Database/Adapter/MariaDB.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1396,9 +1396,9 @@ protected function handleDistanceSpatialQueries(Query $query, array &$binds, str
13961396
if ($wktType != Database::VAR_POINT || $attrType != Database::VAR_POINT) {
13971397
throw new QueryException('Distance in meters is not supported between '.$attrType . ' and '. $wktType);
13981398
}
1399-
return "ST_DISTANCE_SPHERE({$alias}.{$attribute}, " . $this->getSpatialGeomFromText(":{$placeholder}_0") . ", " . Database::EARTH_RADIUS . ") {$operator} :{$placeholder}_1";
1399+
return "ST_DISTANCE_SPHERE({$alias}.{$attribute}, " . $this->getSpatialGeomFromText(":{$placeholder}_0", null) . ", " . Database::EARTH_RADIUS . ") {$operator} :{$placeholder}_1";
14001400
}
1401-
return "ST_Distance({$alias}.{$attribute}, " . $this->getSpatialGeomFromText(":{$placeholder}_0") . ") {$operator} :{$placeholder}_1";
1401+
return "ST_Distance({$alias}.{$attribute}, " . $this->getSpatialGeomFromText(":{$placeholder}_0", null) . ") {$operator} :{$placeholder}_1";
14021402
}
14031403

14041404
/**
@@ -1417,11 +1417,11 @@ protected function handleSpatialQueries(Query $query, array &$binds, string $att
14171417
switch ($query->getMethod()) {
14181418
case Query::TYPE_CROSSES:
14191419
$binds[":{$placeholder}_0"] = $this->convertArrayToWKT($query->getValues()[0]);
1420-
return "ST_Crosses({$alias}.{$attribute}, " . $this->getSpatialGeomFromText(":{$placeholder}_0") . ")";
1420+
return "ST_Crosses({$alias}.{$attribute}, " . $this->getSpatialGeomFromText(":{$placeholder}_0", null) . ")";
14211421

14221422
case Query::TYPE_NOT_CROSSES:
14231423
$binds[":{$placeholder}_0"] = $this->convertArrayToWKT($query->getValues()[0]);
1424-
return "NOT ST_Crosses({$alias}.{$attribute}, " . $this->getSpatialGeomFromText(":{$placeholder}_0") . ")";
1424+
return "NOT ST_Crosses({$alias}.{$attribute}, " . $this->getSpatialGeomFromText(":{$placeholder}_0", null) . ")";
14251425

14261426
case Query::TYPE_DISTANCE_EQUAL:
14271427
case Query::TYPE_DISTANCE_NOT_EQUAL:
@@ -1431,43 +1431,43 @@ protected function handleSpatialQueries(Query $query, array &$binds, string $att
14311431

14321432
case Query::TYPE_INTERSECTS:
14331433
$binds[":{$placeholder}_0"] = $this->convertArrayToWKT($query->getValues()[0]);
1434-
return "ST_Intersects({$alias}.{$attribute}, " . $this->getSpatialGeomFromText(":{$placeholder}_0") . ")";
1434+
return "ST_Intersects({$alias}.{$attribute}, " . $this->getSpatialGeomFromText(":{$placeholder}_0", null) . ")";
14351435

14361436
case Query::TYPE_NOT_INTERSECTS:
14371437
$binds[":{$placeholder}_0"] = $this->convertArrayToWKT($query->getValues()[0]);
1438-
return "NOT ST_Intersects({$alias}.{$attribute}, " . $this->getSpatialGeomFromText(":{$placeholder}_0") . ")";
1438+
return "NOT ST_Intersects({$alias}.{$attribute}, " . $this->getSpatialGeomFromText(":{$placeholder}_0", null) . ")";
14391439

14401440
case Query::TYPE_OVERLAPS:
14411441
$binds[":{$placeholder}_0"] = $this->convertArrayToWKT($query->getValues()[0]);
1442-
return "ST_Overlaps({$alias}.{$attribute}, " . $this->getSpatialGeomFromText(":{$placeholder}_0") . ")";
1442+
return "ST_Overlaps({$alias}.{$attribute}, " . $this->getSpatialGeomFromText(":{$placeholder}_0", null) . ")";
14431443

14441444
case Query::TYPE_NOT_OVERLAPS:
14451445
$binds[":{$placeholder}_0"] = $this->convertArrayToWKT($query->getValues()[0]);
1446-
return "NOT ST_Overlaps({$alias}.{$attribute}, " . $this->getSpatialGeomFromText(":{$placeholder}_0") . ")";
1446+
return "NOT ST_Overlaps({$alias}.{$attribute}, " . $this->getSpatialGeomFromText(":{$placeholder}_0", null) . ")";
14471447

14481448
case Query::TYPE_TOUCHES:
14491449
$binds[":{$placeholder}_0"] = $this->convertArrayToWKT($query->getValues()[0]);
1450-
return "ST_Touches({$alias}.{$attribute}, " . $this->getSpatialGeomFromText(":{$placeholder}_0") . ")";
1450+
return "ST_Touches({$alias}.{$attribute}, " . $this->getSpatialGeomFromText(":{$placeholder}_0", null) . ")";
14511451

14521452
case Query::TYPE_NOT_TOUCHES:
14531453
$binds[":{$placeholder}_0"] = $this->convertArrayToWKT($query->getValues()[0]);
1454-
return "NOT ST_Touches({$alias}.{$attribute}, " . $this->getSpatialGeomFromText(":{$placeholder}_0") . ")";
1454+
return "NOT ST_Touches({$alias}.{$attribute}, " . $this->getSpatialGeomFromText(":{$placeholder}_0", null) . ")";
14551455

14561456
case Query::TYPE_EQUAL:
14571457
$binds[":{$placeholder}_0"] = $this->convertArrayToWKT($query->getValues()[0]);
1458-
return "ST_Equals({$alias}.{$attribute}, " . $this->getSpatialGeomFromText(":{$placeholder}_0") . ")";
1458+
return "ST_Equals({$alias}.{$attribute}, " . $this->getSpatialGeomFromText(":{$placeholder}_0", null) . ")";
14591459

14601460
case Query::TYPE_NOT_EQUAL:
14611461
$binds[":{$placeholder}_0"] = $this->convertArrayToWKT($query->getValues()[0]);
1462-
return "NOT ST_Equals({$alias}.{$attribute}, " . $this->getSpatialGeomFromText(":{$placeholder}_0") . ")";
1462+
return "NOT ST_Equals({$alias}.{$attribute}, " . $this->getSpatialGeomFromText(":{$placeholder}_0", null) . ")";
14631463

14641464
case Query::TYPE_CONTAINS:
14651465
$binds[":{$placeholder}_0"] = $this->convertArrayToWKT($query->getValues()[0]);
1466-
return "ST_Contains({$alias}.{$attribute}, " . $this->getSpatialGeomFromText(":{$placeholder}_0") . ")";
1466+
return "ST_Contains({$alias}.{$attribute}, " . $this->getSpatialGeomFromText(":{$placeholder}_0", null) . ")";
14671467

14681468
case Query::TYPE_NOT_CONTAINS:
14691469
$binds[":{$placeholder}_0"] = $this->convertArrayToWKT($query->getValues()[0]);
1470-
return "NOT ST_Contains({$alias}.{$attribute}, " . $this->getSpatialGeomFromText(":{$placeholder}_0") . ")";
1470+
return "NOT ST_Contains({$alias}.{$attribute}, " . $this->getSpatialGeomFromText(":{$placeholder}_0", null) . ")";
14711471

14721472
default:
14731473
throw new DatabaseException('Unknown spatial query method: ' . $query->getMethod());

src/Database/Adapter/MySQL.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,13 @@ protected function handleDistanceSpatialQueries(Query $query, array &$binds, str
117117

118118
if ($useMeters) {
119119
$attr = "ST_SRID({$alias}.{$attribute}, " . Database::SRID . ")";
120-
$geom = $this->getSpatialGeomFromText(":{$placeholder}_0");
120+
$geom = $this->getSpatialGeomFromText(":{$placeholder}_0", null);
121121
return "ST_Distance({$attr}, {$geom}, 'metre') {$operator} :{$placeholder}_1";
122122
}
123-
$attr = "ST_GeomFromText(ST_AsText({$alias}.{$attribute}), 0)";
123+
// need to use srid 0 because of geometric distance
124+
$attr = "ST_SRID({$alias}.{$attribute}, " . 0 . ")";
124125
$geom = $this->getSpatialGeomFromText(":{$placeholder}_0", 0);
125126
return "ST_Distance({$attr}, {$geom}) {$operator} :{$placeholder}_1";
126-
// Without meters, use default behavior
127-
// return "ST_Distance(ST_GeomFromText({$alias}.{$attribute}, 0, 'axis-order=lat-long')), ST_GeomFromText(:{$placeholder}_0, 0, 'axis-order=lat-long')) {$operator} :{$placeholder}_1";
128127
}
129128

130129
public function getSupportForIndexArray(): bool
@@ -237,6 +236,17 @@ public function getSpatialSQLType(string $type, bool $required): string
237236
*/
238237
public function getSupportForSpatialAxisOrder(): bool
239238
{
240-
return false; // Temporarily disable to test
239+
return true;
240+
}
241+
242+
/**
243+
* Get the spatial axis order specification string for MySQL
244+
* MySQL with SRID 4326 expects lat-long by default, but our data is in long-lat format
245+
*
246+
* @return string
247+
*/
248+
protected function getSpatialAxisOrderSpec(): string
249+
{
250+
return "'axis-order=long-lat'";
241251
}
242252
}

tests/e2e/Adapter/Scopes/SpatialTests.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,15 @@ public function testSpatialTypeDocuments(): void
119119
'$id' => 'doc1',
120120
'pointAttr' => [5.0, 5.0],
121121
'lineAttr' => [[1.0, 2.0], [3.0, 4.0]],
122-
'polyAttr' => [[[0.0, 0.0], [0.0, 10.0], [10.0, 10.0], [0.0, 0.0]]],
122+
'polyAttr' => [
123+
[
124+
[0.0, 0.0],
125+
[0.0, 10.0],
126+
[10.0, 10.0],
127+
[10.0, 0.0],
128+
[0.0, 0.0]
129+
]
130+
],
123131
'$permissions' => [Permission::update(Role::any()), Permission::read(Role::any())]
124132
]);
125133
$createdDoc = $database->createDocument($collectionName, $doc1);
@@ -187,9 +195,17 @@ public function testSpatialTypeDocuments(): void
187195
$polyQueries = [
188196
'contains' => Query::contains('polyAttr', [[5.0, 5.0]]), // Point inside polygon
189197
'notContains' => Query::notContains('polyAttr', [[15.0, 15.0]]), // Point outside polygon
190-
'intersects' => Query::intersects('polyAttr', [5.0, 5.0]), // Point inside polygon should intersect
198+
'intersects' => Query::intersects('polyAttr', [0.0, 0.0]), // Point inside polygon should intersect
191199
'notIntersects' => Query::notIntersects('polyAttr', [15.0, 15.0]), // Point outside polygon should not intersect
192-
'equals' => query::equal('polyAttr', [[[[0.0, 0.0], [0.0, 10.0], [10.0, 10.0], [0.0, 0.0]]]]), // Exact same polygon
200+
'equals' => query::equal('polyAttr', [[
201+
[
202+
[0.0, 0.0],
203+
[0.0, 10.0],
204+
[10.0, 10.0],
205+
[10.0, 0.0],
206+
[0.0, 0.0]
207+
]
208+
]]), // Exact same polygon
193209
'notEquals' => query::notEqual('polyAttr', [[[[20.0, 20.0], [20.0, 30.0], [30.0, 30.0], [20.0, 20.0]]]]), // Different polygon
194210
'overlaps' => Query::overlaps('polyAttr', [[[5.0, 5.0], [5.0, 15.0], [15.0, 15.0], [15.0, 5.0], [5.0, 5.0]]]), // Overlapping polygon
195211
'notOverlaps' => Query::notOverlaps('polyAttr', [[[20.0, 20.0], [20.0, 30.0], [30.0, 30.0], [30.0, 20.0], [20.0, 20.0]]]) // Non-overlapping polygon

0 commit comments

Comments
 (0)