I have a problem, the grass 7 python shell can't find my rasters. When I try to use the python shell to set the region I get "region not found". I can set the region in the grass shell (and other tools I use can read the images from the grass shell), but I'm trying to develop a looping python code for a big part of my master's thesis and stop on this problem. I have the same problem on two computers, I'm using grass 7 (because of i.segment) on windows vista

The code I type into the python shell is: grass.run_command('g.region', region='xKM_430_420.1').

Am I typing the code in the wrong way? Should I use another command to set the region when using python? Or is it maybe some environmental variables?

Any ideas?


As you are working with GRASS 7, you can use (look at Workshop pygrass: modules):

1) grass.scriptwith the run_command(), read_command(),parse_command() functions or in pure Python

from grass.script import core as grass  
region = grass.parse_command('g.region', flags='p')
{'ellipsoid:  international': None, 'zone: 0': None, 'north:131321.2037345': None, 'east:164555.24362815': None, 'cols: 30426': None, 'cells:354615030': None, 'nsres:      2.11689968': None, 'south: 106648.73800047': None, 'projection: 99 (Lambert Conformal Conic)': None, 'west:100152.80214237': None, 'datum: bel72': None, 'ewres:    2.11669104': None, 'rows:11655': None}
# or
region = grass.region()
{'rows': 11655, 'e': 164555.24362815, 'cells': 354615030, 'cols': 30426, 'n': 131321.20373450001, 's': 106648.73800047, 'w': 100152.80214237, 'ewres': 2.1166910400000001, 'nsres': 2.11689968}

For setting a region, look at Set Grass Region in Python grass.script

List of rasters

g.parse_command("g.list", _type="rast")
# or

2) pygrassin pure Python (look at pygrass or Workshop pygrass: GIS objects)

from grass.pygrass.modules.shortcuts import general as g
# an then call directly the module
g.region(flags='p') # and you can adapt the solution given above 
# or
from grass.pygrass.gis.region import Region
reg = Region()
[('proj', 99), ('zone', 0), ('north', 131321.20373450001), ('south', 106648.73800047), ('west', 100152.80214237), ('east', 164555.24362815), ('top', 1.0), ('bottom', 0.0), ('nsres', 2.1168996768794526), ('ewres', 2.1166910368033918), ('tbres', 1.0), ('rows', 11655), ('cols', 30426), ('cells', 354615030)]

List of rasters

rasters  = g.mlist(type='rast')
# or
from grass.pygrass.gis import Mapset
m = Mapset()
  • Thank you! This was really useful, I finally managed to type something into the python shell that worked. Both ways of calling the list of rasters worked and now I can set computational region using grass.read_command('g.region', flags='p', rast='xKm_440_430.1'). – Adam H Oct 13 '14 at 10:12
  • grass.read_command('i.segment') works as well. I am extremely thankful for this – Adam H Oct 13 '14 at 10:54
  • BTW: there is new documentation available: grasswiki.osgeo.org/wiki/… – markusN Oct 20 '14 at 12:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.