[Qgis-user] Setting a line's length attribute

Andreas Neumann a.neumann at carto.net
Wed Mar 4 02:17:08 PST 2009


Hi Micha,

It is updating on inserting and updating as in the example I set the
trigger on both insert and update.

I should note, however, that the length updates only after you saved the
layer in QGIS. QGIS does bulk saves/updates and not per individual row you
edited. This is something to keep in mind - maybe you wonder why it isn't
updating immediately after editing.

Andreas


On Wed, March 4, 2009 9:31 am, Micha Silver wrote:
> Thanks for the excellent example!  One additional question (sorry for
> hijacking the original post):
>
>
> Andreas Neumann wrote:
>
>> Hi,
>>
>> Here is some documentation:
>> http://www.postgresql.org/docs/8.3/static/plpgsql.html (plpgsql) and
>> http://www.postgresql.org/docs/8.3/static/plpgsql-trigger.html
>> specifically for triggers.
>>
>> This would work for you (assuming that your geometry column is
>> "the_geom"
>> and the length columen is "object_length":
>>
>> CREATE FUNCTION update_length() RETURNS trigger AS $update_length$
>>   BEGIN
>>     NEW.object_length := ST_Length(NEW.the_geom);
>>     RETURN NEW;
>>   END;
>> $update_length$ LANGUAGE 'plpgsql';
>>
>> CREATE TABLE test.test_lines
>> (
>>   gid serial NOT NULL,
>>   the_geom geometry,
>>   object_length numeric,
>>   CONSTRAINT pkey_test_lines_gid PRIMARY KEY (gid),
>>   CONSTRAINT enforce_dims_the_geom CHECK (ndims(the_geom) = 2),
>>   CONSTRAINT enforce_geotype_the_geom CHECK (geometrytype(the_geom) =
>> 'LINESTRING'::text OR the_geom IS NULL),
>>   CONSTRAINT enforce_srid_the_geom CHECK (srid(the_geom) = 21781)
>> )
>> WITH (OIDS=FALSE);
>>
>> CREATE INDEX in_test_test_lines_gist
>>   ON test.test_lines
>>   USING gist
>>   (the_geom);
>>
>> CREATE TRIGGER update_length_testlines
>>   BEFORE INSERT OR UPDATE
>>   ON test.test_lines
>>   FOR EACH ROW
>>   EXECUTE PROCEDURE update_length();
>>
>>
> So, does this update the object_length column *both* on inserting a new
> feature and on editing an existing feature?
>
> Thanks again,
> Micha
>
>
>


-- 
Andreas Neumann
http://www.carto.net/neumann/
http://www.svgopen.org/




More information about the Qgis-user mailing list