Speed up R sripts – external program (RSAGA)

What if all the features we have tested/written do not allow us to get satisfactory execution times for the intended task? We can use other programs that have functions we are interested in and have a library implemented in R.

We will show you how to do this using old familiar Zonal Statistics, which we have been using for some time. To process our data, we will use another open source software, namely SAGA GIS (http://www.saga-gis.org).

For this we need the RSAGA library:

The library requires the program SAGA-GIS installed on our computer. By default, it looks for access to it under the paths:

If you install the library in a different location, it will be searched everywhere on the disk. To speed up the whole process, we can specify the path to the SAGA-GIS environment itself:

Let’s verify that we created the environment correctly by displaying the version of our SAGA+GIS:

SAGA- GIS allows data processing through its GUI (saga_gui.exe) and command line (saga_cmd.exe). The RSAGA library uses the command line. You can read how to create commands in the documentation for each version, available here.

In short, the tools are grouped into libraries. For each tool, we can define parameters. In our case, we will use the tool “Grid Statistics for Polygons” which is located in the library “shapes_grid”. In R, we will run saga_cmd using the rsaga.geoprocessor function:

, where:

lib – is the library with the tool we are interested in

module – is a sequence number of our tool

param – is a list of parameters of the tool

To see what our command should look like, it is best to look at the documentation. At the end of each tool description there is an example of a command:

http://www.saga-gis.org/saga_tool_doc/7.0.0/shapes_grid_2.html

After running the command, you will see a vector file with associated values of raster statistics in the location specified in RESULT. Let’s load these:

And let’s check the statistics:

The RSAGA library has some SAGA-GIS tools implemented directly in R that can be used without detailed knowledge of the documentation. One such function is rsaga.contour for generating contours. We leave the familiarization with these functions to you.

In the next entry, we will show you how to use functions from other programs in your code that do not yet have a library in R.

Leave a Reply

Your email address will not be published.

Translate using Google Translate»
Social media & sharing icons powered by UltimatelySocial

Podoba Ci się nasza strona? Odwiedź nasz profil