You are on page 1of 3

SIK002 - Databases

PostGIS Part 2 – Spatial joins and aggregation

1 Spatial joins
Practice

Figure 1: Landscape (purple) and trees (red) at UJI

Practice
Run the following queries:
1. Find how many trees are placed inside 'Grass' landscape areas (Answer: 105)
select count(*)
from trees t join landscapearea l on ST_Within(t.geom,l.geom)
where l.surftype='Grass';

2. Find how many trees are placed on each surface type (Answer: 1544 on Ivy /
Groundcover; 280 on Planter; 105 on Grass)
select l.surftype,count(*)
from landscapearea l join trees t on ST_Within(t.geom,l.geom)
Group By l.surftype
Order by count desc;
3. How many landscape areas overlap with other landscape areas? (Answer: 16)
Select count(*)
from landscapearea a join landscapearea b on ST_Overlaps(a.geom,b.geom);

4. How many trees are there within a distance 100 from building 'Escola Infantil' ?
(Answer: 44)
select count(*)
from trees t join buildings b on ST_DWithin(b.geom,t.geom,100)
where b.longname='Escola Infantil';

2 Spatial aggregation

Figure 2: Convex hull of the sport area at UJI

Practice
1. Which is the area of the convex hull of all buildings with facility type 'D'?
(Answer:123570.024817824)
SELECT facilityke, ST_Area(ST_ConvexHull(ST_Collect(geom)))
from buildings
where facilityke ='D'
Group by facilityke;
2. Which is the area occupied by the convex hull of all trees of each name? (Answer:
No name: 351226.325091665; Elm: 926287.047598897; Pine: 33148.6450494188;
Palm: 17522.6649254289 )
SELECT name, ST_Area(ST_ConvexHull(ST_Collect(geom)))
from trees
Group by name;

You might also like