Connect PostGIS with pygeoAPI
Posted on January 20, 2025
3 minutes
PygeoAPI is an efficient way to expose geospatial data via OGC-compliant APIs like OGC API — Features. It allows you to serve vector and raster data seamlessly, making it accessible to web and desktop applications.
Prerequisites
- PostGIS: Ensure you have PostgreSQL with the PostGIS extension installed.
- Python Environment: Use Python 3.8+.
- PygeoAPI Installed: Install PygeoAPI and dependencies.
Installation
To integrate PygeoAPI with PostGIS, install the required Python libraries:
pip install geoalchemy2 psycopg2-binary
geoalchemy2
: Provides support for PostGIS queries.psycopg2-binary
: PostgreSQL adapter for Python.
Add PostGIS as a Resource in PygeoAPI
Edit the PygeoAPI configuration file to add a new resource for your PostGIS table.
imp-places:
type: collection
title:
en: Important places
description:
en: All important places in world
keywords:
en:
- places
- point
- important
links:
- type: text/html
rel: canonical
title: information
href: https://www.naturalearthdata.com/downloads/10m-cultural-vectors/
hreflang: en-US
extents:
spatial:
bbox: [-180, -90, 180, 90]
crs: http://www.opengis.net/def/crs/OGC/1.3/CRS84
providers:
- type: feature
name: PostgreSQL
data:
host: 127.0.0.1
port: 5432 # Default 5432 if not provided
dbname: postgres
user: postgres
password: postgres
search_path: [public]
id_field: id
table: places
geom_field: geom
Explanation:
type: collection
: Defines a collection of features.providers
: Specifies PostgreSQL as the data source.- `Database Credentials ` : Replace
host
,dbname
,user
, andpassword
with your database details. table
: The table in PostGIS containing your geospatial data.geom_field
: The geometry column storing spatial data
Generate the OpenAPI Definition
After configuring your resource, generate the OpenAPI documentation using the following
pygeoapi openapi generate $PYGEOAPI_CONFIG --output-file $PYGEOAPI_OPENAPI
Replace:
$PYGEOAPI_CONFIG
: Path to your PygeoAPI configuration file.$PYGEOAPI_OPENAPI
: Desired output file for the OpenAPI documentation.
Run PygeoAPI
Start the PygeoAPI server to serve your PostGIS data:
pygeoapi serve
Query Your Data
Fetch All Data
Retrieve all features from the imp-places
collection:
http://localhost:5000/collections/imp-places/items
Filter by Bounding Box
Fetch features within a specific bounding box
http://localhost:5000/collections/imp-places/items?bbox=73.084466,16.734051,81.208856,21.752345
Sort Features
Retrieve features sorted by the name
attribute
http://localhost:5001/collections/imp-places/items?sortby=name
Filter by Comparison Operators
Use comparison operators in your filter expressions, such as =
, !=
, <
, >
, <=
, and >=
.
Example: Retrieve places with population less than 100:
http://localhost:5000/collections/imp-places/items?filter=pop_min<100
Filter by Like or Pattern Matching
Use the LIKE
operator for pattern matching on string attributes.
Example: Retrieve features where the adm0name attribute starts with "Ind"
http://localhost:5001/collections/imp-places/items?filter=adm0name LIKE "Ind%"
Who am I ? 🙍🏻
Hi, I’m Krishna Lodha, I’m an open source GIS Developer, Contributor and Content creator. I have my agency Rotten Grapes Pvt. Ltd. where we develop Web GIS Applications using Open source stack.
Apart from development, I upload Videos about such technologies on YouTube. I upload new blogposts every now and then on this Newsletter. Make sure you Subscribe before you leave ✅.