2020-05-20 17:14:22 +00:00
|
|
|
DROP TRIGGER IF EXISTS trigger_delete_point ON osm_water_polygon;
|
|
|
|
DROP TRIGGER IF EXISTS trigger_update_point ON osm_water_polygon;
|
|
|
|
DROP TRIGGER IF EXISTS trigger_insert_point ON osm_water_polygon;
|
2016-12-28 23:19:54 +00:00
|
|
|
|
2020-05-20 17:14:22 +00:00
|
|
|
CREATE OR REPLACE VIEW osm_water_point_view AS
|
2020-06-08 16:19:55 +00:00
|
|
|
SELECT wp.osm_id,
|
|
|
|
ST_PointOnSurface(wp.geometry) AS geometry,
|
|
|
|
wp.name,
|
|
|
|
wp.name_en,
|
|
|
|
wp.name_de,
|
|
|
|
update_tags(wp.tags, ST_PointOnSurface(wp.geometry)) AS tags,
|
|
|
|
ST_Area(wp.geometry) AS area,
|
|
|
|
wp.is_intermittent
|
|
|
|
FROM osm_water_polygon AS wp
|
|
|
|
LEFT JOIN lake_centerline ll ON wp.osm_id = ll.osm_id
|
|
|
|
WHERE ll.osm_id IS NULL
|
|
|
|
AND wp.name <> '';
|
2020-05-20 17:14:22 +00:00
|
|
|
|
|
|
|
-- etldoc: osm_water_polygon -> osm_water_point
|
|
|
|
-- etldoc: lake_centerline -> osm_water_point
|
|
|
|
CREATE TABLE IF NOT EXISTS osm_water_point AS
|
2020-06-08 16:19:55 +00:00
|
|
|
SELECT *
|
|
|
|
FROM osm_water_point_view;
|
|
|
|
DO
|
|
|
|
$$
|
2020-05-20 17:14:22 +00:00
|
|
|
BEGIN
|
2020-06-08 16:19:55 +00:00
|
|
|
ALTER TABLE osm_water_point
|
|
|
|
ADD CONSTRAINT osm_water_point_pk PRIMARY KEY (osm_id);
|
|
|
|
EXCEPTION
|
|
|
|
WHEN OTHERS THEN
|
|
|
|
RAISE NOTICE 'primary key osm_water_point_pk already exists in osm_water_point.';
|
2020-05-20 17:14:22 +00:00
|
|
|
END;
|
|
|
|
$$;
|
2016-11-29 19:13:38 +00:00
|
|
|
CREATE INDEX IF NOT EXISTS osm_water_point_geometry_idx ON osm_water_point USING gist (geometry);
|
2017-03-17 11:56:54 +00:00
|
|
|
|
2017-01-04 21:59:42 +00:00
|
|
|
-- Handle updates
|
2017-01-02 23:31:17 +00:00
|
|
|
|
2017-01-11 21:31:47 +00:00
|
|
|
CREATE SCHEMA IF NOT EXISTS water_point;
|
2017-01-04 21:59:42 +00:00
|
|
|
|
2020-06-08 16:19:55 +00:00
|
|
|
CREATE OR REPLACE FUNCTION water_point.delete() RETURNS trigger AS
|
|
|
|
$$
|
2017-01-04 21:59:42 +00:00
|
|
|
BEGIN
|
2020-06-08 16:19:55 +00:00
|
|
|
DELETE
|
|
|
|
FROM osm_water_point
|
|
|
|
WHERE osm_water_point.osm_id = OLD.osm_id;
|
2020-05-20 17:14:22 +00:00
|
|
|
|
2020-06-08 16:19:55 +00:00
|
|
|
RETURN NULL;
|
2017-03-17 11:56:54 +00:00
|
|
|
END;
|
2020-06-08 16:19:55 +00:00
|
|
|
$$ LANGUAGE plpgsql;
|
2020-05-20 17:14:22 +00:00
|
|
|
|
2020-06-08 16:19:55 +00:00
|
|
|
CREATE OR REPLACE FUNCTION water_point.update() RETURNS trigger AS
|
|
|
|
$$
|
2020-05-20 17:14:22 +00:00
|
|
|
BEGIN
|
|
|
|
UPDATE osm_water_point
|
|
|
|
SET (osm_id, geometry, name, name_en, name_de, tags, area, is_intermittent) =
|
2020-06-08 16:19:55 +00:00
|
|
|
(SELECT * FROM osm_water_point_view WHERE osm_water_point_view.osm_id = NEW.osm_id)
|
2020-05-20 17:14:22 +00:00
|
|
|
WHERE osm_water_point.osm_id = NEW.osm_id;
|
|
|
|
|
2020-06-08 16:19:55 +00:00
|
|
|
RETURN NULL;
|
2020-05-20 17:14:22 +00:00
|
|
|
END;
|
2020-06-08 16:19:55 +00:00
|
|
|
$$ LANGUAGE plpgsql;
|
2020-05-20 17:14:22 +00:00
|
|
|
|
2020-06-08 16:19:55 +00:00
|
|
|
CREATE OR REPLACE FUNCTION water_point.insert() RETURNS trigger AS
|
|
|
|
$$
|
2020-05-20 17:14:22 +00:00
|
|
|
BEGIN
|
|
|
|
INSERT INTO osm_water_point
|
|
|
|
SELECT *
|
|
|
|
FROM osm_water_point_view
|
2020-07-21 15:35:49 +00:00
|
|
|
WHERE osm_water_point_view.osm_id = NEW.osm_id
|
|
|
|
-- May happen in case we replay update
|
|
|
|
ON CONFLICT ON CONSTRAINT osm_water_point_pk
|
|
|
|
DO NOTHING;
|
2020-05-20 17:14:22 +00:00
|
|
|
|
2020-06-08 16:19:55 +00:00
|
|
|
RETURN NULL;
|
2020-05-20 17:14:22 +00:00
|
|
|
END;
|
2020-06-08 16:19:55 +00:00
|
|
|
$$ LANGUAGE plpgsql;
|
2020-05-20 17:14:22 +00:00
|
|
|
|
|
|
|
CREATE TRIGGER trigger_delete_point
|
2020-06-08 16:19:55 +00:00
|
|
|
AFTER DELETE
|
|
|
|
ON osm_water_polygon
|
2020-05-20 17:14:22 +00:00
|
|
|
FOR EACH ROW
|
2020-06-08 16:19:55 +00:00
|
|
|
EXECUTE PROCEDURE water_point.delete();
|
2020-05-20 17:14:22 +00:00
|
|
|
|
|
|
|
CREATE TRIGGER trigger_update_point
|
2020-06-08 16:19:55 +00:00
|
|
|
AFTER UPDATE
|
|
|
|
ON osm_water_polygon
|
2020-05-20 17:14:22 +00:00
|
|
|
FOR EACH ROW
|
2020-06-08 16:19:55 +00:00
|
|
|
EXECUTE PROCEDURE water_point.update();
|
2020-05-20 17:14:22 +00:00
|
|
|
|
|
|
|
CREATE TRIGGER trigger_insert_point
|
2020-06-08 16:19:55 +00:00
|
|
|
AFTER INSERT
|
|
|
|
ON osm_water_polygon
|
2017-01-04 21:59:42 +00:00
|
|
|
FOR EACH ROW
|
2020-06-08 16:19:55 +00:00
|
|
|
EXECUTE PROCEDURE water_point.insert();
|