Last active
August 29, 2015 14:16
-
-
Save quommit/3c8cd42164dea83bbcc6 to your computer and use it in GitHub Desktop.
PostgreSQL trigger function for geometry change notification
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE OR REPLACE FUNCTION redis_pubsub.notifygeometrychange_trigger() | |
RETURNS trigger AS | |
$BODY$ | |
DECLARE | |
message text; | |
geom geometry; | |
xmin double precision; | |
ymin double precision; | |
xmax double precision; | |
ymax double precision; | |
BEGIN | |
message := concat_ws(';', TG_TABLE_NAME, TG_OP); | |
IF TG_OP = 'UPDATE' THEN | |
IF NOT st_equals(OLD.geometria, NEW.geometria) THEN | |
IF NEW.geometria IS NULL THEN | |
geom := OLD.geometria; | |
ELSE | |
geom := NEW.geometria; | |
END IF; | |
END IF; | |
ELSIF TG_OP = 'INSERT' THEN | |
geom := NEW.geometria; | |
ELSIF TG_OP = 'DELETE' THEN | |
geom := OLD.geometria; | |
END IF; | |
xmin := st_xmin(box2d(geom)); | |
ymin := st_ymin(box2d(geom)); | |
xmax := st_xmax(box2d(geom)); | |
ymax := st_ymax(box2d(geom)); | |
message := concat_ws(';', message, xmin::text, ymin::text, xmax::text, ymax::text); | |
PERFORM redis_pubsub.notify('mapproxy', message, '127.0.0.1', 6379); | |
RETURN NULL; | |
END; | |
$BODY$ | |
LANGUAGE plpgsql VOLATILE; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment