Skip to content

Commit

Permalink
Merge pull request #483 from jjrom/develop
Browse files Browse the repository at this point in the history
Replace SQL function ST_SplitDateLine from tamn with PHP antimeridian code
  • Loading branch information
jjrom authored Dec 26, 2024
2 parents 3de6c4b + a3464f1 commit 7f877ab
Show file tree
Hide file tree
Showing 42 changed files with 6,137 additions and 566 deletions.
50 changes: 50 additions & 0 deletions admin_scripts/migrate_to_9.5.3.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/command/with-contenv php

<?php

require_once("/app/resto/core/RestoConstants.php");
require_once("/app/resto/core/RestoDatabaseDriver.php");
require_once("/app/resto/core/utils/RestoLogUtil.php");
require_once("/app/resto/core/utils/Antimeridian.php");
require_once("/app/resto/core/dbfunctions/UsersFunctions.php");

/*
* Read configuration from file...
*/
$configFile = '/etc/resto/config.php';
if ( !file_exists($configFile)) {
exit(1);
}
$config = include($configFile);
$dbDriver = new RestoDatabaseDriver($config['database'] ?? null);
$queries = [];

$antimeridian = new AntiMeridian();

$targetSchema = $dbDriver->targetSchema;

try {

$dbDriver->query('BEGIN');

// First populate geometry column
$dbDriver->query('UPDATE ' . $targetSchema . '.feature SET geometry = geom WHERE geometry IS NULL');

// Now convert all geometry so there are correctly computed with antimeridian
$results = $dbDriver->query('SELECT id, ST_AsGeoJSON(geometry) as geometry FROM ' . $targetSchema . '.feature');
while ($result = pg_fetch_assoc($results)) {
$dbDriver->pQuery('UPDATE ' . $targetSchema . '.feature SET geom=ST_SetSRID(ST_GeomFromGeoJSON($2), 4326) WHERE id=$1', array(
$result['id'],
json_encode($antimeridian->fixGeoJSON(json_decode($result['geometry'], true)), JSON_UNESCAPED_SLASHES)
));
}

$dbDriver->query('COMMIT');

} catch(Exception $e){
$dbDriver->query('ROLLBACK');
RestoLogUtil::httpError(500, $e->getMessage());
}
echo "Looks good\n";


2 changes: 1 addition & 1 deletion app/resto/core/RestoConstants.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class RestoConstants
// [IMPORTANT] Starting resto 7.x, default routes are defined in RestoRouter class

// resto version
const VERSION = '9.5.2';
const VERSION = '9.5.3';

/* ============================================================
* NEVER EVER TOUCH THESE VALUES
Expand Down
41 changes: 14 additions & 27 deletions app/resto/core/dbfunctions/GeneralFunctions.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,17 +238,23 @@ public function getTopologyAnalysis($geometry, $params)
* Convert to EPSG:4326 if input SRID differs from this projection
*/
$epsgCode = RestoGeometryUtil::geoJSONGeometryToSRID($geometry);
$geoJsonParser = 'ST_SetSRID(ST_GeomFromGeoJSON($1), 4326)';
if ($epsgCode !== "4326") {
$geoJsonParser = 'ST_Transform(ST_SetSRID(ST_GeomFromGeoJSON($1), ' . $epsgCode . '), 4326)';
if ($epsgCode !== 4326) {
try {
$result = pg_fetch_row($this->dbDriver->query_params('SELECT ST_AsGeoJSON(ST_Force2D(ST_Transform(ST_SetSRID(ST_GeomFromGeoJSON($1), ' . $epsgCode . '), 4326))) AS geom', array(
json_encode($geometry, JSON_UNESCAPED_SLASHES)
)), 0, PGSQL_ASSOC);
$geometry = json_decode($result['geom'], true);
} catch (Exception $e) {
$error = '[GEOMETRY] ' . pg_last_error($this->dbDriver->getConnection());
}
}

$antimeridian = new AntiMeridian();
$fixedGeometry = $antimeridian->fixGeoJSON($geometry);
try {
$result = pg_fetch_row($this->dbDriver->query_params('WITH tmp AS (SELECT ST_Force2D(' . $geoJsonParser . ') AS geom, ST_Force2D(' . $this->getSplitterFunction($geoJsonParser, $params) . ') AS _geom) SELECT geom, _geom, ST_Force2D(ST_SetSRID(ST_Centroid(_geom), 4326)) AS centroid, Box2D(ST_SetSRID(_geom, 4326)) as bbox FROM tmp', array(
json_encode(array(
'type' => $geometry['type'],
'coordinates' => $geometry['coordinates']
), JSON_UNESCAPED_SLASHES)
$result = pg_fetch_row($this->dbDriver->query_params('WITH tmp AS (SELECT ST_Force2D(ST_SetSRID(ST_GeomFromGeoJSON($1), 4326)) AS geom, ST_Force2D(ST_SetSRID(ST_GeomFromGeoJSON($2), 4326)) AS _geom) SELECT geom, _geom, ST_Force2D(ST_SetSRID(ST_Centroid(_geom), 4326)) AS centroid, Box2D(ST_SetSRID(_geom, 4326)) as bbox FROM tmp', array(
json_encode($geometry, JSON_UNESCAPED_SLASHES),
json_encode($fixedGeometry, JSON_UNESCAPED_SLASHES)
)), 0, PGSQL_ASSOC);
} catch (Exception $e) {
$error = '[GEOMETRY] ' . pg_last_error($this->dbDriver->getConnection());
Expand Down Expand Up @@ -294,23 +300,4 @@ private function getIp()
return $best;
}

/**
* Return Split function
*
* @param string $geom
* @param array $params
*/
private function getSplitterFunction($geom, $params)
{
// Specifically no split required !
if (isset($params['_splitGeom']) && !$params['_splitGeom']) {
return $geom;
}

if (!isset($params['tolerance'])) {
return 'ST_SetSRID(ST_SplitDateLine(' . $geom . '), 4326)';
}

return 'ST_SetSRID(ST_SimplifyPreserveTopologyWhenTooBig(ST_SplitDateLine(' . $geom . '),' . $params['tolerance'] . (isset($params['maxpoints']) ? ',' . $params['maxpoints'] : '') . '), 4326)';
}
}
Loading

0 comments on commit 7f877ab

Please sign in to comment.