Add a helper function for populating the geometry field of an entity (logic extracted from farm_log.module).

This commit is contained in:
Michael Stenta 2017-05-10 09:00:43 -04:00
parent 40546015f2
commit d86660c368
1 changed files with 36 additions and 0 deletions

View File

@ -84,6 +84,42 @@ function farm_map_openlayers_object_preprocess_alter(&$build, $context) {
}
}
/**
* Helper function for populating the geometry field of an entity.
*
* @param Entity $entity
* The entity object.
* @param array $geoms
* An array of geometry strings in WKT format.
*/
function farm_map_geofield_populate(&$entity, $geoms = array()) {
// If no geometries were found, bail.
if (empty($geoms)) {
return;
}
// Load the GeoPHP library.
geophp_load();
// Implode the array of geometries into a single string.
$geom = implode(',', $geoms);
// If there is more than one geometry, wrap them all in a geometry collection.
if (count($geoms) > 1) {
$geom = 'GEOMETRYCOLLECTION (' . $geom . ')';
}
// Convert to a GeoPHP geometry object and reduce the geometry.
$geometry = geoPHP::load($geom, 'wkt');
$geometry = geoPHP::geometryReduce($geometry);
// Save the combined geometry to the movement log.
if (!empty($geometry)) {
$entity->field_farm_geofield[LANGUAGE_NONE][0] = geofield_get_values_from_geometry($geometry);
}
}
/**
* Calculate latitude degree length at a given latitude. Equations are taken
* from https://en.wikipedia.org/wiki/Geographic_coordinate_system#Expressing_latitude_and_longitude_as_linear_units