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

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


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();


Hope this helps,
Andreas


 Matej wrote: Hi Andreas,

 the solution with a trigger sounds very interesting.
 Could you please present more about it?

 Thanks,
 Matej
 matej at matnet.net

 ----- Original Message ----- From: "Andreas Neumann"
 To: "qgis-user"
 Sent: Tuesday, March 03, 2009 8:37 PM
 Subject: Re: [Qgis-user] Setting a line's length attribute










More information about the Qgis-user mailing list