You are here

function geofield_compute_values in Geofield 7

Same name and namespace in other branches
  1. 7.2 geofield.module \geofield_compute_values()

Geofield Compute Values

Compute all dependant values. We compute all other values from whichever column is specified in the master_column value

Steps: 1. Load the geoPHP library 2. Load the Geometry object from the master-column 3. Get out all the computer values from the Geometry object 4. Set all the values

Allowed values for master_column are wkt, latlon, bounds

5 calls to geofield_compute_values()
geofield_element_validate in ./geofield.widgets.inc
Geofield Element Validate
geofield_feeds_combined_source in ./geofield.feeds.inc
Callback; Provides a source combining geo information from items.
geofield_field_presave in ./geofield.module
Implements hook_field_presave(). PDO throws an error when attempting to insert an empty string into a float field. Go through all values and convert empty strings to NULL.
geofield_set_target_wkt in ./geofield.feeds.inc
Feeds processor target callback for WKT source.
_geofield_devel_generate in ./geofield.devel_generate.inc

File

./geofield.module, line 175

Code

function geofield_compute_values(&$values, $master_column = 'wkt') {

  // If only a wkt string has been passed in, then format it correctly by wrapping it in an array
  if ($master_column == 'wkt' && !is_array($values)) {
    $values = array(
      'wkt' => $values,
    );
  }

  // Load up geoPHP to do the conversions
  $geophp = geophp_load();
  if (!$geophp) {
    drupal_set_message(t("Unable to load geoPHP library. Not all values will be calculated correctly"), 'error');
    return;
  }

  // Load up the geometry object from the master-column data
  if ($master_column == 'wkt') {
    $wkt = $values['wkt'];
    if ($wkt) {
      $geometry = geoPHP::load($wkt, 'wkt');
    }
  }
  if ($master_column == 'latlon') {
    $lat = $values['lat'];
    $lon = $values['lon'];
    if (is_numeric($lat) && is_numeric($lon)) {
      $geometry = new Point(floatval($lon), floatval($lat));
    }
  }
  if ($master_column == 'bounds') {
    $top = $values['top'];
    $bottom = $values['bottom'];
    $right = $values['right'];
    $left = $values['left'];
    if (is_numeric($top) && is_numeric($bottom) && is_numeric($right) && is_numeric($left)) {
      $wkt_bounds_format = 'POLYGON((left bottom,right bottom,right top,left top,left bottom))';
      $wkt = strtr($wkt_bounds_format, array(
        'top' => $top,
        'bottom' => $bottom,
        'right' => $right,
        'left' => $left,
      ));
      $geometry = geoPHP::load($wkt, 'wkt');
    }
  }

  // Get values from geometry
  if (isset($geometry)) {
    $values = geofield_get_values_from_geometry($geometry);
  }
  else {
    $values = array();
  }
  return $values;
}