-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathupdate-station-geometry.sql
More file actions
53 lines (47 loc) · 1.32 KB
/
update-station-geometry.sql
File metadata and controls
53 lines (47 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
CREATE OR REPLACE FUNCTION update_station_bounding_box(station_id_param INTEGER)
RETURNS VOID AS $$
BEGIN
-- Update the bounding_box for the specified station
-- by calculating the envelope of all associated measurement points
UPDATE stations
SET geometry = subquery.bbox
FROM (
SELECT
ST_Envelope(ST_Collect(geometry))::geometry AS bbox
FROM
measurements
WHERE
stationid = station_id_param
GROUP BY
stationid
) AS subquery
WHERE stations.stationid = station_id_param;
-- If no measurements exist for this station, set bounding_box to NULL
IF NOT FOUND THEN
UPDATE stations
SET geometry = NULL
WHERE stationid = station_id_param;
END IF;
END;
$$ LANGUAGE plpgsql;
UPDATE stations
SET geometry = subquery.bbox
FROM (
SELECT
ST_Envelope(ST_Collect(geometry))::geometry AS bbox
FROM
measurements
WHERE
stationid = 1
GROUP BY
stationid
) AS subquery
WHERE stations.stationid = 1;
SELECT
ST_AsText(ST_GeomFromEWKT(ST_AsEWKT(ST_Envelope(ST_Collect(geometry))))) AS bbox
FROM
measurements
WHERE
stationid = 1
GROUP BY
stationid