You are here

public function WktTrait::combineWkt in farmOS 2.x

Combine WKT geometries.

This does not use Geometry::union(), which is only available when GEOS is installed.

Parameters

array $geoms: An array of WKT geometry strings.

Return value

string Returns a combined WKT geometry string.

2 calls to WktTrait::combineWkt()
GeofieldWidget::fileParse in modules/core/map/src/Plugin/Field/FieldWidget/GeofieldWidget.php
Submit function to parse geometries from uploaded files.
LocationTest::testPopulateLogGeometry in modules/core/location/tests/src/Kernel/LocationTest.php
Test auto population of log geometry field.

File

modules/core/geo/src/Traits/WktTrait.php, line 37

Class

WktTrait
Provides methods to work with WKT.

Namespace

Drupal\farm_geo\Traits

Code

public function combineWkt(array $geoms) {

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

  // 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 = [];
  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 = [
      '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
          ->out('wkt');
      }

      // 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
        ->out('wkt');
    }
  }

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

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

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

  // Return the combined WKT.
  return $wkt;
}