Changes since 1.x

  • Pluggable backend architecture - Data storage can work natively with Postgres/PostGIS databases via a plugin.
  • Native FormAPI elements - Geofield provides 3 FormAPI elements (Lat/Lon, Bounding Box, and Proximity) for use in custom forms.
  • Consolidation of Field Widgets - The HTML5 Geolocation widget from 1.x has been merged into the Lat/Lon widget.
  • Views-powered proximity queries

Geofield from a developer perspective

Database

Geofield stores the following columns in the database.

  • geom - the canonical representation of the geometry. Stored as WKB if accessing directly, automatically adjusted to WKT when loaded with entity_load.
  • geo_type
  • lat - For non-points calculated from centerpoint
  • lon - For non-points, calculated from centerpoint
  • left
  • top
  • right
  • bottom
  • geohash - http://en.wikipedia.org/wiki/Geohash - very useful for fast proximity searches via solr.

Geofield defines a pluggable system to change the column type of geom. We do this to allow for database-specific optimizations, like using geometry columns in Postgres/PostGIS. A plugin for doing just that can be found at https://github.com/phayes/geofield_postgis.

Saving data programmatically

Saving geospatial data in Geofield can be tricky. To help make it easier, Geofield can accept data in a wide array of formats. For an example entity with a field named 'field_geofield,' one could save data under $node->field_geofield[LANGAUGE_NONE][$delta] in the following formats.

  • An array with a 'geom' value in WKT format.
  • An array with an input_format value of GEOFIELD_INPUT_WKT, which processes the 'geom' value as wkt data.
  • An array with an input_format value of GEOFIELD_INPUT_GEOJSON, which process the 'geom' value as geojson data.
  • An array with an input_format value of GEOFIELD_INPUT_LAT_LON and a geom value of an array with lat and lon values as decimal degrees.
  • An array with an input_format value of GEOFIELD_INPUT_BOUNDS, which processes the 'top', 'right', 'bottom', and 'left' values as geo data.
  • A string that can be parsed by any of geoPHP's processors.

Here is an example, showing how to update a Geofield field of a node using metadata wrapper. The $node is the node object or nid to be updated, the "field_wkt_data" is the machine name of the Geofield field. In this case the geofield_compute_values function is used to update the Geofield field values from wkt string.

$object = entity_metadata_wrapper('node', $node);

$geofield_data = geofield_compute_values('LINESTRING (20.2 40.4, 19.1 40.3)', GEOFIELD_INPUT_WKT);

$object->field_wkt_data->set($geofield_data);
$object->save();

To update a point location (longitude/latitude) Geofield field in a node, the $geofield_data assignment in the above example would change to (taken from this source):

$geofield_data = geofield_compute_values(array(
        'lat' => [Latitude], 
        'lon' => [Longitude]
  ), GEOFIELD_INPUT_LAT_LON);

Comments

kiwipion’s picture

These this & this page has examples of GEOFIELD_INPUT_WKT & GEOFIELD_INPUT_LAT_LON in action connecting to a drupal service via JSON.

zterry95’s picture

I can't open the 2 links above.

pal4life’s picture

Hi,
This Geofield 2.x note does not mention about KML support? but WKT. Is KML still supported in Geofield 2.x?

Thanks.