Export multiple raster from grass gis

1.6k Views Asked by At

I used r.tile (http://grass.osgeo.org/grass71/manuals/r.tile.html) to create tiles from a huge geotiff. Now i want to export each tile to a file but havent found an easy way: Using r.out.gdal I only can export one tile at a time which makes it unuseable as I have 100 tiles...

I havent found any other solution...anyone any ideas?

3

There are 3 best solutions below

1
On BEST ANSWER

You just call r.out.gdal in a for loop in some scripting language.

Here is a example for Bash. It is a simple but complete script. It expects the raster map names as command line parameters and if the parameters are not present it fails with a usage suggestion.

#!/usr/bin/env bash

if [ $# -eq 0 ]
then
    >&2 echo "No arguments supplied"
    >&2 echo "Usage: $0 raster1 raster2 ..."
fi

for RASTER in "$@"
do
    r.out.gdal input=$RASTER output=$RASTER.tiff format=GTiff
done

To run it you have to set it as executable (chmod u+x export.sh) first, then you can run it in the command line:

./export.sh tile1 tile2

Here is an example for Python which is much more convented for bigger tasks. The code does completely the same as the Bash script. Additionally, it uses GRASS Python Scripting Library to call the r.in.gdal module and print function to be forward compatible with Python 3.

#!/usr/bin/env python

from __future__ import print_function
import sys
import grass.script as gscript

if len(sys.argv) == 1:
    print("No arguments supplied", file=sys.stderr)
    print("Usage: {} raster1 raster2 ...".format(sys.argv[0]), file=sys.stderr)

for raster in sys.argv[1:]:
    gscript.run_command('r.out.gdal', input=raster,
                        output=raster + '.tiff', format='GTiff')

Again, to run it you have to set it as executable (chmod u+x export.py) first, then you can run it in the command line:

./export.py tile1 tile2

Both examples assume you are using Linux, Mac OS X or something similar and running the script in system command line in GRASS GIS. On MS Windows, you should probably use the Python version and run it, for example from the GRASS GIS GUI through the Launch script item in the File menu in the Layer Manager.

0
On

The above answers require you to input the name of each raster layer. If you want to automate the whole process you can use the following Python script. This script first creates a list of all rasters that are available to grass, it then exports each as a Tiff file individually. To run it, either copy and paste the code into the GRASS python shell (accessible as a tab on the GRASS GUI layer manager window) or, save it as a .py file and, again from the Python shell, use the command execfile("foo.py").

import grass.script as grass

# create list of all rasters

rastlist=grass.read_command("g.list",type="rast")
rastlist1= rastlist.split(':', 1)[1]
rastlist2= rastlist1.split('-', 1)[0]
RastListClean=rastlist2.split()

# export all rasters

for raster in RastListClean:
    grass.run_command('r.out.gdal', input=raster,
                        output=raster + '.tiff', format='GTiff')
0
On

Working with GRASS 8.2 on macOS, this version works. The g.list() seems to return a string, separated perhaps by newlines.

split() breaks it into a python list that can be iterated. Note that this only works because GRASS does not allow spaces in object names.

#!/usr/bin/env python3

import grass.script as grass

rastlist=grass.read_command("g.list", type="rast")

rastlistClean=rastlist.split()

for raster in rastlistClean:
  grass.run_command('r.out.gdal', input=raster, output= raster + '.tif', format='GTiff', overwrite=True)  

Credit to Beeman's answer which is the basis for this one.