Skip to content

Instantly share code, notes, and snippets.

@mstenta
Created September 7, 2022 20:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mstenta/fbc912cb0e41f6af4f32b22b742009c7 to your computer and use it in GitHub Desktop.
Save mstenta/fbc912cb0e41f6af4f32b22b742009c7 to your computer and use it in GitHub Desktop.
Fix farmOS (v2) geometry longitudes per https://github.com/farmOS/farmOS/issues/33
<?php
$debug = TRUE;
// Function for recursively fixing the longitude points within a Geometry.
function recursive_fix_lon(&$feature) {
$fixed = FALSE;
if ($feature->geometryType() == 'Point' && !empty($feature->coords[0])) {
$lon = &$feature->coords[0];
$lon = fmod($lon, 360);
if ($lon < -180) {
$lon += 360;
$fixed = TRUE;
}
elseif ($lon > 180) {
$lon -= 360;
$fixed = TRUE;
}
}
elseif ($feature->geometryType() != 'Point' && !empty($feature->components)) {
foreach ($feature->components as $component) {
if (recursive_fix_lon($component)) {
$fixed = TRUE;
}
}
}
return $fixed;
}
// Load logs with geometry.
$logs = [];
$query = \Drupal::database()->select('log__geometry', 'g');
$query->addField('g', 'entity_id', 'id');
$query->isNotNull('g.geometry_value');
$query->condition('g.geometry_value', '', '!=');
$query->condition('g.geometry_value', '', '!=');
$result = $query->execute();
$log_ids = [];
foreach ($result as $row) {
if (!empty($row->id)) {
$log_ids[] = $row->id;
}
}
$logs = \Drupal::service('entity_type.manager')->getStorage('log')->loadMultiple($log_ids);
// Iterate through logs.
foreach ($logs as $log) {
// Load the geometry features.
$features = geoPHP::load($log->get('geometry')->value, 'wkt');
// Fix the longitude points.
$fixed = recursive_fix_lon($features);
// If points were fixed, save it.
if ($fixed) {
$features->setGeos(NULL);
$log->set('geometry', $features->out('wkt'));
if (!$debug) {
$log->save();
}
print("Fixed log " . $log->id() . "\n");
}
}
// Load assets with intrinsic geometry.
$assets = [];
$query = \Drupal::database()->select('asset__intrinsic_geometry', 'g');
$query->addField('g', 'entity_id', 'id');
$query->isNotNull('g.intrinsic_geometry_value');
$query->condition('g.intrinsic_geometry_value', '', '!=');
$query->condition('g.intrinsic_geometry_value', '', '!=');
$result = $query->execute();
$asset_ids = [];
foreach ($result as $row) {
if (!empty($row->id)) {
$asset_ids[] = $row->id;
}
}
$assets = \Drupal::service('entity_type.manager')->getStorage('asset')->loadMultiple($asset_ids);
// Iterate through assets.
foreach ($assets as $asset) {
// Load the geometry features.
$features = geoPHP::load($asset->get('intrinsic_geometry')->value, 'wkt');
// Fix the longitude points.
$fixed = recursive_fix_lon($features);
// If points were fixed, save it.
if ($fixed) {
$features->setGeos(NULL);
$asset->set('intrinsic_geometry', $features->out('wkt'));
if (!$debug) {
$asset->save();
}
print("Fixed asset " . $asset->id() . "\n");
}
}
@mstenta
Copy link
Author

mstenta commented Sep 7, 2022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment