You are here

function _views_geojson_render_fields in Views GeoJSON 7

Same name and namespace in other branches
  1. 6 views_geojson.module \_views_geojson_render_fields()

Render views fields to GeoJSON.

Takes each field from a row object and renders the field as determined by the field's theme.

Parameters

object $view: View the row belongs to.

object $row: Row object.

Return value

array Object containing all the raw and rendered fields

1 call to _views_geojson_render_fields()
views_plugin_style_geojson::render in views/views_plugin_style_geojson.inc
Implementation of view_style_plugin::render().

File

./views_geojson.module, line 42
Provide a GeoJSON Views feed style.

Code

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;
}