You are here

function leaflet_process_geofield in Leaflet 7

Convert a geofield into an array of map points.

Parameters

array $items: A collection of geofield values.

Return value

array Points built for consumption by the leaflet module as expected by leaflet_render_map().

2 calls to leaflet_process_geofield()
leaflet_field_formatter_view in ./leaflet.formatters.inc
Implements hook_field_formatter_view().
leaflet_views_plugin_style::render in leaflet_views/leaflet_views_plugin_style.inc
Renders view.

File

./leaflet.formatters.inc, line 180
Leaflet field formatter functions.

Code

function leaflet_process_geofield($items = array()) {
  $data = array();
  geophp_load();
  foreach ($items as $delta => $item) {

    // Translate linestring to polyline:
    if ($item['geo_type'] == 'multilinestring') {
      $item['geo_type'] = 'multipolyline';
    }

    // Geofield 7.x-2.x compatibility.
    if (!isset($item['wkt']) && isset($item['geom'])) {
      $item['wkt'] = $item['geom'];
    }
    $datum = array(
      'type' => $item['geo_type'],
    );
    switch ($item['geo_type']) {
      case 'point':
        $datum += array(
          'lat' => (double) $item['lat'],
          'lon' => (double) $item['lon'],
        );
        $data[] = $datum;
        break;
      case 'linestring':
        $geom = geoPHP::load($item['wkt'], 'wkt');
        $components = $geom
          ->getComponents();
        foreach ($components as $component) {
          $datum['points'][] = array(
            'lat' => $component
              ->getY(),
            'lon' => $component
              ->getX(),
          );
        }
        $data[] = $datum;
        break;
      case 'polygon':
        $geom = geoPHP::load($item['wkt'], 'wkt');
        $tmp = $geom
          ->getComponents();
        $components = $tmp[0]
          ->getComponents();
        foreach ($components as $component) {
          $datum['points'][] = array(
            'lat' => $component
              ->getY(),
            'lon' => $component
              ->getX(),
          );
        }
        $data[] = $datum;
        break;
      case 'multipoint':
        $geom = geoPHP::load($item['wkt'], 'wkt');
        $points = $geom
          ->getComponents();
        foreach ($points as $point) {
          $data[] = array(
            'type' => 'point',
            'lat' => $point
              ->getY(),
            'lon' => $point
              ->getX(),
          );
        }
        break;
      case 'multipolygon':
        $geom = geoPHP::load($item['wkt'], 'wkt');
        $tmp = $geom
          ->getComponents();
        $components = array();
        foreach ($tmp as $polygon) {
          $polygon_component = $polygon
            ->getComponents();
          foreach ($polygon_component as $linestring) {
            $components[] = $linestring;
          }
        }
        foreach ($components as $key => $component) {
          $subcomponents = $component
            ->getComponents();
          foreach ($subcomponents as $subcomponent) {
            $datum['component'][$key]['points'][] = array(
              'lat' => $subcomponent
                ->getY(),
              'lon' => $subcomponent
                ->getX(),
            );
          }
          unset($subcomponent);
        }
        $data[] = $datum;
        break;
      case 'multipolyline':
        $geom = geoPHP::load($item['wkt'], 'wkt');
        $components = $geom
          ->getComponents();
        foreach ($components as $key => $component) {
          $subcomponents = $component
            ->getComponents();
          foreach ($subcomponents as $subcomponent) {
            $datum['component'][$key]['points'][] = array(
              'lat' => $subcomponent
                ->getY(),
              'lon' => $subcomponent
                ->getX(),
            );
          }
          unset($subcomponent);
        }
        $data[] = $datum;
        break;
      case 'geometrycollection':
        $geom = geoPHP::load($item['wkt'], 'wkt');
        foreach ($geom as $geom_items) {
          foreach ($geom_items as $geom_item) {
            $class = get_class($geom_item);
            switch ($class) {
              case 'Point':
                $datum = array();
                $datum['type'] = 'point';
                $datum += array(
                  'lat' => (double) $geom_item->coords[1],
                  'lon' => (double) $geom_item->coords[0],
                );
                $data[] = $datum;
                break;
              case 'LineString':
                $points = $geom_item
                  ->getComponents();
                $datum = array();
                $datum['type'] = 'linestring';
                foreach ($points as $component) {
                  $datum['points'][] = array(
                    'lat' => $component
                      ->getY(),
                    'lon' => $component
                      ->getX(),
                  );
                }
                $data[] = $datum;
                break;
              case 'Polygon':
                $points = $geom_item
                  ->getComponents();
                $components = $points[0]
                  ->getComponents();
                $datum = array();
                $datum['type'] = 'polygon';
                foreach ($components as $component) {
                  $datum['points'][] = array(
                    'lat' => $component
                      ->getY(),
                    'lon' => $component
                      ->getX(),
                  );
                }
                $data[] = $datum;
                break;
            }
          }
        }
        break;
    }
  }
  return $data;
}