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

Micha Silver micha at arava.co.il
Wed Mar 4 00:31:07 PST 2009


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





More information about the Qgis-user mailing list