You are here

function farm_map_combine_geoms in farmOS 7

Combine multiple WKT geometries into a single GeoPHP geometry object.

Parameters

array $geoms: An array of geometry strings in WKT format.

Return value

object|bool Returns a GeoPHP object, or FALSE on failure.

3 calls to farm_map_combine_geoms()
farm_livestock_move_form in modules/farm/farm_livestock/farm_livestock.farm_quick.move.inc
Form for adding animal movement logs.
farm_livestock_move_form_submit in modules/farm/farm_livestock/farm_livestock.farm_quick.move.inc
Submit function for movement quick form.
farm_map_geofield_populate in modules/farm/farm_map/farm_map.module
Helper function for populating the geometry field of an entity.

File

modules/farm/farm_map/farm_map.geo.inc, line 87
Farm map geometry functions.

Code

function farm_map_combine_geoms($geoms = array()) {

  // If no geometries were found, return an empty geometry.
  if (empty($geoms)) {
    return FALSE;
  }

  // Load the GeoPHP library.
  geophp_load();

  // If there is more than one geometry, we will wrap it all in a
  // GEOMETRYCOLLECTION() at the end.
  $geometrycollection = FALSE;
  if (count($geoms) > 1) {
    $geometrycollection = TRUE;
  }

  // Build an array of WKT strings.
  $wkt_strings = array();
  foreach ($geoms as &$geom) {

    // If the geometry is empty, skip it.
    if (empty($geom)) {
      continue;
    }

    // Convert to a GeoPHP geometry object.
    $geometry = geoPHP::load($geom, 'wkt');

    // If this is a geometry collection, multi-point, multi-linestring, or
    // multi-polygon, then extract its components and add them individually to
    // the array.
    $multigeometries = array(
      'GeometryCollection',
      'MultiPoint',
      'MultiLineSting',
      'MultiPolygon',
    );
    if (in_array($geometry
      ->geometryType(), $multigeometries)) {

      // Iterate through the geometry components and add each to the array.
      $components = $geometry
        ->getComponents();
      foreach ($components as $component) {
        $wkt_strings[] = $component
          ->asText();
      }

      // Set $geometrycollection to TRUE in case there was only one geometry in
      // the $geoms parameter of this function, so that we know to wrap the WKT
      // in a GEOMETRYCOLLECTION() at the end.
      $geometrycollection = TRUE;
    }
    else {
      $wkt_strings[] = $geometry
        ->asText();
    }
  }

  // Combine all the WKT strings together into one.
  $wkt = implode(',', $wkt_strings);

  // If the WKT is empty, bail.
  if (empty($wkt)) {
    return FALSE;
  }

  // If there is more than one geometry, wrap them all in a geometry collection.
  if ($geometrycollection) {
    $wkt = 'GEOMETRYCOLLECTION (' . $wkt . ')';
  }

  // Convert to a final GeoPHP geometry object and reduce the geometry.
  $geometry = geoPHP::load($wkt, 'wkt');
  $geometry = geoPHP::geometryReduce($geometry);

  // Return the geometry.
  return $geometry;
}