1

I'm having trouble accessing files from my PERMANENT mapset in the Python code editor inside GRASS.

I am able to access the raster I would like to set my projection to from the console, but for some reason running the equivalent command on the Python code editor reports that the file doesnt exist.

After tracing where the file is in grassdata, I found it to be in the 'grassdata\nc_spm_08_grass7\PERMANENT\windows' directory while my location is set to 'grassdata\nc_spm_08_grass7\lab7'.

I am able to access other rasters that appear in the output of g.list type=raster from the Python editor but I can't see anything from my PERMANENT mapset. I have also checked that the PERMANENT mapset is accessible.

g.region rural_1m -p
projection: 99 (Lambert Conformal Conic)
zone:       0
datum:      nad83
ellipsoid:  a=6378137 es=0.006694380022900787
north:      220750
south:      220000
west:       638300
east:       639000
nsres:      1
ewres:      1
rows:       750
cols:       700
cells:      525000
#!/usr/bin/env python3

import grass.script as gscript


def main():
    gscript.run_command('g.region', raster='rural_1m', flags='p')


if __name__ == '__main__':
    main()
ERROR: Raster map <rural_1m> not found

1 Answer 1

1

I cannot confirm this. I tested in a local mapset, and set the region to a raster map from PERMANENT, with no problem. Here's my code copied from the "simple editor":

#!/usr/bin/env python3

import grass.script as gs

def main():
    m = gs.parse_command("g.mapset", flags="p")
    r_dict = gs.parse_command("g.list", type="rast", mapset="PERMANENT") 
    r_names = [r for r in r_dict]
    r = r_names[2]
    # Make sure what mapset the chosen raster is in
    r_mapset = gs.parse_command("g.list", type="rast", pattern=r, flags="m")
    reg = gs.parse_command('g.region', raster=r, flags='p')
    print("Current mapset:  {}".format(m))
    print("Chosen raster: {}".format(r_mapset))
    print("Region settings: {}".format(reg))

if __name__ == '__main__':
    main()

and here's the console output:

(Sat Mar  5 17:09:06 2022)                                                      
/home/micha/GIS/grass/ITM/Arava/.tmp/RMS/5469.0.py                              
Current mapset:  {'Arava': None}
Chosen raster: {'landuse_2014_3@PERMANENT': None}
Region settings: {'projection: 99 (Israel 1993 / Israeli TM Grid)': None, 'zone:       0': None, 'datum:      towgs84': '-48,55,52,0,0,0,0', 'ellipsoid:  grs80': None, 'north:      804595': None, 'south:      377795': None, 'west:       130135': None, 'east:       284135': None, 'nsres:      100': None, 'ewres:      100': None, 'rows:       4268': None, 'cols:       1540': None, 'cells:      6572720': None}
(Sat Mar  5 17:09:07 2022) Command finished (0 sec)                           

                    

You can also list the regions that were defined in the mapset (they appear in the windows subdirectory) then set the current computational region to one of those named regions. For example:

#!/usr/bin/env python3    
import grass.script as gs

def main():
    reg_dict = gs.parse_command('g.list', type="region")
    print([reg for reg in reg_dict])
    gs.run_command("g.region", region="arava_basins", flags="p")
 
if __name__ == '__main__':
    main()

Referring back to your original question, I see that the CLI command:

g.region rural_1m -p

worked. This is due to g.region assuming that the first argument is a named region. If rural_1m was a raster that command would have failed, with the message "Raster named rural_1m not found" (as it did when you ran the command in python. To set a region to a raster (or vector) extent you have to say explicitly:

g.region -p rast=<the raster map name>
3
  • Hi Micha thanks for trying to replicate the problem. When trying to list the available rasters in my PERMANENT mapset using r_dict = gs.parse_command("g.list", type="rast", mapset="PERMANENT") I find a list of rasters in the grassdata\nc_spm_08_grass7\PERMANENT\cats. However, the raster I'm interested in is in grassdata\nc_spm_08_grass7\PERMANENT\windows. Not sure how I can access this from the simple python code editor. Commented Mar 6, 2022 at 4:17
  • Under the windows subdirectory are regions not rasters. See my edits to the answer above
    – Micha
    Commented Mar 7, 2022 at 17:02
  • Ahh ofcourse. Thank you so much. Commented Mar 8, 2022 at 0:34

Your Answer

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

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