Kartoza - Zonal Operations using PostGIS
Interactions with remote datasets from Cloud storage providers opens up many possibilities
Recently I have been reviewing training materials from the QGIS Changelog site. One particular lesson I liked was how to do Zonal Operations. I thought about how to replicate the output produced in the lesson using Cloud storage and the PostGIS database.
The basis of this tutorial assumes a user has the following:
- Cloud Storage. This could be Minio or Amazon S3 buckets or any other storage providers.
- A PostgreSQL database instance.
- ogr_fdw extension installed in the PostgreSQL database.
For a local setup, I am using a docker-compose setup.
version: '3.9'volumes:postgis-data:minio_data:services:minio:image: quay.io/minio/minioenvironment:- MINIO_ROOT_USER=minio_admin- MINIO_ROOT_PASSWORD=secure_minio_secretentrypoint: /bin/bashcommand: -c 'minio server /data --console-address ":9001"'volumes:- minio_data:/mapproxyports:- "9000:9000"- "9001:9001"db:image: kartoza/postgis:16-3.4volumes:- postgis-data:/var/lib/postgresqlenvironment:- POSTGRES_DB=gis- POSTGRES_USER=docker- POSTGRES_PASS=docker- ALLOW_IP_RANGE=0.0.0.0/0# Add extensions you need to be enabled by default in the DB. Default are the five specified below- POSTGRES_MULTIPLE_EXTENSIONS=postgis,hstore,postgis_topology,postgis_raster,pgrouting,ogr_fdw- RUN_AS_ROOT=trueports:- "25432:5432"restart: on-failuredepends_on:minio:condition: service_startedhealthcheck:test: "PGPASSWORD=docker pg_isready -h 127.0.0.1 -U docker -d gis"
- Use the docker-compose specified above to bring the services up. `docker-compose up -d`
- Navigate to the MinIO UI and create a bucket i.e. `postgis`.
- Download sample datasets from the training tutorial and upload the datasets into the bucket you created in step 2.
- Exec into the PostgreSQL container using the command `docker-compose exec db bash`.
- Install postgis which will allow you to access the raster tool `raster2pgsql`
apt get update;apt install -y postgis
- Load the raster data into the database
export PGPASSWORD="docker"raster2pgsql -s 3857 -t 256x256 -I -R /vsicurl/http://minio:9000/postgis/kzn_pop_count.tif kzn_pop_count | psql -d gis -p 5432 -U docker -h localhost
- Use GDAL to see if you can access the spatial data stored in the MinIO bucket.
ogrinfo -ro -al -so /vsicurl/http://minio:9000/postgis/districts.shp
- Login to the database using psql command line.
psql -d gis -p 5432 -U docker -h localhost
- Run he SQL commands to create a server and foreign table accessing the remote vector data.
-- Create remote server to interact with vector layer stored in minio bucketCREATE SERVER remote_shpFOREIGN DATA WRAPPER ogr_fdwOPTIONS (datasource '/vsicurl/http://minio:9000/postgis/districts.shp',format 'ESRI Shapefile' );-- create foreign table to map the vector layer into the DBCREATE FOREIGN TABLE "districts" (fid integer,"geom" geometry(MultiPolygon, 4326),"name" varchar ,"DISTRICT" varchar ,"PROVINCE" varchar)SERVER remote_shpOPTIONS (layer 'districts');
- To speed up accessing the foreign table we create a materialized view of the vector layer.
--Also project the geom to match the CRS of the raster layerCREATE materialized view mv_districts asSELECT fid,name, "DISTRICT" as district, "PROVINCE" as province, st_transform(geom,3857) as geomfrom districts;
- Run the SQL for generating zonal statistics.
SELECT-- provides: count | sum | mean | stddev | min | max(ST_SummaryStats(ST_Clip(kzn_pop_count.rast, mv_districts.geom, TRUE))).*,mv_districts.name AS name,mv_districts.geom AS geometryFROMkzn_pop_count, mv_districts;
- If your raster data has multiple bands the SQL above will not work and should be adjusted in the following part `uST_Clip(kzn_pop_count.rast,1, mv_districts.geom` or alternatively use GDAL to translate the raster, extracting the band you need before uploading the raster layer in step 3, or pass the `-b 1` option in `raster2pgsql` command .
There are multiple ways to tackle geospatial problems and various software provides different methods to use. Storing data on cloud storage providers allows multiple tools to access the same datasets and thereby allowing various analysis to be formulated.
No comments yet. Login to start a new discussion Start a new discussion