Geofield
Provides a geo-location field type for storing and managing geographic data including points, lines, polygons, and multi-type geometries.
geofield
Install
composer require 'drupal/geofield:8.x-1.66'
Overview
Geofield is a comprehensive geospatial field module for Drupal that enables the storage and management of geographic data. It supports all geometry types defined by the Open Geospatial Consortium (OGC) including Points, MultiPoints, LineStrings, MultiLineStrings, Polygons, and MultiPolygons.
The module stores geographic data in Well Known Text (WKT) format along with computed metadata including centroid coordinates, bounding box values, and geohash for efficient spatial queries. It provides multiple input widgets for data entry (WKT text, Latitude/Longitude, Degrees-Minutes-Seconds, and Bounding Box) and multiple output formatters for display.
Geofield includes robust Views integration with proximity-based filtering, sorting, and field display capabilities using the Haversine formula for accurate distance calculations. The module supports multiple database backends including the default WKT storage and PostgreSQL with PostGIS for enhanced spatial functionality.
As a foundational module, Geofield integrates seamlessly with mapping libraries through companion modules like Geofield Map and Leaflet, geocoding services through the Geocoder module, and search functionality through Search API Location.
Features
- Stores all OGC geometry types: Point, MultiPoint, LineString, MultiLineString, Polygon, MultiPolygon
- Multiple input widgets: WKT text input, Latitude/Longitude with HTML5 geolocation, Degrees-Minutes-Seconds (DMS), and Bounding Box
- Multiple output formatters: Raw output in various formats (WKT, GeoJSON, KML, GPX, Google Geocode) and Lat/Lon display with decimal, DMS, and DM formats
- Comprehensive Views integration with proximity filter, sort, field, and argument handlers
- Rectangular boundary filter and argument for Views-based geographic queries
- Haversine formula implementation for accurate distance calculations in various units (kilometers, meters, miles, yards, feet, nautical miles)
- Multiple storage backends: Default WKT and PostGIS geometry for PostgreSQL databases
- Extensible proximity source plugin system for defining origin points (manual input, client browser geolocation, context filters, existing proximity filters)
- Migration support from Drupal 7 with dedicated process plugins
- Feeds module integration for bulk importing geographic data
- Diff module integration for comparing geographic field revisions
- Automatic computation of centroid, bounding box, and geohash values
- HTML5 Geolocation API integration for automatic location detection
Use Cases
Store and Display Business Locations
Add a Geofield to a 'Location' content type to store business addresses as geographic points. Use the Latitude/Longitude widget with HTML5 geolocation for easy data entry, and combine with Geofield Map or Leaflet modules to display locations on an interactive map.
Build a Store Locator
Create a Views page with an exposed Geofield Proximity Filter allowing visitors to enter their location or use browser geolocation. Sort results by distance and display the calculated proximity to each store. Combine with rectangular boundary filter for map-based searches.
Track Delivery Routes
Use Geofield to store LineString geometries representing delivery routes. The module stores the complete route as a WKT linestring while computing the centroid and bounding box for efficient spatial queries.
Define Geographic Boundaries
Store Polygon geometries to define sales territories, service areas, or administrative boundaries. Use the Bounding Box widget for quick rectangular areas or the WKT widget for complex polygon shapes.
Migrate Location Data
Use the geofield_latlon migrate process plugin to combine separate latitude and longitude source fields into Geofield values during Drupal migrations.
Bulk Import Locations
Use the Feeds module integration to import location data from CSV files containing either lat/lon columns or WKT/GeoJSON geometry strings.
Tips
- Use dot (.) as the decimal separator for coordinates, not comma (,), to ensure accurate proximity calculations
- The Latitude/Longitude widget only supports Point geometries; use the WKT widget for lines and polygons
- Enable geometry validation on the WKT widget when accepting user input to prevent invalid data
- Choose the PostGIS backend for PostgreSQL databases to leverage native spatial functions and indexes
- Proximity calculations use the Haversine formula which is accurate for most use cases but assumes a spherical Earth
- Views proximity arguments support unit suffixes: km (kilometers), m (meters), mi (miles), yd (yards), ft (feet), nmi (nautical miles)
- The geohash column enables efficient spatial indexing and nearby searches without complex calculations
- For large datasets, add database indexes on the lat, lon, and geohash columns for better query performance
Technical Details
Hooks 1
hook_field_views_data
Implements Views data for Geofield fields, adding proximity and boundary handlers.