You are here

views_geojson.module in Views GeoJSON 7

Same filename and directory in other branches
  1. 6 views_geojson.module

Provide a GeoJSON Views feed style.

File

views_geojson.module
View source
<?php

/**
 * @file
 * Provide a GeoJSON Views feed style.
 */

/**
 * Implements hook_views_api().
 */
function views_geojson_views_api() {
  return array(
    'api' => '3.0',
    'path' => drupal_get_path('module', 'views_geojson') . '/views',
  );
}

/**
 * Implements hook_ctools_plugin_api().
 */
function views_geojson_ctools_plugin_api($module, $api) {
  return array(
    'version' => 1,
    'path' => drupal_get_path('module', 'views_geojson') . '/includes',
  );
}

/**
 * Render views fields to GeoJSON.
 *
 * Takes each field from a row object and renders the field as determined by the
 * field's theme.
 *
 * @param object $view
 *   View the row belongs to.
 * @param object $row
 *   Row object.
 *
 * @return array
 *   Object containing all the raw and rendered fields
 */
function _views_geojson_render_fields($view, $row, $index) {
  $excluded_fields = array();
  $feature = array(
    'type' => 'Feature',
  );
  $data_source = $view->style_plugin->options['data_source'];
  $field_ids = array_keys($view->field);

  // Pre-render fields to handle those rewritten with tokens.
  foreach ($view->field as $field_idx => $field) {
    $field
      ->advanced_render($row);
  }
  switch ($data_source['value']) {
    case 'latlon':
      $options = array(
        'latitude',
        'longitude',
      );
      $latitude = NULL;
      $longitude = NULL;
      foreach ($view->field as $field_idx => $field) {
        foreach ($options as $option) {
          if ($data_source[$option] === $field_idx) {
            ${$option} = $field
              ->advanced_render($row);
            $excluded_fields[] = $field_idx;
          }
        }
      }
      if (!empty($latitude) && !empty($longitude)) {
        $feature['geometry'] = array(
          'type' => 'Point',
          'coordinates' => array(
            (double) $longitude,
            (double) $latitude,
          ),
        );
      }
      break;
    case 'geofield':
      foreach ($view->field as $field_idx => $field) {
        if ($data_source['geofield'] === $field_idx) {
          $geofield = $view->style_plugin
            ->get_field_value($index, $field_idx);
          if (!empty($geofield)) {
            $geofield = isset($geofield[0]['wkt']) ? $geofield[0]['wkt'] : $geofield[0]['geom'];
          }
          $view->row_index = $index;
          $excluded_fields[] = $field_idx;
        }
      }
      if (!empty($geofield)) {
        geophp_load();
        $json = geoPHP::load($geofield);
        if (is_object($json)) {
          $feature['geometry'] = json_decode($json
            ->out('json'));
        }
      }
      break;
    case 'wkt':
      foreach ($view->field as $field_idx => $field) {
        if ($data_source['wkt'] === $field_idx) {
          $wkt = $field
            ->advanced_render($row);
          $view->row_index = $index;
          $excluded_fields[] = $field_idx;
        }
      }
      if (!empty($wkt)) {
        geophp_load();
        $json = geoPHP::load($wkt, 'wkt');
        if (is_object($json)) {
          $feature['geometry'] = json_decode($json
            ->out('json'));
        }
      }
      break;
  }

  // Only add features with geometry data.
  if (empty($feature['geometry'])) {
    return;
  }

  // Add the name and description attributes
  // as chosen through interface.
  $name_field = '';
  if ($data_source['name_field']) {
    foreach ($view->field as $field_idx => $field) {
      if ($data_source['name_field'] === $field_idx) {
        $name_field = $field
          ->advanced_render($row);
        $excluded_fields[] = $field_idx;
      }
    }
  }
  $feature['properties']['name'] = $name_field;
  if ($data_source['description_field']) {
    $description_field = NULL;
    foreach ($view->field as $field_idx => $field) {
      if ($data_source['description_field'] === $field_idx) {
        $description_field = $field
          ->advanced_render($row);
        $excluded_fields[] = $field_idx;
      }
    }
    $feature['properties']['description'] = $description_field;
  }
  else {
    $feature['properties']['description'] = '';
  }

  // Fill in attributes that are not:
  // - Coordinate fields,
  // - Name/description (already processed),
  // - Views "excluded" fields.
  foreach ($field_ids as $id) {
    $field = $view->field[$id];
    if (!in_array($id, $excluded_fields, TRUE) && !$field->options['exclude']) {

      // Allows you to customize the name of the property by setting a label to
      // the field.
      $key = empty($field->options['label']) ? $id : $field->options['label'];
      $value_rendered = $field
        ->advanced_render($row);
      $feature['properties'][$key] = $value_rendered;

      // Check if the data should be typed.
      if (!isset($view->style_options['typed_data_excluded_fields'][$id])) {
        $feature['properties'][$key] = is_numeric($feature['properties'][$key]) ? (double) $feature['properties'][$key] : $feature['properties'][$key];
      }
    }
  }
  return $feature;
}

Functions