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

Andrew MacIntyre Andrew.MacIntyre at acma.gov.au
Wed Jul 18 17:53:30 PDT 2012


I suggest running a test script that reports sys.path to confirm that the actual Python module search path seen by the Postgres server's environment is as expected.

-------------------------> "These thoughts are mine alone!" <---------
Andrew MacIntyre           Operations Branch
tel:   +61 2 6219 5356     Communications Infrastructure Division
fax:   +61 2 6253 3277     Australian Communications & Media Authority
email: andrew.macintyre at acma.gov.au            http://www.acma.gov.au/

> Message: 5
> Date: Wed, 18 Jul 2012 22:58:59 +1000
> From: Mark Wynter <mark at dimensionaledge.com>
> To: grass-user at lists.osgeo.org
> Subject: [GRASS-user] Problem accessing GRASS from
> 	PostgreSQL9.1/PostGIS2.0	via pg-python or plpythonu
> Message-ID: <4D8125BB-F64C-4D07-8D4B-
> 69C64060CB7E at dimensionaledge.com>
> Content-Type: text/plain; charset="utf-8"
> 
> 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.html>
> 
> ------------------------------
> 
> _______________________________________________
> grass-user mailing list
> grass-user at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/grass-user
> 
> 
> End of grass-user Digest, Vol 75, Issue 31
> ******************************************

NOTICE: This email message is for the sole use of the intended recipient(s) 
 and may contain confidential and privileged information. Any unauthorized 
 review, use, disclosure or distribution is prohibited. If you are not the 
 intended recipient, please contact the sender by reply email and destroy all 
 copies of the original message.


More information about the grass-user mailing list