[GIS] way to have a ‘live’ buffer in QGIS

bufferqgis

I use QGIS (2.10.1) to create buffers around polygons. I often create a series of buffers, and later need to edit the underlying polygons. It is frustrating to start from scratch in re-create several buffers based on the changes when this happens.

Is there a way to create a 'live' buffer, that will automatically update to changes made to the parent polygon, or as a second best, a means to replicate with one click, the buffer settings and formatting that you originally created?

Best Answer

Creating a View in PostGIS to have a "live buffer" is of course the best choice if you are willing to setup a RDBMS. I was curious to see if you could also get some sort of live buffer without PostGIS, so I gave it a try with spatialite and it works too. The difference is just that it seems that you can't create a View with a different geometry-type in spatialite. I found this inforation here: https://groups.google.com/forum/#!topic/spatialite-users/ZZIPwYt8-uA there was written:

"there is absolutely no way to show a View geometry in spatialite-gis, if the geometry-class isn't exactly the same of the one used for the main-table"

So you have to use a buffer table and three triggers:

CREATE TABLE lines ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL);


SELECT AddGeometryColumn('lines', 'geometry', 3857, 'LINESTRING', 'XY');


CREATE TABLE lines_buffer (id INTEGER PRIMARY KEY AUTOINCREMENT, gid INTEGER, name TEXT NOT NULL);

SELECT AddGeometryColumn('lines_buffer', 'geo', 3857, 'POLYGON', 'XY');

CREATE TRIGGER "lines_buffer_insert" AFTER
INSERT ON "lines"
FOR EACH ROW BEGIN
INSERT INTO lines_buffer (gid, name, geo)
SELECT new.id,
       new.name,
       ST_Buffer(new.geometry, 1000) ; 
END

CREATE TRIGGER "lines_buffer_update" AFTER
UPDATE ON "lines"
FOR EACH ROW BEGIN
UPDATE lines_buffer
SET geo=(ST_Buffer(NEW.geometry, 1000))
WHERE gid=NEW.id ;
  UPDATE lines_buffer
  SET name=NEW.name WHERE gid=NEW.id ; 
END

  CREATE TRIGGER "lines_buffer_delete" AFTER
  DELETE ON "lines"
  FOR EACH ROW BEGIN
  DELETE
  FROM lines_buffer WHERE gid=OLD.id ;
 END

I havent tested it excessively, but i created, updated and deleted several lines and the buffer was always updated (after refreshing/panning QGIS of course). So this could be an alternative where you have just one spatialite file to store the data and don't have to setup a PostGIS database... enter image description here

Related Question