-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtrigger.sql
More file actions
67 lines (59 loc) · 2.01 KB
/
trigger.sql
File metadata and controls
67 lines (59 loc) · 2.01 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
CREATE
OR REPLACE FUNCTION perform_polygon_subdivide () RETURNS TRIGGER AS $$
BEGIN
IF ST_NPoints(NEW.geog::geometry) > max_vertices()
AND GeometryType(NEW.geog::geometry) IN ('POLYGON', 'MULTIPOLYGON')
THEN
INSERT INTO polygons_subdivided (osm_id, osm_type, geom)
VALUES (
NEW.osm_id,
NEW.osm_type,
ST_SubDivide(NEW.geog::geometry, max_vertices())
);
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER insert_polygon_trigger
AFTER INSERT ON geometries FOR EACH ROW
EXECUTE FUNCTION perform_polygon_subdivide ();
CREATE
OR REPLACE FUNCTION delete_subdivided_polygon () RETURNS TRIGGER AS $$
BEGIN
-- we do not check the vertices count like in the other statements, because it does not matter in this case
IF GeometryType(OLD.geog::geometry) IN ('POLYGON', 'MULTIPOLYGON')
THEN
DELETE FROM polygons_subdivided WHERE osm_id = OLD.osm_id;
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER delete_polygon_trigger
AFTER DELETE ON geometries FOR EACH ROW
EXECUTE FUNCTION delete_subdivided_polygon ();
CREATE
OR REPLACE FUNCTION update_subdivided_polygon () RETURNS TRIGGER AS $$
BEGIN
IF ST_NPoints(NEW.geog::geometry) > max_vertices()
THEN
IF GeometryType(NEW.geog::geometry) IN ('POLYGON', 'MULTIPOLYGON') THEN
DELETE FROM polygons_subdivided WHERE osm_id = OLD.osm_id;
INSERT INTO polygons_subdivided (osm_id, osm_type, geom)
VALUES (
NEW.osm_id,
NEW.osm_type,
ST_SubDivide(NEW.geog::geometry, max_vertices())
);
END IF;
ELSE
-- edge case that object became less vertices than 'max_vertices'
-- therefore we remove it from subdivied table
DELETE FROM polygons_subdivided WHERE osm_id = OLD.osm_id;
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER update_polygon_trigger
AFTER
UPDATE ON geometries FOR EACH ROW
EXECUTE FUNCTION update_subdivided_polygon ();