SQL for spatial queries (1)
In this exercise, a connection between QGIS and the PostgreSQL/PostGIS Server is made, which is followed by loading vector shapefiles of Salzburgs district boundary, its roads and building footprints into the database.
Then, via SQL queries in the DB Manager within QGIS, several queries are executed. Three layers were extracted via these Queries:
- Layer containing school buildings in Salzburg:
- type= ‘school’
- Layer containing church buildings in Salzburg:
- type= ‘church’
- Layer containing both church and school buildings in Salzburg:
- type= ‘church’ OR type= ‘school’
SELECT
*
FROM
salzburg_buildings
WHERE
type = 'church' or type = 'school';
In the next step, we calculate the footprints of the school buildings as well. Following this, we load the resulting layer into QGIS and visualize the schools depending on their footprint.
SELECT
b.*,
st_area(b.geom) as area
FROM
salzburg_buildings AS b
WHERE
type = 'school';
Next up is a selection by location, more specifically by the distance from the bell tower of the Salzburg fortress. Additionally, the result of the query is exported as a layer to QGIS.
SELECT
b.*,
st_distance(
b.geom,
g.geom) AS distance
FROM
salzburg_buildings AS b,
(SELECT geom FROM salzburg_buildings WHERE name = 'Glockenturm' AS g;
SELECT
b.*
FROM
salzburg_buildings AS b,
(SELECT geom FROM salzburg_buildings WHERE name = 'Glockenturm') AS g
WHERE
st_distance(b.geom, g.geom) < 500;
ALTER TABLE salzburg_buildings
ADD COLUMN area float;
UPDATE salzburg_buildings
SET area = ST_AREA(geom);
SELECT
AVG(area)
FROM
salzburg_buildings AS b,
(SELECT geom FROM salzburg_buildings WHERE name = 'Glockenturm' AS g
WHERE
st_distance(b.geom,g.geom) <1000;
The 1905 buildings within 1.000m have an average footprint of 298.67. Because the WGS84 system is used, the result is in square meters.