2

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?

asked Oct 12, 2014 at 0:09

1 Answer 1

3

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()
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
grass.list_strings('rast')

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()
reg.items()
[('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()
m.glist('rast')
answered Oct 12, 2014 at 12:35
3
  • 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'). Commented Oct 13, 2014 at 10:12
  • grass.read_command('i.segment') works as well. I am extremely thankful for this Commented Oct 13, 2014 at 10:54
  • BTW: there is new documentation available: grasswiki.osgeo.org/wiki/… Commented Oct 20, 2014 at 12:08

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.