【问题标题】:Wrapper function for usage of gdal_calc.py in R在 R 中使用 gdal_calc.py 的包装函数
【发布时间】:2019-05-10 01:54:01
【问题描述】:

R 使用 gdalUtils 包为 gdal 库提供包装器。我想使用 gdal_calc.py 函数进行光栅计算,但是,我没有在包中找到有关此特定函数的信息。我创建了自己的 wrapper 函数,它可以工作,但通常不是 100% 完美,而且只能在 linux 上运行。它也没有原来的灵活(如果有兴趣,请参见下文)。 R 中还有其他实现吗?

f.gdal.calc<-function(path.in.r.1,
                      #r1.band=1,
                      path.in.r.2,
                      # r2.band=1,
                      path.out.r,
                      my.fun="A*B",#defaults to multiplication
                      r.datatype="Float32",
                      cachemax=2000, 
                      overwrite=FALSE, # caution, can destroy data
                      printcommand=FALSE,# should the commad be printed prior to execution? Can help debugging 
                      bigtiff=F,
                      compression=F) 
{gdalcommand<-paste("gdal_calc.py ",
                    "-A ",path.in.r.1,
                    # " --allBands A ",
                    #"--A_band=",r1.band,
                    " -B ",path.in.r.2,
                    #"--b_band=",r2.band,
                    " --outfile=",path.out.r,
                    " --calc=",shQuote(my.fun,type = "cmd"),# supply function as character like "A+B"
                    " --type ",r.datatype,
                    ifelse(overwrite==TRUE,paste(" --overwrite"),paste("")),
                    ifelse(compression==TRUE,paste(" --co COMPRESS=LZW"),paste("")), #should not be enabled when creating bigtiffs
                    ifelse(bigtiff==TRUE,paste(" --co BIGTIFF=YES"),paste("")),
                    sep=""
                     ) 
if(printcommand==T){print(gdalcommand)}
system(gdalcommand)
}

【问题讨论】:

    标签: r geospatial gdal rgdal


    【解决方案1】:

    据开发人员称,目前gdalUtils 中没有实现基于 python 的 gdal 函数。因此,如果您想在 R 中使用gdal_calc 进行栅格计算,您需要创建自己的与上述类似的包装函数。

    【讨论】:

      【解决方案2】:

      现在您可以在“agroEcoTradeoff” R 包中使用“rast_math”和“gdal_calc”函数:

      https://rdrr.io/github/marcusspiegel/agroEcoTradeoff/src/R/rast_math.R

      希望有用!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-10-02
        • 2020-02-01
        • 1970-01-01
        • 1970-01-01
        • 2020-02-17
        • 2017-12-16
        • 1970-01-01
        • 2022-01-22
        相关资源
        最近更新 更多