[R-sig-Geo] rsaga esri wrapper

Alexander Brenning alexander.brenning at uwaterloo.ca
Sat Jun 8 23:16:02 CEST 2013


Hi Chuck,

thanks for catching this problem. Below is a corrected version of the rsaga.esri.wrapper function that works with multiple grids, which you specify as c("C_A_1HA","C_SLOPE") for example. (No semicolons - RSAGA takes care of this when passing the argument to saga_cmd.)

This bug affected only function calls that involved multiple in.* arguments, at least one of which has more than one element - my that's why it hasn't been caught earlier. (You will still get a warning because the wrapper function isn't smart enough to recognize that in.shapefile is not a grid. I'll put this on my to do list.)

On the other hand, it's always more transparent (and errors are tracked more easily) if you make the .asc to .sgrd conversions explicitly using rsaga.esri.to.sgrd and rsaga.sgrd.to.esri calls.

Btw, the next RSAGA release is coming up very soon, and I will include this fix in the release. We're currently testing RSAGA on Linux and Mac OS X. The new release will support SAGA GIS 2.1.0 (release candidates) (but will partly discontinue support of SAGA GIS 2.0.4).

Cheers
  Alex


Date: Sat, 8 Jun 2013 02:03:55 -0700 (PDT)
From: Chuck Bulmer <cebulmer at telus.net>
To: r-sig-geo at r-project.org
Subject: [R-sig-Geo] rsaga esri wrapper
Message-ID: <1370682234988-7583742.post at n2.nabble.com>
Content-Type: text/plain; charset=us-ascii

Hi all.

I am using the rsaga.esri.wrapper to add grid values to a shapefile. The
below code works great for a single grid, but I've made many attempts to add
additional grids to this command and I can't get it to work. Can someone
please provide the correct syntax??

Thanks R3.0.1 Win 7 Saga 2.08

Chuck


rsaga.esri.wrapper(rsaga.add.grid.values.to.points,in.shapefile="points_atts.shp",in.grid="C_A_1HA",out.shapefile="points_atts.shp",in.esri=TRUE,out.esri=TRUE,esri.workspace="D:\\BC_DSM\\grids",format=1,esri.extension="asc")

I've tried in.grids=c("C_A_1HA","C_SLOPE"),

semicolons etc etc but can't figure it out so far

--
View this message in context: http://r-sig-geo.2731867.n2.nabble.com/rsaga-esri-wrapper-tp7583742.html
Sent from the R-sig-geo mailing list archive at Nabble.com.


#### Corrected rsaga.esri.wrapper:

rsaga.esri.wrapper = function(fun, in.esri=TRUE, out.esri=TRUE, 
    env=rsaga.env(), esri.workspace=env$workspace,
    format="ascii", georef="corner", prec=5, esri.extension,
    condensed.res=TRUE, clean.up=TRUE, intern=TRUE, ...)
{
    in.res = NULL
    geoproc.res = NULL
    out.res = NULL
    format = match.arg.ext(format,choices=c("binary","ascii"),base=0,ignore.case=TRUE,numeric=TRUE)
    if (missing(esri.extension))
        esri.extension = c(".flt",".asc")[format+1]
    args = list(...)
    argnms = names(args)
    
    in.ok = TRUE
    if (in.esri) {
        wh = grep("^in\\.",names(args))
        if (length(wh)==0) {
            warning("'in.esri' is TRUE, but the geoprocessing function does not have an 'in.*' grid argument")
        } else {
            in.args = args[wh]
            in.res = rsaga.esri.to.sgrd(in.grids=set.file.extension(unlist(in.args),esri.extension),
                intern=intern, show.output.on.console=FALSE,
                out.sgrds=unlist(in.args), in.path=esri.workspace, env=env) # more args to geoproc
            if (!intern) in.ok = all(in.res==0)
        }
    }
    
    geoproc.ok = TRUE
    if (in.ok) {
        geoproc.res = fun(env=env,intern=intern,...)
        if (!intern) geoproc.ok = all(geoproc.res==0)
    }
    if (clean.up) {
        del.files = set.file.extension(in.args,"")
        del.files = unlist(lapply(as.list(del.files), function(x) paste(x,c("sgrd","hgrd","sdat","mgrd"),sep="")))
        unlink(del.files)
    }
    
    out.ok = TRUE
    if (out.esri & in.ok & geoproc.ok) {
        wh = grep("^out\\.",names(args))
        if (length(wh)==0) {
            warning("'out.esri' is TRUE, but the geoprocessing function does not have an 'out.*' grid argument")
        } else {
            out.args = args[wh]
            out.res = rsaga.sgrd.to.esri(in.sgrds=unlist(out.args),
                out.grids=set.file.extension(out.args,unlist(esri.extension)),
                out.path=esri.workspace, env=env, intern=intern, show.output.on.console=FALSE,
                format=format, georef=georef, prec=prec) # more args to geoproc
            if (!intern) out.ok = all(out.res==0)
            if (clean.up) {
                del.files = set.file.extension(out.args,"")
                del.files = unlist(lapply(as.list(del.files), function(x) paste(x,c("sgrd","hgrd","sdat","mgrd"),sep="")))
                unlink(del.files)
            }
        }
    }

    res = list( in.res=in.res, geoproc.res=geoproc.res, out.res=out.res )
    if (condensed.res) {
        if (intern) {
            res = geoproc.res
        } else   res = max(abs(unlist(res)))
    }
    if (intern) {
        invisible(res)
    } else  return( res )
}



--
Alexander Brenning
brenning at uwaterloo.ca - T +1-519-888-4567 ext 35783
Department of Geography and Environmental Management
University of Waterloo
200 University Ave. W - Waterloo, ON - Canada N2L 3G1
UW: https://uwaterloo.ca/gem/people-profiles/alexander-brenning
Academia.edu: http://uwaterloo.academia.edu/AlexanderBrenning
LinkedIn: http://ca.linkedin.com/pub/alexander-brenning/3/242/440


More information about the R-sig-Geo mailing list