[R-sig-Geo] RSAGA issues

Rainer Hurling rhurlin at gwdg.de
Mon Oct 29 19:27:44 CET 2012


On 29.10.2012 16:18 (UTC+2), Ludwig Hilger wrote:
> Dear list, 
> 
> TY for answering.
> The first (Mr. Hurling) and the second (Input grids in the form of GRIDS =
> "t_asp_class.sgrd;t_elev_class.sgrd" helped) issue are solved now.
> I am still fighting with the resampling grid module and I have not found a
> solution in the archives and got none on sourceforge. So here it is again,
> hopefully explained a little better now:
> 
> 1. When I resample a grid, I have to provide USER_YMIN, USER_XMIN, USER_XMAX
> and USER_YMAX. Is there a way to automatically calculate these values from
> the input DEM header and the cellsize selected for
> resampling (as SAGA seems to do in the GUI)? 

Hi Ludwig,

one possibility to calculate USER_XMIN etc. is, to use the values from
the header data of the .sgrd file, extracted for example like described
in my previous mail (see below).

In my example with the 'header' file you could use the (numerical)
values after 'POSITION_[XY]MIN', 'CELLSIZE' and 'CELLCOUNT_[XY]' to
compute the needed values:

  USER_XMIN:   POSITION_XMIN
  USER_XMAX:   POSITION_XMIN + CELLSIZE * CELLCOUNT_X
  USER_YMIN:   POSITION_YMIN
  USER_YMAX:   POSITION_YMIN + CELLSIZE * CELLCOUNT_Y

Depending of the method you read the .sgrd file, you eventually have to
transform the strings to get numbers.

>From this view my header object is a bit to complicated. If you want to
give it a try you could use something like

position_xmin <-
  as.numeric(as.character(unlist(subset(header, Parameter %in%
  "POSITION_XMIN", Value))))

I guess there are much more elegant ways to extract the header data
directly into your wanted formats and places ;)


> 2. And if I copy the values from the SAGA GUI (after typing in the new
> cellsize, the other four values in the GUI dialoge change), it is still not
> working!

The text based version 'saga_cmd' is a bit pedantic. When I try your
code I have to change from 'GRID_GRID' to 'USER_GRID', because you are
using 'TARGET=0' (which means user defined).

So this should work:

rsaga.geoprocessor(lib = "grid_tools", module = 0,
                   param = list(
                     INPUT = "mydem.sgrd",
                     USER_GRID = "DEM_resampled.sgrd",
                     KEEP_TYPE = TRUE,
                     TARGET = 0,
                     SCALE_UP_METHOD = 6,
                     SCALE_DOWN_METHOD = 4,
                     USER_SIZE = res,  # res = 50
                     USER_XMIN = 24285,
                     USER_XMAX = 35835,
                     USER_YMIN = 186945,
                     USER_YMAX = 197795
                     ))


Hope this helps,
Rainer


> This code: 
> setwd("My working directory")
> rsaga.geoprocessor(lib = "grid_tools", module = 0, param = list(INPUT =
> "mydem.sgrd", GRID_GRID = "DEM_resampled.sgrd", KEEP_TYPE = TRUE, TARGET =
> 0, SCALE_UP_METHOD = 6, SCALE_DOWN_METHOD = 4,
> USER_SIZE = res,          # new cellsize read by scan(), eg 50
> USER_XMIN = 24285,     # should be automatically calculated from input file
> header and new cellsize
> USER_YMIN = 186945,   # should be automatically calculated from input file
> header and new cellsize
> USER_YMAX = 197795,  # should be automatically calculated from input file
> header and new cellsize
> USER_XMAX = 35835))  # should be automatically calculated from input file
> header and new cellsize            
> 
> gives:
> Grid system: 10; 1158x 1086y; 24285x 186945y
> Grid: DEM_2006-2010
> Additional Grids: No objects
> Additional Grids: No objects
> Preserve Data Type: yes
> Target Grid: user defined
> 
> [...] (rsaga.get.usage("grid_tools", 0))
> 
> error: executing module [Resampling]
> Warnmeldung:
> Ausführung von Kommando '"C:\Program Files
> (x86)\SAGA-GIS_2.0.8/saga_cmd.exe" grid_tools 0 -INPUT
> "D:/PROSA_Daten/ALS/ALS_2006-2010/DEM_2006-2010.sgrd" -GRID_GRID
> "DEM_resampled.sgrd" -KEEP_TYPE  -TARGET "0" -SCALE_UP_METHOD "6"
> -SCALE_DOWN_METHOD "4" -USER_SIZE "50" -USER_XMIN "24285" -USER_YMIN
> "186945" -USER_YMAX "197795" -USER_XMAX "35835"' ergab Status 1
> 
> Thank you again!
> Ludwig
> 
> 
> 
> Rainer Hurling wrote
>> On 26.10.2012 09:44 (UTC+2), Ludwig Hilger wrote:
>>>
>>> Dear list,
>>>
>>> I am comparatively new to RSAGA and have come across three problems I
>>> have not been able solve by searching on the internet. Your help would
>>> be very much appreciated. This is SAGA 2.0.8 and R 2.15.1.
>>>
>>> 1. Is there a way to access the header of a *.sgrd without having to
>>> load the whole grid, i.e. not using read.sgrd()$header? I want to
>>> determine the cellsize of a *.sgrd from within r for informational
>>> purposes and I have 1 m grids with 62 km2.....
>>
>> The .sgrd file _is_ the header file of a SAGA GIS grid file. The grids
>> data is in .sdat instead. You can read these very small header files
>> (.sgrd) with several functions, for example
>>
>>
>> hd <- readLines("filename.sgrd", n=-1)
>>
>> # split strings in names and values parts
>> require(stringr)
>> hd <- unlist(str_split(hd, "\t= "))
>>
>> # make a dataframe from the header data
>> header <- data.frame(Parameter=hd[seq(1,length(hd),2)],
>>                      Value=hd[seq(2,length(hd),2)])
>>
>> # The contents of such header is like that
>> header
>> #         Parameter              Value
>> #1             NAME          DGM5_saga
>> #2      DESCRIPTION
>> #3             UNIT
>> #4  DATAFILE_OFFSET                  0
>> #5       DATAFORMAT              FLOAT
>> #6    BYTEORDER_BIG              FALSE
>> #7    POSITION_XMIN 3449220.0000000000
>> #8    POSITION_YMIN 5487020.0000000000
>> #9      CELLCOUNT_X               2907
>> #10     CELLCOUNT_Y               6538
>> #11        CELLSIZE      10.0000000000
>> #12        Z_FACTOR           1.000000
>> #13    NODATA_VALUE      -99999.000000
>> #14     TOPTOBOTTOM              FALSE
>>
>>
>> The other questions seem to be already answered on r-sig-geo and/or SAGA
>> GIS discussion forum (
>> http://sourceforge.net/p/saga-gis/discussion/790705/thread/77e3979c/ ).
>>
>> Hope this helps,
>> Rainer
>>
>>
>>> -------------------------------------------------------------------------------
>>>
>>>
>>> 2. I have tried to call the Grid sum module in the following way:
>>>     rsaga.geoprocessor(
>>>     lib = “grid_calculus”,
>>>     module = 8,
>>>     param = list(
>>>     GRIDS = list(“t_elev_class.sgrd”, “t_asp_class.sgrd”),
>>>     RESULT = “t_codes”
>>>             )
>>>     )
>>>
>>> I get the following error, i.e. arguments of the function seem to get
>>> mixed up:
>>>
>>> “library path: C:Program Files (x86)SAGA-GIS_2.0.8/modules
>>> library name: grid_calculus
>>> module name : Grids Sum
>>> author : O. Conrad (c) 2010
>>>
>>> Load grid: t_codes...
>>>
>>> failed
>>>
>>> error: Grid file could not be opened.
>>>
>>> error: input file t_codes
>>>
>>> error: empty input list GRIDS
>>> Usage: 8 -GRIDS -RESULT
>>> -GRIDS: Grids
>>> Grid list (input)
>>> -RESULT: Sum
>>> Grid (output)
>>>
>>> error: executing module Grids Sum
>>> Warnmeldung:
>>> Ausführung von Kommando '"C:Program Files
>>> (x86)SAGA-GIS_2.0.8/saga_cmd.exe" grid_calculus 8 -GRIDS
>>> "t_elev_class.sgrd" -RESULT "t_asp_class.sgrd" -GRIDS "t_codes"' ergab
>>> Status 1”
>>>
>>> I have also tried c() instead of list() for the GRID-argument, I have
>>> moeved GRID out of the param-list, etc. . In contrast,
>>> rsaga.grid.calculus() works fine. I seem to have the same problem with
>>> other modules as well, probably I do not provide the input grids in the
>>> right way?
>>>
>>> -------------------------------------------------------------------------------
>>>
>>>
>>> 3.
>>> a) When I resample a grid, I have to provide USER_YMIN, USER_XMIN,
>>> USER_XMAX and USER_YMAX. Is there a way to automatically calculate these
>>> values from the input DEM header and the cellsize selected for
>>> resampling (as SAGA seems to do in the GUI)?
>>> b) If I want to use a target grid as a model for the new grid, where is
>>> this to be specified? rsaga.get.usage("grid_tools", 0) gives me only the
>>> options GRID_GRID and USER_GRID (which are output arguments according to
>>> get.usage). There are no arguments for the path to the target grid.
>>>
>>> thank you,
>>> Ludwig



More information about the R-sig-Geo mailing list