← All latest work

Forestry Geomatics

PostGIS database for forest tourism safety

A post-graduate team project: a PostGIS database and 16 spatial SQL queries that help a forest district service tourist sites and find trees that could fall on paths, roads and buildings.

PostGIS · PostgreSQL · SQL · QGIS · Docker · Forestry

Map of the Giżycko Forest District with litter-bin sites coloured by forest range and an inset of building density per range

This was the final project of the Spatial Databases course in my post-graduate programme, Geomatics and Artificial Intelligence in Forestry. There were three of us. A forest district is legally responsible for visitor safety, so we built a PostgreSQL + PostGIS database that supports servicing tourist sites and finding trees that could fall on them. The study area was the Giżycko Forest District. I managed the project, and we discussed the details together on Discord.

Infrastructure

I first ran PostGIS as a Docker stack on a Raspberry Pi 5 NAS, managed through Portainer, and the team reached it over a Tailscale VPN. Later I made it a reproducible docker-compose stack with GDAL and an init script. I designed three schemas: raw for imported data, temporary for intermediate tables and analityka for results. Each person had their own role, with read-only access to the raw data. A bash script loads every shapefile into EPSG:2180 with ogr2ogr.

ERD of the analytics schema, with result tables grouped by author

The data covered BDOT10k, Forest Data Bank layers, Strava routes, field measurements, stand heights and tree crowns segmented by AI from a canopy height model.

My queries

I wrote queries 1–4 and made the map for them. They cover tourist sites with bins per forest range, Strava route length per range, the nearest fire station to each campsite (a LATERAL join with <-> KNN) and building density per range.

Team results

My teammates flagged crown segments whose fall distance (1.5 × height) reaches infrastructure. They also modelled hazard zones and aggregated the results on a 15 m hex grid. In the Las Miejski forest they analysed 76,816 segments, and the hazard zone covered 110.27 ha, about 24.6% of the area.

Crown segments classified as hazardous (red) along roads and trails in the Las Miejski forest

Heatmap of hazardous trees per 15 m hexagon

What I learned

  • Indexes are not optional. My building-density query first ran without spatial indexes. It used up the server’s memory and the connection dropped (error 08006). With GiST indexes and VACUUM ANALYZE, it took seconds instead of minutes.
  • Reduce the data before joining. Crowns covered one forest, infrastructure the whole district. Clipping the infrastructure to the bounding box of the crowns made the slow queries usable. CTEs kept joins from multiplying rows.
  • Check the coordinate system. Field points arrive in WGS84, while forest data uses PL-1992. Buffers in degrees are wrong, so geometry is transformed to EPSG:2180 inside the query.
  • Make scripts re-runnable. Queries now drop their own result tables first, so they can run again after the yearly forest-database update.
  • Containers have traps. With tty: true, psql opened a pager and init hung. Init scripts run only on an empty volume, and the import order (shapefiles, CSV, then indexes and foreign keys) matters.
  • QGIS projects are fragile. Print layouts find layers by name, so a moved project needs the same connection name and layer names.