You are here

function geofield_geometry_from_values in Geofield 7.2

Primary function for processing geoPHP geometry objects from raw data.

@TODO: Refactor the function to not check for $input_format from both the optional secondary parameter and an array item in $raw_data. This is probably an artifact from how Geofield's widgets pass data to various field hooks. We should only check the optional secondary parameter. @TODO: Move constants from geofield.widgets.inc to geofield.module @TODO: Provide useful failure return (FALSE)

Parameters

$raw_data: The info we're trying to process. Valid input can be a string or an array. If $raw_data is a string, the value is passed directly to geophp for parsing. If $raw_data is an array (as is expected for Lat/Lon or Bounds input), process into raw WKT and generate geometry object from there.

$input_format: Geofield module defined constants that specify a specific type of input. Useful for ensuring that only a specific type of data is valid (i.e., if we're expecting WKT, valid GeoJSON doesn't get processed).

Return value

A populated geoPHP geometry object if valid geometry, no return otherwise.

1 call to geofield_geometry_from_values()
geofield_compute_values in ./geofield.module
Geofield Compute Values

File

./geofield.module, line 454

Code

function geofield_geometry_from_values($raw_data, $input_format = NULL) {

  // 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;
  }
  if (is_array($raw_data)) {
    if (!empty($raw_data['input_format'])) {
      if ($raw_data['input_format'] == GEOFIELD_INPUT_LAT_LON) {
        $geometry = new Point($raw_data['geom']['lon'], $raw_data['geom']['lat']);
      }
      elseif ($raw_data['input_format'] == GEOFIELD_INPUT_BOUNDS) {
        $wkt_bounds_format = 'POLYGON((left bottom,right bottom,right top,left top,left bottom))';
        $wkt = strtr($wkt_bounds_format, array(
          'top' => $raw_data['geom']['top'],
          'right' => $raw_data['geom']['right'],
          'bottom' => $raw_data['geom']['bottom'],
          'left' => $raw_data['geom']['left'],
        ));
        $geometry = geoPHP::load($wkt);
      }
      else {
        $geometry = geoPHP::load($raw_data['geom'], $raw_data['input_format']);
      }
    }
    else {

      // No input format - let geoPHP figure it out
      if (!empty($raw_data['geom'])) {
        $geometry = geoPHP::load($raw_data['geom']);
      }
      elseif (!empty($raw_data['lat']) && !empty($raw_data['lon'])) {
        $geometry = new Point($raw_data['lon'], $raw_data['lat']);
      }
    }
  }
  else {
    if ($input_format) {
      $geometry = geoPHP::load($raw_data, $input_format);
    }
    else {

      // All we have at this point is a raw string. let GeoPHP figure it out
      $geometry = geoPHP::load($raw_data);
    }
  }
  if (isset($geometry)) {
    return $geometry;
  }
}