diff --git a/farm_map.module b/farm_map.module index ee583833..05f37473 100644 --- a/farm_map.module +++ b/farm_map.module @@ -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