2

I had to get used to GRASS the last two days because I can't set up my python IDE to work with my osgeo4w python and I find it incredibly clunky. I have a simple ascii raster - I think its origin is ESRI ArcGIS given the following header structure:

ncols         389
nrows         265
xllcorner     665140
yllcorner     4823160
cellsize      20
NODATA_value  -9999

All I want to do is a simple import with the GRASS python editor to perform simple raster analyes. None of the 'Import raster data' menu entries worked out (probably because I can't flag the input type?), nor with the python editor. I tried to use r.in.gdal:

import grass.script as grass
grass.run_command("r.in.gdal",
                input="AAIGrid_raster.asc",
                output="GRASS_raster.asc",
                flags="f")

But I don't know how to specify the import format (most likely AAIGrid), as the description here doesn't help me sufficiently (btw. I think there are errors too on that page). In case I'd use -f AAIGrid in the shell, how does that look in pythonian?

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
GeoEki
  • 437
  • 1
  • 4
  • 15

1 Answers1

3

Simply read the GRASS GIS documentation. Thus, r.in.gdal specify that the result with the flag 'f' is

List supported formats and exit

 grass.run_command("r.in.gdal",flags="f",input='/User/me/test.asc',output='test')
 Supported formats:
 GRASS (ro): GRASS Database Rasters (5.7+)
 PDF (rw): Geospatial PDF
 VRT (rw+): Virtual Raster
 GTiff (rw+): GeoTIFF
 ...
 AAIGrid (rw): Arc/Info ASCII Grid
 ...

Therefore, you don't need to specify that the original layer is an Arc/Info ASCII Grid (.asc), the format is detected automatically.

Simply use

grass.run_command("r.in.gdal",flags="o",input='/User/me/test.asc',output='test',overwrite=True)

or

grass.parse_command('r.in.gdal', flags='o', input='/User/me/test.asc',output='test', overwrite=True)

With:

-input = path/name of GDAL dataset to be imported
-output= name for output raster map (in Grass)
-flags="o" = Override projection check (use current location's projection)

You can also use r.import

grass.run_command("r.import",flags="o",input='/User/me/test.asc',output='test2',overwrite=True)

For the second question

(I'd also highly appreciate guides on how to set up my windows cmd so I can use python with GRASS bindings...).

There are many answers in GIS SE, in the GRASS GIS Python library documentation ( GRASS GIS Python scripting with script package), , in the GRASS GIS wiki (Accessing GRASS modules without starting a GRASS session), in ...

gene
  • 54,868
  • 3
  • 110
  • 187