R - gdalUtils - gdal_grid 示例数据给出零值...?

R - gdalUtils - gdal_grid example data giving zero values...?

我一直在尝试在 R 中使用 gdal_grid,而当 运行 示例数据集时,我收到了一个只有零值的栅格。我尝试用我自己的数据对此进行试验,但没有成功地搜索论坛。其他人可以让这个例子起作用吗?

我试图显式调用我的 GDAL 库路径,并更新了 GDAL 的版本。我是 运行 R studio,版本 3.3.1。

library(raster)
library(rgeos)
library(gdalUtils)

# We'll pre-check to make sure there is a valid GDAL install
# and that raster and rgdal are also installed.
# Note this isn't strictly neccessary, as executing the function will
# force a search for a valid GDAL install.
gdal_setInstallation()
valid_install <- !is.null(getOption("gdalUtils_gdalPath"))
if(require(raster) && valid_install)
{
  # Create a properly formatted CSV:
  temporary_dir <- tempdir()
  tempfname_base <- file.path(temporary_dir,"dem")
  tempfname_csv <- paste(tempfname_base,".csv",sep="")

  pts <- data.frame(
    Easting=c(86943.4,87124.3,86962.4,87077.6),
    Northing=c(891957,892075,892321,891995),
    Elevation=c(139.13,135.01,182.04,135.01)
  )

  write.csv(pts,file=tempfname_csv,row.names=FALSE)

  # Now make a matching VRT file
  tempfname_vrt <- paste(tempfname_base,".vrt",sep="")
  vrt_header <- c(
    '<OGRVRTDataSource>',
    '\t<OGRVRTLayer name="dem">',
    '\t<SrcDataSource>dem.csv</SrcDataSource>',
    '\t<GeometryType>wkbPoint</GeometryType>',
    '\t<GeometryField encoding="PointFromColumns" x="Easting" y="Northing" z="Elevation"/>',
    '\t</OGRVRTLayer>',
    '\t</OGRVRTDataSource>'
  )
  vrt_filecon <- file(tempfname_vrt,"w")
  writeLines(vrt_header,con=vrt_filecon)
  close(vrt_filecon)

  tempfname_tif <- paste(tempfname_base,".tiff",sep="")

  # Now run gdal_grid:
  setMinMax(gdal_grid(src_datasource=tempfname_vrt,
                      dst_filename=tempfname_tif,a="invdist:power=2.0:smoothing=1.0",
                      txe=c(85000,89000),tye=c(894000,890000),outsize=c(400,400),
                      of="GTiff",ot="Float64",l="dem",output_Raster=TRUE))
}

r<-raster(tempfname_tif)

r

#class       : RasterLayer 
#dimensions  : 400, 400, 160000  (nrow, ncol, ncell)
#resolution  : 10, 10  (x, y)
#extent      : 85000, 89000, 890000, 894000  (xmin, xmax, ymin, ymax)
#coord. ref. : NA 
#data source : C:\Users\m.modeler\AppData\Local\Temp\RtmpW6HvOc\dem.tiff 
#names       : dem 
#min values  :   0 
#max values  :   0 

plot(r)

具有零值的栅格结果图:

非常感谢,

通过将路径从临时目录更改为我硬盘驱动器上的文件夹,我获得了 运行 的代码。下面的例子。

# change to a path on your computer
setwd("C:\Users\m.modeler\Documents\R\gdal_Examples")

#######################################################
#create XYZ csv

pts <- data.frame(
  Easting=c(86943.4,87124.3,86962.4,87077.6),
  Northing=c(891957,892075,892321,891995),
  Elevation=c(139.13,135.01,182.04,135.01))

write.csv(pts,file="dem.csv",row.names=FALSE)

#######################################################
#create VRT

fn_vrt<-"dem.vrt"

# Now make a matching VRT file
vrt_header <- c(
  '<OGRVRTDataSource>',
  '\t<OGRVRTLayer name="dem">',
  '\t<SrcDataSource>dem.csv</SrcDataSource>',
  '\t<GeometryType>wkbPoint</GeometryType>',
  '\t<GeometryField encoding="PointFromColumns" x="Easting" y="Northing" z="Elevation"/>',
  '\t</OGRVRTLayer>',
  '\t</OGRVRTDataSource>')

vrt_filecon <- file(fn_vrt,"w")
writeLines(vrt_header,con=vrt_filecon)
close(vrt_filecon)

#######################################################
#create interpolated DEM

fn_tif <- "dem.tif"

# Now run gdal_grid:
r.dem <- setMinMax(gdal_grid(src_datasource=fn_vrt,
                    dst_filename=fn_tif,a="invdist:power=2.0:smoothing=1.0",
                    txe=c(85000,89000),tye=c(894000,890000),outsize=c(400,400),
                    of="GTiff",ot="Float64",l="dem",output_Raster=TRUE,verbose=TRUE))

plot(r.dem)