[GRASS-user] mapcalc problem in python script!!
Johannes Radinger
JRadinger at gmx.at
Thu Feb 17 09:48:50 EST 2011
-------- Original-Nachricht --------
> Datum: Wed, 9 Feb 2011 01:37:43 +0000
> Von: Glynn Clements <glynn at gclements.plus.com>
> An:
> CC: grass-user at lists.osgeo.org
> Betreff: Re: [GRASS-user] mapcalc problem in python script!!
>
> Johannes Radinger wrote:
>
> > > > Somehow I didn't your answer via email, I just found it at
> > > > http://permalink.gmane.org/gmane.comp.gis.grass.user/38530
> > >
> > > For whatever reason, GMX doesn't like my IP address, and bounces my
> > > emails. The problem is compounded by a "feature" of the mailing list
> > > software which (by default) doesn't send messages to any subscriber
> > > whose email address appears in the To or CC fields (on the assumption
> > > that they'll already get a copy). You can (and probably should)
> > > disable this feature in your preferences (via the link at the bottom
> > > of the email).
> >
> > I don't know which feature to disable?!?
>
> Use the link at the bottom of the page to edit your subscription
> options. The option in question is labelled "Avoid duplicate copies of
> messages?", and should be set to "no".
Now I found it... :) Done!
Thank you
>
> > > grass.parser() needs to know the valid options for the script. These
> > > are specified by comments beginning with "#%". See any existing script
> > > for reference.
> > >
> > > Then, you need to run the script with the appropriate command line
> > > options. I'm assuming that isn't happening, because you should get an
> > > error if you provide an unrecognised option (and there are no
> > > recognised options).
> >
> > I added your comments "#%" in the script (see attachment). Is it the
> > only way to run the grass.mapcalc version in python with the
> > grass.parser? I run my script external, directly from IDLE (without
> > starting grass).
>
> That won't work. Starting GRASS sets several environment variables
> required by the GRASS libraries. If you want to be able to run GRASS
> commands from anywhere, you will have to configure the environment
> variables yourself.
>
> The following is a minimal setup for Linux:
>
> export GISBASE=/opt/grass-7.0.svn
> export GRASS_PAGER=cat
> export GRASS_PYTHON=python
> export GRASS_MESSAGE_FORMAT=silent
>
> export PATH="$GISBASE/bin:$GISBASE/scripts:$PATH"
> export LD_LIBRARY_PATH="$GISBASE/lib"
> export GRASS_LD_LIBRARY_PATH="$LD_LIBRARY_PATH"
> export PYTHONPATH="$GISBASE/etc/python:$PYTHONPATH"
>
> export GIS_LOCK=$$
> export GRASS_VERSION="7.0.svn"
>
> tmp=/tmp/grass6-"`whoami`"-$GIS_LOCK
> export GISRC="$tmp/gisrc"
> mkdir "$tmp"
> cp ~/.grassrc6 "$GISRC"
>
> On Windows, the paths will be different, and you need to set PATH
> rather than LD_LIBRARY_PATH for the libraries.
>
> The most important of the above are GISBASE, which must point to the
> top-level GRASS installation directory, and GISRC, which must point to
> a file containing settings for GISDBASE, LOCATION_NAME and MAPSET.
This environmental variables are already set like stated in
http://grass.osgeo.org/wiki/GRASS_and_Python#Creating_Python_scripts_that_call_GRASS_functionality_from_outside
in my case:
GISBASE= C:\GRASS6.5.SVN
GISRC= C:\Users\Johannes Radinger\.grassrc6
LD_LIBRARY_PATH= C:\GRASS6.5.SVN\lib
PATH= C:\GRASS6.5.SVN\etc;C:\GRASS6.5.SVN\etc\python;C:GRASS6.5.SVN\lib;C:\GRASS6.5.SVN\bin;C:\GRASS6.5.SVN\extralib;C:\GRASS6.5.SVN\msys\bin;C:\Program Files (x86)\Python26\ArcGIS10.0;
PYTHONLIB= C:\Program Files (x86)\Python26\ArcGIS10.0
PYTHONPATH= C:\GRASS6.5.SVN\etc\python
GRASS_SH= C:\GRASS6.5.SVN\msys\bin\sh.exe
Some of these variables also set in the Python Script as well, like:
gisbase = os.environ['GISBASE'] = "C:/GRASS6.5.SVN"
sys.path.append(os.path.join(os.environ['GISBASE'], "etc", "python"))
gisrc = gsetup.init('C:/GRASS6.5.SVN', 'C:\Users\Johannes Radinger\Documents\Grass GIS', 'Treene_Location', 'Treene')
But I still don't have any sucess...(either with and without this addtional environmental settings in the python script)
how should I proceed, how can I test if the variables are set correctly?
And as I already stated, other python-scripts than the mapcalc function are working (e.g. grass.run_command("r.thin",....) when I run the script from outside grass (with IDLE).
Any suggestions?
Attached there's again the mapcalc example script which isn't working.
Thank you.
Johannes
>
> --
> Glynn Clements <glynn at gclements.plus.com>
> _______________________________________________
> grass-user mailing list
> grass-user at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/grass-user
--
GMX DSL Doppel-Flat ab 19,99 Euro/mtl.! Jetzt mit
gratis Handy-Flat! http://portal.gmx.net/de/go/dsl
-------------- next part --------------
#!/usr/bin/env python
#
############################################################################
#
# MODULE: Modell
#
# AUTHOR(S): Johannes Radinger
#
# PURPOSE: Script generated by wxGUI Graphical Modeler.
#
# DATE: Tue Feb 01 14:00:50 2011
#
#############################################################################
#%Module
#% description: Drapes a color raster over a shaded relief map using d.his
#%End
#%option
#% key: upstream_shreve
#% type: string
#% gisprompt: new,cell,raster
#% description:
#% required : yes
#%ends
#%option
#% key: upstream_part
#% type: string
#% gisprompt: old,cell,raster
#% description:
#% required : yes
#%option
#% key: shreve
#% type: string
#% gisprompt: old,cell,raster
#% description:
#% required : yes
#%end
import sys
import os
import atexit
import grass.script as grass
import grass.script.setup as gsetup
# set mapset/location/dbase/gisbase
gisbase = os.environ['GISBASE'] = "C:/GRASS6.5.SVN"
sys.path.append(os.path.join(os.environ['GISBASE'], "etc", "python"))
gisrc = gsetup.init('C:/GRASS6.5.SVN', 'C:\Users\Johannes Radinger\Documents\Grass GIS', 'Treene_Location', 'Treene')
def cleanup():
pass
def main():
grass.mapcalc("$upstream_shreve = ${upstream_part}+${shreve}",
upstream_shreve = options['upstream_shreve'],
upstream_part = options['upstream_part'],
shreve = options['shreve'])
if __name__ == "__main__":
options, flags = grass.parser()
atexit.register(cleanup)
sys.exit(main())
More information about the grass-user
mailing list