2

Is there anyway I can input a raster and number and then use the SAGA algorithm "Fill Sinks (Wang and Liu)" on the raster through an IDE such as PyCharm?

How would I go about calling the algorithm?

What I want to do is write code in Python that inputs a raster and then outputs it by calling a function from the SAGA toolkit.

  • What have you tried so far? In terms of setting PyCharm up, using processing.run('saga:etc...', {'INPUT':etc}, 'ARGS etc...')? Have you run it from the toolbox GUI and then copied the python syntax from the history? – user2856 Aug 3 '18 at 3:26
  • I have been trying to import the necessary tools. I can't get past import qgis and import processing. I have tried several tutorials but it just won't detect those libraries. – Suspectful Aug 3 '18 at 14:24
  • Please edit your question to add additional information, including exactly what you did, not just "followed tutorials". – user2856 Aug 3 '18 at 23:43
3

SAGA has a command-line interface, so once you have it installed and on your system path, you can access the tools directly using a command line call from Python (I do this all the time with R, the process is very similar). Here's an example of calling the help function for SAGA:

from subprocess import call
call(['saga_cmd', '-h'])

and here's the help for the tool you want to use:

call(['saga_cmd', 'ta_preprocessor', '4'])

You should be able to figure out the sequence of arguments to build the full command from there. Note that all the processing happens outside of python, so you'll need to supply an *.sgrd file as input and the tool will write another one (or more) to disk.

  • 2
    tip: to get rasters into the saga-specific .sgrd format, use saga_cmd io_gdal 0. You can import any GDAL supported raster format. – Steven Kay Aug 4 '18 at 18:41
2

I would just call the command line argument via os.system. Here is an example of running SAGA's Gaussian Filter tool via Python and os.system:

import os

input_file = "/some/raster.sgrd"
output_file = "/some/other_raster.sgrid"

os.system("saga_cmd grid_filter 1 -INPUT '" + input_file + "' -RESULT '" + output_file + "' -SIGMA 1.0 -MODE 1 -RADIUS 5")

That is the same as running the following on the command line:

saga_cmd grid_filter 1 -INPUT '/some/raster.sgrd' -RESULT '/some/other_raster.sgrd' -SIGMA 1.0 -MODE 1 -RADIUS 5
  • Which program do I run the first set of code through? I have never used os.system before. – Suspectful Aug 3 '18 at 14:26
  • That is Python code. It would be typed in a script and run. Or, it will run from your IDE. – Jackson Aug 4 '18 at 2:09
  • Hey, when I try to run the code, I get the error: "sh: saga_cmd: command not found." Any ideas on how to fix this? – Suspectful Aug 6 '18 at 17:42
  • Ensure two things: 1) SAGA is installed; 2) the saga_cmd excecutable is in your PATH. – Jackson Aug 7 '18 at 22:49
  • How do I go about making sure the saga_cmd executable is in my PATH? – Suspectful Aug 14 '18 at 17:41

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