You are here

public function GeolocationGeometry::getShapesFromItem in Geolocation Field 8.3

Get shapes from field item list.

Parameters

\Drupal\Core\Field\FieldItemInterface $fieldItem: Views field definition.

Return value

array Renderable shapes.

Overrides DataProviderBase::getShapesFromItem

File

modules/geolocation_geometry/src/Plugin/geolocation/DataProvider/GeolocationGeometry.php, line 189

Class

GeolocationGeometry
Provides GPX.

Namespace

Drupal\geolocation_geometry\Plugin\geolocation\DataProvider

Code

public function getShapesFromItem(FieldItemInterface $fieldItem) {
  $settings = $this
    ->getSettings();
  $shapes = $locations = [];
  $this
    ->parseGeoJson($fieldItem
    ->get('geojson')
    ->getString(), $locations, $shapes);
  $positions = [];
  foreach ($shapes as $shape) {
    $random_color = sprintf('#%06X', mt_rand(0, 0xffffff));
    switch ($shape->type) {
      case 'Polygon':
        $coordinates = '';
        foreach ($shape->coordinates[0] as $coordinate) {
          $coordinates .= $coordinate[1] . ',' . $coordinate[0] . ' ';
        }
        $position = [
          '#type' => 'geolocation_map_polygon',
          '#coordinates' => $coordinates,
          '#stroke_color' => $settings['stroke_color_randomize'] ? $random_color : $settings['stroke_color'],
          '#stroke_width' => (int) $settings['stroke_width'],
          '#stroke_opacity' => (double) $settings['stroke_opacity'],
          '#fill_color' => $settings['fill_color_randomize'] ? $random_color : $settings['fill_color'],
          '#fill_opacity' => (double) $settings['fill_opacity'],
        ];
        $positions[] = $position;
        break;
      case 'MultiPolygon':
        $container = [
          '#type' => 'container',
          '#attributes' => [
            'class' => [
              'geolocation-multipolygon',
            ],
          ],
        ];
        foreach ($shape->coordinates as $key => $polygon) {
          $coordinates = '';
          foreach ($polygon[0] as $coordinate) {
            $coordinates .= $coordinate[1] . ',' . $coordinate[0] . ' ';
          }
          $position = [
            '#type' => 'geolocation_map_polygon',
            '#coordinates' => $coordinates,
            '#stroke_color' => $settings['stroke_color_randomize'] ? $random_color : $settings['stroke_color'],
            '#stroke_width' => (int) $settings['stroke_width'],
            '#stroke_opacity' => (double) $settings['stroke_opacity'],
            '#fill_color' => $settings['fill_color_randomize'] ? $random_color : $settings['fill_color'],
            '#fill_opacity' => (double) $settings['fill_opacity'],
          ];
          $container[$key] = $position;
        }
        $positions[] = $container;
        break;
      case 'LineString':
        $coordinates = '';
        foreach ($shape->coordinates[0] as $coordinate) {
          $coordinates .= $coordinate[1] . ',' . $coordinate[0] . ' ';
        }
        $position = [
          '#type' => 'geolocation_map_polyline',
          '#coordinates' => $coordinates,
          '#stroke_color' => $settings['stroke_color_randomize'] ? $random_color : $settings['stroke_color'],
          '#stroke_width' => (int) $settings['stroke_width'],
          '#stroke_opacity' => (double) $settings['stroke_opacity'],
        ];
        $positions[] = $position;
        break;
      case 'MultiLineString':
        $container = [
          '#type' => 'container',
          '#attributes' => [
            'class' => [
              'geolocation-multipolyline',
            ],
          ],
        ];
        foreach ($shape->coordinates as $key => $polyline) {
          $coordinates = '';
          foreach ($polyline[0] as $coordinate) {
            $coordinates .= $coordinate[1] . ',' . $coordinate[0] . ' ';
          }
          $position = [
            '#type' => 'geolocation_map_polyline',
            '#coordinates' => $coordinates,
            '#stroke_color' => $settings['stroke_color_randomize'] ? $random_color : $settings['stroke_color'],
            '#stroke_width' => (int) $settings['stroke_width'],
            '#stroke_opacity' => (double) $settings['stroke_opacity'],
          ];
          $container[$key] = $position;
        }
        $positions[] = $container;
        break;
    }
  }
  return $positions;
}