I'm writing a python script in which I want to import some grass functions. To test the script I used a test command like r.null, but the code returns an error before that. The procedure I'm using is shown in this example:
import os
from grass_session import Session
from grass.script import core as gcore
import grass.script as gscript
import grass.script.setup as gsetup
gisdb = 'path/to/Grass_session'
inputFile = os.path.join(gisdb,'elevation.tif')
outputFile = os.path.join(gisdb,'output_elevation.tif')
location_name = 'Work_dir'
mapset = 'mapset'
PERMANENT = Session()
PERMANENT.open(gisdb=gisdb, location=location_name, mapset=mapset,
create_opts='EPSG:32632')
west = 1083988.1464839160908014
south = 4535980.4144736304879189
east = 1163008.1464839160908014
north = 4582000.4144736304879189
gscript.run_command('g.region', n=4582000.4144736304879189, s=4535980.4144736304879189, e=1163008.1464839160908014, w=1083988.1464839160908014, ewres=20, nsres=20, verbose=True, flags='p')
gscript.run_command('r.in.gdal', input = inputFile,
output= outputFile, overwrite = True)
gscript.run_command("r.null", map = 'elevation.tif', setnull = 0)
The error is:
GRASSBIN: grass74
GISBASE: /usr/lib/grass74
ERROR: Unable to open element file <> for <DEFAULT_WIND@PERMANENT>
Traceback (most recent call last):
File "test.py", line 27, in <module>
gscript.run_command('g.region', n=4582000.4144736304879189, s=4535980.4144736304879189, e=1163008.1464839160908014, w=1083988.1464839160908014, ewres=20, nsres=20, verbose=True, flags='p')
File "/usr/lib/grass74/etc/python/grass/script/core.py", line 421, in run_command
return handle_errors(returncode, returncode, args, kwargs)
File "/usr/lib/grass74/etc/python/grass/script/core.py", line 332, in handle_errors
returncode=returncode)
File "/usr/lib/grass74/etc/python/grass/exceptions/__init__.py", line 68, in __init__
msg = _("Module run %s %s ended with error") % (module, code)
File "/usr/lib/python2.7/gettext.py", line 369, in lgettext
return tmsg.encode(locale.getpreferredencoding())
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe8' in position 30: ordinal not in range(128)
I was able to use the r.region and r.ingdal commands without error, but I get an error with the r.null command:
import os
from grass_session import Session
from grass.script import core as gcore
import grass.script as gscript
import grass.script.setup as gsetup
gisdb = 'path/to/Grass_session'
inputFile = os.path.join(gisdb,'elevation.tif')
outputFile = os.path.join(gisdb,'output_elevation.tif')
location_name = 'Work_dir'
mapset = 'PERMANENT'
PERMANENT = Session()
PERMANENT.open(gisdb=gisdb, location=location_name, mapset=mapset,
create_opts='EPSG:32632')
gscript.run_command('g.region', n=4588802.4653103761374950, s=4529242.1501442901790142, e=1167158.4690597453154624, w=1080258.9182251752354205, ewres=20, nsres=20, verbose=True, flags='p')
gscript.run_command('r.in.gdal', overwrite = True, input = inputFile, output= 'output_elevation.tif')
gscript.run_command("r.null", map = 'elevation.tif', setnull = 0)
The error is:
GRASSBIN: grass74
GISBASE: /usr/lib/grass74
path/to/Grass_session/elevation.tif
projection: 1 (UTM)
zone: 32
datum: wgs84
ellipsoid: wgs84
north: 4588802.46531
south: 4529242.15014
west: 1080258.91823
east: 1167158.46906
nsres: 20.00010583
ewres: 19.99989662
rows: 2978
cols: 4345
cells: 12939410
WARNING: Raster map <output_elevation.tif> already exists and will be
overwritten
Importing raster map <output_elevation.tif>...
100%
ERROR: Raster map <elevation.tif> not found
Traceback (most recent call last):
File "test.py", line 31, in <module>
gscript.run_command("r.null", map = 'elevation.tif', setnull = 0)
File "/usr/lib/grass74/etc/python/grass/script/core.py", line 421, in run_command
return handle_errors(returncode, returncode, args, kwargs)
File "/usr/lib/grass74/etc/python/grass/script/core.py", line 332, in handle_errors
returncode=returncode)
File "/usr/lib/grass74/etc/python/grass/exceptions/__init__.py", line 68, in __init__
msg = _("Module run %s %s ended with error") % (module, code)
File "/usr/lib/python2.7/gettext.py", line 369, in lgettext
return tmsg.encode(locale.getpreferredencoding())
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe8' in position 30: ordinal not in range(128)
PERMANENT
, but you create onlymapset='mapset'
. Following their example, try working inmapset='PERMANENT'
first. Does that help?