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

时间:2018-03-28 17:27:32

标签: r raster gdal

我一直在尝试使用R中的gdal_grid,在运行示例数据集时,我收到一个只有零值的栅格。我尝试用我自己的数据进行试验,并且没有运气搜索论坛。其他人可以举例说明吗?

我试图明确调用我的GDAL库的路径,并更新了GDAL的版本。我正在使用版本3.3.1运行R studio。

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)

带零值的栅格结果图:

Raster results plot with zero values

非常感谢,

1 个答案:

答案 0 :(得分:0)

我已经通过将temp目录中的路径更改为硬盘驱动器上的文件夹来运行代码。以下示例。

# 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)