<div dir="ltr"><div><div><div><div>Hey,<br></div>it is tricky to use qgis value directly, because depending on the version of qgis (and gdal?) you may have errors with (multi , 3D, M ).<br></div>I would advise to strongly enforce a type to any input from qgis.<br></div>(for instance ST_SnapToGrid(  ST_Force2D(  ST_GeometryN(ST_CollectionExtract(geom,1),1)  ) , 0.1) <br></div><div>for a 2D point precision 0.1 .<br></div><div><br></div>Cheers,<br>Rémi-C<br></div><div class="gmail_extra"><br><div class="gmail_quote">2014-10-14 13:39 GMT+02:00 Pietro Rossin <span dir="ltr"><<a href="mailto:pierigis@gmail.com" target="_blank">pierigis@gmail.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi all<br>
I'm trying to write a trigger function to automatically populate data for<br>
some fields based on spatial relationships<br>
I want to control if the new point falls inside a set of polygons<br>
(monitoraggio_acque.corpi_idrici_cw_tw) and if positive I want to take a<br>
value to be written in "cod_ci_sup" from there..<br>
<br>
If the added point falls externally to the previous polygons I want to take<br>
values from other postgis tables, based on distance and overlap relationship<br>
<br>
<br>
This is the trigger:<br>
*******************<br>
CREATE TRIGGER settavaloriautomaticiscarichi<br>
  BEFORE INSERT<br>
  ON catasto_scarichi.aa_impianti_pn<br>
  FOR EACH ROW<br>
  EXECUTE PROCEDURE<br>
catasto_scarichi.setta_valoriautomaticiscarichi_function();<br>
*******************<br>
<br>
And this is the relative funcion:<br>
<br>
*******************<br>
CREATE OR REPLACE FUNCTION<br>
catasto_scarichi.setta_valoriautomaticiscarichi_function()<br>
  RETURNS trigger AS<br>
$BODY$<br>
BEGIN<br>
        IF (SELECT NEW.geom&&b.geom from (select geom from<br>
monitoraggio_acque.corpi_idrici_cw_tw) b where st_contains(b.geom,<br>
NEW.geom))= 't' THEN<br>
        NEW.cod_ci_sup = (SELECT cod_corpo FROM<br>
monitoraggio_acque.corpi_idrici_cw_tw b WHERE NEW.geom&&b.geom and<br>
st_contains(b.geom, NEW.geom));<br>
        RETURN NEW;<br>
<br>
        ELSE<br>
        NEW.cod_asta_reg = (SELECT codice_fvg FROM<br>
idrologia.idrfvg_reteidrografica_l a WHERE st_dwithin (NEW.geom, a.geom,<br>
1000) ORDER BY st_distance(NEW.geom, a.geom) asc limit 1);<br>
        RETURN NEW;<br>
        end if;<br>
<br>
END;<br>
$BODY$<br>
  LANGUAGE plpgsql VOLATILE<br>
  COST 100;<br>
*******************<br>
<br>
If I try to add values from QGis 2.4 I don't get any message but no value is<br>
added..<br>
Where is my mistake??<br>
<br>
Thanks<br>
Pietro<br>
<br>
<br>
<br>
--<br>
View this message in context: <a href="http://postgis.17.x6.nabble.com/function-trigger-to-populate-values-based-on-spatial-relationships-tp5007143.html" target="_blank">http://postgis.17.x6.nabble.com/function-trigger-to-populate-values-based-on-spatial-relationships-tp5007143.html</a><br>
Sent from the PostGIS - User mailing list archive at Nabble.com.<br>
_______________________________________________<br>
postgis-users mailing list<br>
<a href="mailto:postgis-users@lists.osgeo.org">postgis-users@lists.osgeo.org</a><br>
<a href="http://lists.osgeo.org/cgi-bin/mailman/listinfo/postgis-users" target="_blank">http://lists.osgeo.org/cgi-bin/mailman/listinfo/postgis-users</a><br>
</blockquote></div><br></div>