Skip to content

Commit

Permalink
places: set SRID on mysql (#1067)
Browse files Browse the repository at this point in the history
Signed-off-by: Varun Patil <radialapps@gmail.com>
  • Loading branch information
pulsejet committed Mar 10, 2024
1 parent ed53199 commit 2effc74
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ All notable changes to this project will be documented in this file.

- Hide files starting with `.` in the timeline
- Prevent automatically retrying failed indexing jobs
- MySQL fixes: run `occ places-setup` again if you use MySQL (not MariaDB)

## [v6.2.2] - 2024-01-10

Expand Down
30 changes: 21 additions & 9 deletions lib/Service/Places.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ public function detectGisType(): int
}

// Detect database type
$platform = strtolower($this->connection->getDatabasePlatform()::class);
$platform = $this->getDbName();

// Test MySQL-like support in databse
if (str_contains($platform, 'mysql') || str_contains($platform, 'mariadb')) {
try {
$res = $this->connection->executeQuery("SELECT ST_GeomFromText('POINT(1 1)')")->fetch();
$res = $this->connection->executeQuery("SELECT ST_GeomFromText('POINT(1 1)', 4326)")->fetch();
if (0 === \count($res)) {
throw new \Exception('Invalid result');
}
Expand Down Expand Up @@ -98,9 +98,9 @@ public function queryPoint(float $lat, float $lon): array

// Construct WHERE clause depending on GIS type
$where = null;
if (1 === $gisType) {
$where = "ST_Contains(geometry, ST_GeomFromText('POINT({$lon} {$lat})'))";
} elseif (2 === $gisType) {
if (GIS_TYPE_MYSQL === $gisType) {
$where = "ST_Contains(geometry, ST_GeomFromText('POINT({$lon} {$lat})', 4326))";
} elseif (GIS_TYPE_POSTGRES === $gisType) {
$where = "POINT('{$lon},{$lat}') <@ geometry";
} else {
return [];
Expand Down Expand Up @@ -223,7 +223,7 @@ public function importPlanet(string $datafile): void
$query = $this->connection->getQueryBuilder();
$geomParam = (string) $query->createParameter('geometry');
if (GIS_TYPE_MYSQL === $gis) {
$geomParam = "ST_GeomFromText({$geomParam})";
$geomParam = "ST_GeomFromText({$geomParam}, 4326)";
} elseif (GIS_TYPE_POSTGRES === $gis) {
$geomParam = "POLYGON({$geomParam}::text)";
}
Expand Down Expand Up @@ -427,14 +427,18 @@ protected function setupDatabase(int $gis): void
// Drop the table if it exists
$this->connection->executeStatement('DROP TABLE IF EXISTS memories_planet_geometry');

// MySQL requires an SRID definition
// https://github.com/pulsejet/memories/issues/1067
$srid = str_contains($this->getDbName(), 'mysql') ? 'SRID 4326' : '';

// Create table
$sql = 'CREATE TABLE memories_planet_geometry (
$sql = "CREATE TABLE memories_planet_geometry (
id varchar(32) NOT NULL PRIMARY KEY,
poly_id varchar(32) NOT NULL,
type_id int NOT NULL,
osm_id int NOT NULL,
geometry polygon NOT NULL
);';
geometry polygon NOT NULL ${srid}
);";
$this->connection->executeQuery($sql);

// Add indexes
Expand All @@ -450,4 +454,12 @@ protected function setupDatabase(int $gis): void
throw new \Exception('Failed to create database tables: '.$e->getMessage());
}
}

/**
* Get the database platform name.
*/
private function getDbName(): string
{
return strtolower($this->connection->getDatabasePlatform()::class);
}
}

0 comments on commit 2effc74

Please sign in to comment.