Case study
Answering park questions with spatial SQL in PostGIS and QGIS
Loaded a national park dataset into PostGIS and answered distance, length, area and proximity questions with spatial SQL, mapping the results in QGIS.
- Context
- Course lab (GIS 550, Geospatial Data Structures and Web Services), NC State, April 2017. The questions were set by the assignment
- Data
- George Washington Birthplace National Monument layers and fish survey tables, provided with the assignment
- Output
- SQL views, query results and QGIS maps
Problem
The lab asked a set of questions about George Washington Birthplace National Monument that had to be answered in the database rather than with point-and-click tools. How far apart are two buildings? How long are the park's streams? How much building footprint is there? And for a hypothetical bird that roosts in trees and eats White Perch: where might it be found, and which buildings sit close enough to its feeding sites to disturb it?
Approach
I loaded the park layers into a PostGIS database, converting their projections to EPSG codes, and connected QGIS to it. I then wrote SQL views for each question and mapped the views in QGIS. The 200 meter disturbance distance was given. For how far the bird would travel from a roost to feed, no distance was given, so I chose 300 meters and said so in my answer.
-- Identify trees within 300 meters of GEWA whitepearch sites
CREATE OR REPLACE VIEW v_birdlocations AS
SELECT DISTINCT ON (t.id) t.id, t.geom, w.siteid
FROM v_trees t
RIGHT JOIN v_whitepearch w ON ST_DWithin(t.geom, w.the_geom, 300)
ORDER BY t.id, ST_Distance(t.geom, w.the_geom);
-- Identify buidlings within 200 meters of GEWA whitepearch sites
CREATE OR REPLACE VIEW v_DangerBuildings AS
SELECT DISTINCT ON (db.feature) db.feature, db.geom, w.siteid
FROM v_gewa_building db
RIGHT JOIN v_whitepearch w ON ST_DWithin(db.geom, w.the_geom, 200)
ORDER BY db.feature, ST_Distance(db.geom, w.the_geom);
Supporting views pulled tree locations from the vegetation layer, and White Perch survey sites by joining the sites, site visits and fish catch tables.
Result
| Question | Answer | Method |
|---|---|---|
| Distance from the Visitor Center to Rockefeller Barn | 336 m (0.21 mi) | ST_Centroid, ST_Distance |
| Total length of streams | 11.48 km | ST_Length |
| Total building footprint | 3,991 m² (42,947 sq ft) | ST_Area |
| Where the bird might roost and feed | Trees within 300 m of White Perch sites GEWA03, GEWA28, GEWA30 and GEWA31 | ST_DWithin view |
| Buildings that may disturb feeding | Buildings within 200 m of White Perch sites, including the Visitor Center | ST_DWithin view, QGIS buffer |