Improve geospatial functionality with additional PostGIS extensions
PostGIS adds support for geospatial data in PostgreSQL, providing both data types and functions to store and analyze it effectively. The Postgres ecosystem includes multiple extensions built on top of PostGIS, to further enhance its capabilities. This guide introduces you to some of these extensions supported by Neon:
These extensions offer specialized functionality for routing, hierarchical geospatial indexing, advanced geometric operations, and geocoding. We'll explore how to enable these extensions and provide examples of common use cases.
note
These extensions are open-source and can be installed on any Neon Project using the instructions below. For detailed installation instructions, please refer to the documentation for each extension.
Version availability:
For up-to-date information on supported versions for each extension, refer to the list of all extensions available in Neon.
Enable the PostGIS extension
The extensions listed below typically need PostGIS to be installed first, or work in conjunction with it. You can enable PostGIS by running the following CREATE EXTENSION statement in the Neon SQL Editor or from a client such as psql that is connected to Neon.
pgrouting extends PostGIS to provide geospatial routing and network analysis functionality. It's useful for applications involving transportation networks, logistics planning, and urban mobility analysis.
Enable the pgrouting extension
Enable the extension by running the following SQL statement:
Example usage
Let's consider a scenario where we need to find the shortest path between two points in a road network.
Create a table with road network data
This dataset represents a simplified road network with 6 road segments connecting 5 intersections.
Use pgrouting to find the shortest path between nodes
We can use pgrouting's pgr_dijkstra function to find the shortest path between two nodes:
This query returns the sequence of edges that form the shortest path from node 2 to node 4.
Use pgrouting to find alternative routes
For navigation applications, we might need to find multiple alternative routes. We can use the pgr_ksp function to find the K-shortest paths between two nodes:
This query returns two sequence of edges, that can be used to go from node 1 to node 4.
H3 and H3 PostGIS
H3 is a hierarchical geospatial indexing system. It divides the earth's surface into hexagonal cells at multiple resolutions, and provides a unique addressing system for location data. It is used for applications like optimizing delivery zones and service areas, geospatial aggregation, and analytics.
The H3 functionality is split into two extensions: h3 and h3_postgis.
Enable the H3 and H3_PostGIS extensions
Enable these extensions by running the following SQL statements:
Example usage
We will show how to use H3 to analyze ride-sharing data in a large city, focusing on the distribution of pickup locations.
Create a table with pickup location data
This dataset represents the pickup locations for a ride-sharing service in a large city.
Convert points to H3 indexes
We can use the h3_lat_lng_to_cell function to convert lat/long coordinates to H3 indexes:
This query converts each pickup location to an H3 index at resolution 9.
Aggregate data by H3 cells
Let's aggregate the pickup data into H3 cells at resolution 8 (average hexagon edge length of ~461 meters) to identify hotspots:
This query groups the dataset by the H3 index, and then provides a count of pickups, as well as the earliest and latest pickup times for each cell.
Compute neighbour H3 cells
For cells with high demand, you might want to identify neighboring cells to recommend the areas to cover. The h3_grid_disk function can be used to fetch neighboring cells within k distance from the given cell:
This query identifies the hexagon cell for the top pickup location and then fetches the neighboring cells adjacent to it.
PostGIS SFCGAL
PostGIS SFCGAL provides advanced 2D and 3D spatial operations using the SFCGAL library. It's useful for complex geometric calculations, 3D operations, and working with solid objects.
Enable the PostGIS SFCGAL extension
Enable the extension by running the following SQL statement:
Example usage
We will illustrate the use of SFCGAL to perform some urban planning tasks.
Create a table with building data
This query creates a table to store building footprints and heights.
Use SFCGAL to calculate volumes
We can use SFCGAL to calculate the volume of buildings by extruding their footprints:
This query calculates the volume of each building by extruding its 2D footprint to its height, and then calculating the volume of the resulting 3D object.
Use SFCGAL to perform 3D intersection
SFCGAL can be used to perform 3D intersections. For example, an important urban planning task is to examine how buildings might obstruct views from one another.
We can use SFCGAL to create 3D models of our buildings and then check for intersections between these models and sight lines.
This query does the following:
It creates 3D models of all buildings using ST_Extrude.
For each pair of buildings, it creates a line from the center of one building to the center of another, representing a potential sight line.
It uses ST_3DIntersects to check if this sight line intersects with any 3D building model (other than the buildings at the endpoints of the line).
If there's an intersection, it indicates a potential view obstruction.
It returns the following output:
This example demonstrates how SFCGAL's 3D capabilities can be used to analyze spatial relationships between buildings in three dimensions, which is useful for urban planning and architectural design.
PostGIS Tiger Geocoder
PostGIS Tiger Geocoder provides address normalization and geocoding functionality using TIGER (Topologically Integrated Geographic Encoding and Referencing) data. This extension is useful for address validation, normalization, and conversion of addresses to geographic coordinates.
Enable the PostGIS Tiger Geocoder extension
Enable the extension by running the following SQL statement:
Example usage
Use Tiger Geocoder to normalize an address
Address normalization is crucial for ensuring consistency in address data. We can use the normalize_address function to standardize address formats.
This query returns a normalized version of the input addresses.
Conclusion
These examples provide a quick introduction to using other extensions in the PostGIS ecosystem. They can significantly expand the geospatial capabilities of your Neon Postgres database.
For further information, refer to the official documentation for each extension.
Join our Discord Server to ask questions or see what others are doing with Neon. Users on paid plans can open a support ticket from the console. For more details, see Getting Support.