Tuesday, 1 December 2015

How to use interacting PostgreSQL/PostGIS triggers in QGIS to create multiple labels on polygons?


EDIT:


The trigger functions posted are well done but there are still a few issues. I've uploaded two screen recordings:


http://workupload.com/file/ygRnYtp9


http://workupload.com/file/OBY7i9Fh




I use point geometries (table 'label_point') to place multiple labels on polygon geometries (table 'soil'). After doing layer edits I've to update the affected label_point features. Therefore I've created a trigger function.



CREATE TABLE label_point (
gid serial NOT NULL,
geom geometry(point, SRID),
label_sample varchar(255),
CONSTRAINT label_point_pkey PRIMARY KEY (gid)
);

CREATE TABLE soil (
gid serial NOT NULL,
geom geometry(polygon, SRID),

label varchar(255),
CONSTRAINT soil_pkey PRIMARY KEY (gid)
);

CREATE OR REPLACE FUNCTION sample_label()
RETURNS trigger AS $body$
BEGIN
IF GeometryType(NEW.geom) = 'POINT' THEN
EXECUTE 'SELECT label FROM soil WHERE ST_Within($1, soil.geom) LIMIT 1'
USING NEW.geom

INTO NEW.label_sample;
RETURN NEW;
ELSEIF GeometryType(NEW.geom) = 'POLYGON' THEN
EXECUTE 'UPDATE label_point SET label_sample = NULL WHERE ST_Within(label_point.geom, $1)'
USING NEW.geom;
RETURN NEW;
END IF;
END;
$body$ LANGUAGE plpgsql;


CREATE TRIGGER tg_sample_label BEFORE INSERT OR UPDATE
ON label_point FOR EACH ROW
EXECUTE PROCEDURE sample_label();


CREATE TRIGGER tg_sample_label AFTER INSERT OR UPDATE
ON soil FOR EACH ROW
EXECUTE PROCEDURE sample_label();

enter image description here



Unfortunately this solution suffers from two issues.


1.) When deleting a soil feature or moving a soil feature (ST_Within(label_point.geom, soil.geom) = FALSE) the sample_point features are not updated to NULL.


enter image description here enter image description here


2.) When splitting a soil feature using the QGIS 'Split Feature Tool' and changing the label of one polygon part the label_point features are not updated proberly after saving the edits.


enter image description here enter image description here enter image description here


Can anyone help me with this?




No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...