[GRASS-user] Problem accessing GRASS from PostgreSQL9.1/PostGIS2.0 via pg-python or plpythonu

Mark Wynter mark at dimensionaledge.com
Wed Jul 18 05:58:59 PDT 2012


My requirement is to connect to GRASS from PostgreSQL9.1/PostGIS2.0 via the pg-python or plpythonu procedural language.   I'm wondering whether I have a permissions issue?

I'm running my stack on Ubuntu 12.04 LTS
I've  added the following environmental variables to /etc/bash.bashrc
export GISBASE=/usr/local/grass-6.4.3svn
export PATH=$PATH:$GISBASE/bin:$GISBASE/scripts:$GISBASE/lib
export PYTHONPATH=${PYTHONPATH}:$GISBASE/etc/python/
export PYTHONPATH=${PYTHONPATH}:$GISBASE/etc/python/grass
export PYTHONPATH=${PYTHONPATH}:$GISBASE/etc/python/grass/script
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$GISBASE/lib
export GIS_LOCK=$$
export GISRC=$HOME/.grassrc6

If I log out and log in again, the following environment variables are now available to the postgres user:

ubuntu at ip-10-252-74-140:~$ sudo su postgres
postgres at ip-10-252-74-140:/home/ubuntu$ echo $GISBASE
/usr/local/grass-6.4.3svn
postgres at ip-10-252-74-140:/home/ubuntu$ echo $PYTHONPATH
:/usr/local/grass-6.4.3svn/etc/python/:/usr/local/grass-6.4.3svn/etc/python/grass:/usr/local/grass-6.4.3svn/etc/python/grass/script
postgres at ip-10-252-74-140:/home/ubuntu$ echo $HOME
/var/lib/postgresql


if I call the function "import grass.script as grass" from the python console, there is no error.   All seems ok.

postgres at ip-10-252-74-140:/$ python
Python 2.7.3 (default, Apr 20 2012, 22:39:59) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import grass.script as grass
>>> 

If I now log into PostgreSQL/PostGIS via psql, and try to run the same command within in a postgresql plpythonu or pg-python function, I get the following error messages.
psql -d test

CREATE OR REPLACE FUNCTION pythontest() RETURNS text AS
$$
import grass.script as grass
$$
LANGUAGE 'plpythonu' VOLATILE;

test=# select pythontest();
ERROR:  ImportError: No module named grass.script
CONTEXT:  Traceback (most recent call last):
  PL/Python function "pythontest", line 3, in <module>
    import grass.script as grass
PL/Python function "pythontest"
test=# 

I've also tried the pg-python language - in case it had something to do with plpythonu:

CREATE OR REPLACE FUNCTION pgpython_test()
RETURNS text LANGUAGE python AS
$python$
import Postgres
import grass.script as grass
def main():
    return ("Hello World")
$python$;


test=# select pgpython_test();
ERROR:  could not load Python function's module object
CONTEXT:  [exception from Python]
Traceback (most recent call last):
   File "public.pgpython_test()", line 3, in <module>
 ImportError: No module named grass.script

[public.pgpython_test() while loading function module]
test=# 

I can however access GRASS from PostgreSQL9.1/PostGIS2.0 via pl/r and R.  For example, this function works...
CREATE OR REPLACE FUNCTION generate_viewshed() RETURNS text as 
$$
library(spgrass6)
#initialise GRASS.
initGRASS(gisBase = “/usr/local/grass-6.4.3svn/”, home = tempdir(), gisDbase = “/usr/local/share/grassdata/”, location = “aust_dem_1sec_wgs84″, mapset = “postgres”, SG=”aust_dem_1sec”, override = TRUE)
#define the computational region for the analysis – limiting the region to an area slightly greater than the max_distance from the observation point.
execGRASS(“g.region”, parameters = list(n=”-34.9759927736275″, s=”-34.9985728860995″, e=”138.641896696527″, w=”138.619947232807″, align=”aust_dem_1sec”))
#run the viewshed module – all of these parameters can be set dynamically and parsed as plr function arguments – the output is a raster file.
execGRASS(“r.viewshed”, parameters = list(input = “aust_dem_1sec”, output = “viewshed_raster”, coordinate = “138.630922,-34.987283″, obs_elev = 25, max_dist = 1000), flags = c(“b”, “overwrite”)) 
#convert the raster output to a vector.
execGRASS(“r.to.vect”, parameters = list(input = “viewshed_raster”, output = “viewshed_vector”, feature = “area”), flags = c(“overwrite”)) 
#write the vector data back to PostGIS.
execGRASS(“v.out.ogr”, parameters = list(input = “viewshed_vector”, dsn = “PG:host=localhost dbname=test user=postgres password=password”, olayer = “viewshed_vector”, format = “PostgreSQL”, type = “area”), flags = c(“c”)) 
$$
LANGUAGE ‘plr’;

In terms of GRASS permissions...
postgres at ip-10-252-74-140:/home/ubuntu$ vi $HOME/.grassrc6
GISDBASE: /usr/local/share/grassdata
LOCATION_NAME: aust_dem_1sec_wgs84
MAPSET: postgres
GRASS_GUI: text
~                  

This is as far as I've got and need some help to connect to GRASS from PostgeSQL/PostGIS via python (as an alternative to pl/r).
Is there something obvious I have missed?  Could someone please guide me through to a solution.

With thanks and regards

Mark





















-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/grass-user/attachments/20120718/0778037e/attachment-0001.html>


More information about the grass-user mailing list