1

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)
asked Jun 29, 2018 at 13:20
1
  • 1
    It is looking for something in PERMANENT, but you create only mapset='mapset'. Following their example, try working in mapset='PERMANENT' first. Does that help? Commented Jun 29, 2018 at 15:48

2 Answers 2

1

[SOLVED] I found a solution. Below is the script I use:

from grass_session import Session
from grass.script import core as gcore
import grass.script as gscript
import grass.script.setup as gsetup
import os
import shutil
gisdb = os.getcwd()
inputFile = 'elevation.tif'
inputFile_path = os.path.join(gisdb, inputFile)
outputFile = '{}.out.tif'.format(inputFile)
print outputFile
outputFile_path = os.path.join(gisdb,outputFile)
location_name = 'Work_dir'
mapset = 'PERMANENT'
# clean location
shutil.rmtree(location_name)
PERMANENT = Session()
PERMANENT.open(gisdb=gisdb, location=location_name, mapset=mapset,
 create_opts='EPSG:32632')
n=4588802.4653103761374950
s=4529242.1501442901790142
e=1167158.4690597453154624
w=1080258.9182251752354205
gscript.run_command('g.region', n=n, s=s, e=e, w=w, ewres=20, nsres=20, verbose=True, flags='p')
gscript.run_command('r.in.gdal', overwrite = True, input = inputFile, output = outputFile) 
gscript.run_command("r.null", map = outputFile, setnull = 0)
answered Jun 30, 2018 at 12:06
1

You need to change this line

PERMANENT.open(gisdb=gisdb, location=location_name, mapset=mapset,

to this line:

PERMANENT.open(gisdb=gisdb, location=location_name,

Then it starts (didn't check the rest). It operated in the PERMANENT mapset then (which is the default mapset to have anyway).

A full example is available in the GRASS GIS Wiki here.

answered Jun 30, 2018 at 23:32

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.