You are here

protected function GeolocationGeometry::parseGeoJson in Geolocation Field 8.3

Parse GeoJson for content.

Parameters

string $geoJson: GeoJSON.

array $locations: Locations to be filled.

array $shapes: Shapes to be filled.

2 calls to GeolocationGeometry::parseGeoJson()
GeolocationGeometry::getLocationsFromItem in modules/geolocation_geometry/src/Plugin/geolocation/DataProvider/GeolocationGeometry.php
Get locations from field item list.
GeolocationGeometry::getShapesFromItem in modules/geolocation_geometry/src/Plugin/geolocation/DataProvider/GeolocationGeometry.php
Get shapes from field item list.

File

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

Class

GeolocationGeometry
Provides GPX.

Namespace

Drupal\geolocation_geometry\Plugin\geolocation\DataProvider

Code

protected function parseGeoJson(string $geoJson, array &$locations, array &$shapes) {
  $json = json_decode($geoJson);
  if (is_object($json) && isset($json->type)) {
    $json = [
      $json,
    ];
  }
  foreach ($json as $entry) {
    if (empty($entry->type)) {
      continue;
    }
    switch ($entry->type) {
      case 'FeatureCollection':
        if (empty($entry->features)) {
          return;
        }
        $this
          ->parseGeoJson($entry->features, $locations, $shapes);
        return;
      case 'Feature':
        if (empty($entry->geometry)) {
          return;
        }
        $this
          ->parseGeoJson($entry->geometry, $locations, $shapes);
        return;
      case 'GeometryCollection':
        if (empty($entry->geometries)) {
          return;
        }
        $this
          ->parseGeoJson($entry->geometries, $locations, $shapes);
        return;
      case 'MultiPolygon':
      case 'Polygon':
      case 'MultiLineString':
      case 'LineString':
        if (empty($entry->coordinates)) {
          return;
        }
        $shapes[] = $entry;
        return;
      case 'MultiPoint':
      case 'Point':
        if (empty($entry->coordinates)) {
          return;
        }
        $locations[] = $entry;
        return;
    }
  }
}