exportgistilegrass

Export multiple raster from grass gis


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?


Solution

  • 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.