[GIS] Automating non-trivial workflow in SAGA-GIS

saga

My problem: I have two DEMs, one rectangular and containing the other. Both are given in grid formats and the outer one has gridsize 50m, the inner one 10m. I want to 'embed' the smaller one in the larger one, merge them and export the whole merged grid as .xyz files with grid size 50m.

All this I can do manually in SAGA-GIS, using the following steps:

  1. Import the outer grid using the module "io_grid" – "Import Surfer Grid" (ID 3)
  2. Import the inner grid using "io_gdal" – "GDAL: Import Raster" (ID 0)
  3. Use the splendid mosaicking tool "grid_tools" – "Mosaicking" (ID 3)
  4. Export the resulting grid using "io_grid" – "Export Grid to XYZ" (ID 5)

Now I want to automate all this using saga_cmd. I managed to turn the first two steps into a script quite easily:

io_grid 3 -FILE=G:\path_to_file\foo.grd -NODATA=0 -NODATA_VAL=-99999.000000
io_gdal 0 -FILES=G:\path_to_file\bar.adf

Step 3 is tricky though. The SAGA-generated example for the module grid_tools 3 suggests to do something like that:

grid_tools "Mosaicking" -GRIDS=memory;memory -TYPE=7 -INTERPOL=0 -OVERLAP=1 -BLEND_DIST=10.000000 -TARGET=1 -USER_XMIN=0.000000 -USER_XMAX=0.000000 -USER_YMIN=0.000000 -USER_YMAX=0.000000 -USER_SIZE=1.000000 -USER_COLS=0 -USER_ROWS=0 -USER_GRID=NULL -GRID_GRID=

In the SAGA GUI, I simply choose the input grids manually in this step (with a drop-down menu – interactively). But it seems to me that I have to name the imported grids, which I did not do in the script. But how do name them. And how can I tell grid_tools "Mosaicking" to use the two before-loaded grids?

Best Answer

Typing saga_cmd io_grid 3 and cutting the header:

Usage: saga_cmd [-GRID <str>] [-FILE <str>] [-NODATA <str>] [-NODATA_VAL <str>]
  -GRID:<str>           Grid
        Data Object (optional output)
  -FILE:<str>           File
        File path
  -NODATA:<str>         No Data Value
        Choice
        Available Choices:
        [0] Surfer's No Data Value
        [1] User Defined
        Default: 0
  -NODATA_VAL:<str>     User Defined No Data Value
        Floating point
        Default: -99999.000000

you will notice the -GRID optional parameter. It allows to specify the name of the SAGA grid to store on your disk and not in memory. The concept is quite the same for saga_cmd io_gdal 0.

So you need to adapt your commands, storing on your disk every output grid in SDAT (SAGA GIS Binary Grid File Format) format using the .sgrd extension.

saga_cmd io_grid 3 -GRID=G:\path_to_file\foo.sgrd -FILE=G:\path_to_file\foo.grd -NODATA=0 -NODATA_VAL=-99999.000000
saga_cmd io_gdal 0 -GRIDS=G:\path_to_file\bar.sgrd -FILES=G:\path_to_file\bar.adf
saga_cmd grid_tools 3 -GRIDS=G:\path_to_file\foo.sgrd;G:\path_to_file\bar.sgrd -TYPE=7 -INTERPOL=0 -OVERLAP=1 -BLEND_DIST=10.000000 -TARGET=1 -USER_XMIN=0.000000 -USER_XMAX=0.000000 -USER_YMIN=0.000000 -USER_YMAX=0.000000 -USER_SIZE=1.000000 -USER_COLS=0 -USER_ROWS=0 -USER_GRID=mosaic -GRID_GRID=
saga_cmd io_grid 5 -GRID=G:\path_to_file\mosaic.sgrd -FILE=G:\path_to_file\mosaic.xyz

Finally, a useful remark from How to create a python script for saga:

Instead of running through the GUI or the command line you can also run modules from python. Compared to the saga cmd interface this has the advantage that you don't have to save all intermediate results, apart from the fact that you can use all the useful features of python to eg iterate over a number of files.