You are here

private function LeafletService::leafletProcessGeometry in Leaflet 2.1.x

Same name and namespace in other branches
  1. 8 src/LeafletService.php \Drupal\leaflet\LeafletService::leafletProcessGeometry()
  2. 2.0.x src/LeafletService.php \Drupal\leaflet\LeafletService::leafletProcessGeometry()

Process the Geometry Collection.

Parameters

\Geometry $geom: The Geometry Collection.

Return value

array The return array.

1 call to LeafletService::leafletProcessGeometry()
LeafletService::leafletProcessGeofield in src/LeafletService.php
Convert a geofield into an array of map points.

File

src/LeafletService.php, line 210

Class

LeafletService
Provides a LeafletService class.

Namespace

Drupal\leaflet

Code

private function leafletProcessGeometry(\Geometry $geom) {
  $datum = [
    'type' => strtolower($geom
      ->geometryType()),
  ];
  switch ($datum['type']) {
    case 'point':
      $datum = [
        'type' => 'point',
        'lat' => $geom
          ->getY(),
        'lon' => $geom
          ->getX(),
      ];
      break;
    case 'linestring':

      /* @var \GeometryCollection $geom */
      $components = $geom
        ->getComponents();

      /* @var \Geometry $component */
      foreach ($components as $component) {
        $datum['points'][] = [
          'lat' => $component
            ->getY(),
          'lon' => $component
            ->getX(),
        ];
      }
      break;
    case 'polygon':

      /* @var \GeometryCollection $geom */
      $tmp = $geom
        ->getComponents();

      /* @var \GeometryCollection $geom */
      $geom = $tmp[0];
      $components = $geom
        ->getComponents();

      /* @var \Geometry $component */
      foreach ($components as $component) {
        $datum['points'][] = [
          'lat' => $component
            ->getY(),
          'lon' => $component
            ->getX(),
        ];
      }
      break;
    case 'multipolyline':
    case 'multilinestring':
      if ($datum['type'] == 'multilinestring') {
        $datum['type'] = 'multipolyline';
        $datum['multipolyline'] = TRUE;
      }

      /* @var \GeometryCollection $geom */
      $components = $geom
        ->getComponents();

      /* @var \GeometryCollection $component */
      foreach ($components as $key => $component) {
        $subcomponents = $component
          ->getComponents();

        /* @var \Geometry $subcomponent */
        foreach ($subcomponents as $subcomponent) {
          $datum['component'][$key]['points'][] = [
            'lat' => $subcomponent
              ->getY(),
            'lon' => $subcomponent
              ->getX(),
          ];
        }
        unset($subcomponent);
      }
      break;
    case 'multipolygon':
      $components = [];

      /* @var \GeometryCollection $geom */
      $tmp = $geom
        ->getComponents();

      /* @var \GeometryCollection $polygon */
      foreach ($tmp as $delta => $polygon) {
        $polygon_component = $polygon
          ->getComponents();
        foreach ($polygon_component as $k => $linestring) {
          $components[] = $linestring;
        }
      }
      foreach ($components as $key => $component) {
        $subcomponents = $component
          ->getComponents();

        /* @var \Geometry $subcomponent */
        foreach ($subcomponents as $subcomponent) {
          $datum['component'][$key]['points'][] = [
            'lat' => $subcomponent
              ->getY(),
            'lon' => $subcomponent
              ->getX(),
          ];
        }
      }
      break;
    case 'geometrycollection':
    case 'multipoint':

      /* @var \GeometryCollection $geom */
      $components = $geom
        ->getComponents();
      foreach ($components as $key => $component) {
        $datum['component'][$key] = $this
          ->leafletProcessGeometry($component);
      }
      break;
  }
  return $datum;
}