[postgis-users] Re: Trigger in plpythonu

Pedro Martinez Arbizu pmartinez at senckenberg.de
Sun May 18 13:27:06 PDT 2008


Hello,
I found the solution, I didnt realize  that plpy.execute returns a
dictionary.
Works fine if anybody is interested:

CREATE OR REPLACE FUNCTION gis_pos_update()
  RETURNS "trigger" AS
$BODY$
long = TD["new"]["long"]
lat = TD["new"]["lat"]
new_geom = plpy.execute("select GeomFromText('POINT(%s %s)', -1) as geo;" %
(long, lat), 0)
TD["new"]["gis_pos"] = new_geom[0]['geo']
return "MODIFY"
$BODY$
  LANGUAGE 'plpythonu' VOLATILE;
ALTER FUNCTION gis_pos_update() OWNER TO pmartinez;


regards pedro




2008/5/18 Pedro Martinez Arbizu <pmartinez at senckenberg.de>:

> Hello I want to create a trigger with plplythonu
> that will take long and lat from columns "long" and "lat" and write/update
> into column "gis_pos" GeomFromText('POINT(long lat)') when "long" and/or
> "lat" are updated or inserted.
>
> I thought it would be as simple as:
>
> CREATE OR REPLACE FUNCTION update_gispos()
>   RETURNS "trigger" AS
> $BODY$
> TD["new"]["gis_pos"] = GeomFromText('POINT(TD["new"]["long"]
> TD["new"]["lat"])')
> return "MODIFY"
> $BODY$
>   LANGUAGE 'plpythonu' VOLATILE;
> ALTER FUNCTION update_gispos() OWNER TO pmartinez;
>
> This raise the error global name GeomFromText is unknown.
>
> FEHLER:  plpython: function "update_gispos" failed
> DETAIL:  exceptions.NameError: global name 'GeomFromText' is not defined
>
>
> How can I make the function GeomFromText available within the Trigger
> function?
>
> --
> I also tried to parse long and lat with string formating:
>
> CREATE OR REPLACE FUNCTION update_gispos()
>   RETURNS "trigger" AS
> $BODY$
> TD["new"]["gis_pos"] = "GeomFromText('POINT(%s %s)')" %(TD["new"]["long"],
> TD["new"]["lat"])
> return "MODIFY"
> $BODY$
>   LANGUAGE 'plpythonu' VOLATILE;
> ALTER FUNCTION update_gispos() OWNER TO pmartinez;
>
>
> Which obviously raise an error, I guess because geometry cannot be a
> string:
> FEHLER:  parse error - invalid geometry
>
> I would appreciate any help.
>
> pedro
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/postgis-users/attachments/20080518/13b3c341/attachment.html>


More information about the postgis-users mailing list