【问题标题】:Downloading SRTM data with raster package?使用栅格包下载 SRTM 数据?
【发布时间】:2017-12-27 19:03:30
【问题描述】:

我正在尝试使用 R 中的“raster”包获取 SRTM 数据,但是一旦我在 getData 命令中选择SRTM,就会收到以下错误:

library(raster)

srtm <- getData('SRTM', lon=16, lat=48)
trying URL 'ftp://xftp.jrc.it/pub/srtmV4/tiff/srtm_40_03.zip'
trying URL 'http://hypersphere.telascience.org/elevation/cgiar_srtm_v4/tiff/zip/srtm_40_03.ZIP'
downloaded 572 bytes

Error in .SRTM(..., download = download, path = path) : file not found
In addition: Warning messages:
1: In utils::download.file(url = aurl, destfile = fn, method = "auto",  :
  URL 'ftp://xftp.jrc.it/pub/srtmV4/tiff/srtm_40_03.zip': status was 'Couldn't resolve host name'
2: In utils::unzip(zipfilename, exdir = dirname(zipfilename)) :
  error 1 in extracting from zip file

任何想法这个错误是什么?

【问题讨论】:

标签: r zip raster srtm


【解决方案1】:

我有同样的问题,这似乎是一个错误。 raster 包中的 getData 函数检查三个不同 url 中的光栅文件的可用性。

1. ftp://xftp.jrc.it/pub/srtmV4/tiff/FILENAME
2. http://hypersphere.telascience.org/elevation/cgiar_srtm_v4/tiff/zip/FILENAME
3. http://srtm.csi.cgiar.org/SRT-ZIP/SRTM_V41/SRTM_Data_GeoTiff/FILENAME

前两个(截至今天)不工作或无法访问。但是由于某种原因,一小部分数据正在通过服务器传输,因此包假定它是一个可用文件,只是为了达到utils 的错误。然而,第三个网址是三个网址中最受信任的网址之一。

在稍微修改raster 包本身使其使用第三个url 后,我进行了一些挖掘并提出了以下功能。您可以在此处输入 LongitudeLatitude 值。请注意,这仅在您要下载基于纬度和经度的文件时才有用。

SRTM<-function(lon, lat) {
  stopifnot(lon >= -180 & lon <= 180)
  stopifnot(lat >= -60 & lat <= 60)
  rs <- raster(nrows=24, ncols=72, xmn=-180, xmx=180, ymn=-60, ymx=60 )
  rowTile <- rowFromY(rs, lat)
  colTile <- colFromX(rs, lon)
  if (rowTile < 10) { rowTile <- paste('0', rowTile, sep='') }
  if (colTile < 10) { colTile <- paste('0', colTile, sep='') }

  f <- paste('srtm_', colTile, '_', rowTile, sep="")
  theurl <- paste("http://srtm.csi.cgiar.org/wp-content/uploads/files/srtm_5x5/TIFF/", f, ".ZIP", sep="")
  utils::download.file(url=theurl, destfile='srtm_40_0.zip', method="auto", quiet = FALSE, mode = "wb", cacheOK = TRUE)
}

例子:

SRTM(lon=16, lat=48)

这将在您的文件夹中生成一个名为srtm_40_03.zip 的文件,该文件通常包含一个tif、一个hdr 和一个tfw 同名文件。像往常一样使用它们进行进一步处理。

编辑日期 19 年 1 月 22 日: srtm 链接也发生了变化,上面的代码已经适应了这一点。

【讨论】:

  • 这对我仍然不起作用:SRTM(lon=16, lat=48) trying URL 'http://srtm.csi.cgiar.org/SRT-ZIP/SRTM_V41/SRTM_Data_GeoTiff/srtm_40_03.ZIP' Error in utils::download.file(url = theurl, destfile = "srtm_40_03.zip", : cannot open URL 'http://srtm.csi.cgiar.org/SRT-ZIP/SRTM_V41/SRTM_Data_GeoTiff/srtm_40_03.ZIP' In addition: Warning message: In utils::download.file(url = theurl, destfile = "srtm_40_03.zip", : InternetOpenUrl failed: 'The operation timed out'
  • 可以用浏览器访问链接吗?
  • 是的,当我将链接复制到浏览器时正在下载
  • 那么代码应该可以工作了,我已经重新检查过了。查看是否安装了所有必需的软件包,然后重试。如果它不起作用,那么您的 R-Studio 可能有问题。
【解决方案2】:

我在尝试使用 raster::getData() 下载 SRTM 数据时遇到了完全相同的错误。正如samAct 所提到的,问题与不再可访问的 URL 有关。

我的解决方案是编辑 getData 函数,使其首先尝试http://srtm.csi.cgiar.org/SRT-ZIP/SRTM_V41/SRTM_Data_GeoTiff/FILENAME URL。

要使用我编辑的 raster 包版本,只需使用 `devtools::install_github("pokyah/raster")。这将覆盖您当前的版本并使 getData 使用有效的 URL。

devtools::install_github("pokyah/raster")
library(raster)

然后,您可以使用此功能下载您感兴趣区域的 SRTM 数据:

# @param country_code.chr a character specifying the ISO contrycode. Ex : BE for belgium
# @param NAME_1.chr a character specifying the NAME_1 value for lower than country level information
# @param aggregation_factor.num a numeric specifying the aggregation factor to get the desired spatial resolution
# @param EPSG.chr a character specifying the EPSG code of the desired Coordiante Reference System (CRS)
# @param path.chr a character specifying the path where to dowload the SRTM data

build_highRes_terrain_rasters.fun <- function(country_code.chr, NAME_1.chr=NULL, aggregation_factor.num=NULL, EPSG.chr=NULL, path.chr) {
  # Path to downloaded SRTM Tiles refs
  srtm.tiles.ref <- raster::shapefile("<PATH_TO_DOWNLOADED_TILES_REFS")

  # Get country geometry first
  extent.sp <- raster::getData('GADM', country=country_code.chr, level=1)

  if(!is.null(NAME_1.chr)){
    extent.sp <- subset(extent.sp, NAME_1 == NAME_1.chr)
  }

  # Intersect extent geometry and tile grid
  intersects <- rgeos::gIntersects(extent.sp, srtm.tiles.ref, byid=T)
  tiles      <- srtm.tiles.ref[intersects[,1],]

  # Download tiles using getData
  # inspired from https://www.gis-blog.com/download-srtm-for-an-entire-country/
  srtm_list  <- list()
  for(i in 1:length(tiles)) {
    lon <- extent(tiles[i,])[1]  + (extent(tiles[i,])[2] - extent(tiles[i,])[1]) / 2
    lat <- extent(tiles[i,])[3]  + (extent(tiles[i,])[4] - extent(tiles[i,])[3]) / 2

    tile <- getData('SRTM', #data are downloaded from http://www.cgiar-csi.org/. See getData do of pokyah/raster repo on github
                    lon=lon,
                    lat=lat,
                    download = FALSE,
                    path = path.chr)

    srtm_list[[i]] <- tile
  }

  # Mosaic tiles
  srtm_list$fun <- mean
  srtm_mosaic.ras <- do.call(raster::mosaic, srtm_list)

  # Crop tiles to extent borders
  extent.elevation.ras <- raster::crop(srtm_mosaic.ras, extent.sp)
  extent.elevation.ras <- raster::mask(extent.elevation.ras, extent.sp)

  # transform to desired CRS
  if(!is.null(EPSG.chr)){
    raster::projectRaster(extent.elevation.ras, crs = toString((dplyr::filter(rgdal::make_EPSG(), code==EPSG.chr))$prj4))
  }

  # aggregate to lower resolution
  # inspired from https://stackoverflow.com/questions/32278825/how-to-change-the-resolution-of-a-raster-layer-in-r
  if(!is.null(aggregation_factor.num)){
    extent.elevation.ras <- raster::aggregate(extent.elevation.ras, fact=aggregation_factor.num)
  }

  # compute the slope from the elevation
  # inspired from https://rpubs.com/etiennebr/visualraster
  extent.slope.ras <- raster::terrain(extent.elevation.ras, opt="slope", unit="degrees")
  extent.aspect.ras <- raster::terrain(extent.elevation.ras, opt="aspect", unit="degrees")
  extent.roughness.ras <- raster::terrain(extent.elevation.ras, opt="roughness")

  # compute the aspect from the elevation
  extent.terrain.ras = raster::stack(
    extent.elevation.ras,
    extent.slope.ras,
    extent.aspect.ras,
    extent.roughness.ras)
}

希望对您有所帮助!

【讨论】:

    【解决方案3】:

    今天,21-01-2019,链接仍然断开,即使是由 pokyah 纠正的链接。新的工作版本:

    devtools::install_github("fedefyco/raster")
    library(raster)
    

    【讨论】:

    • 是的,上面的代码现在也为新链接修复了。
    猜你喜欢
    • 1970-01-01
    • 2020-05-23
    • 1970-01-01
    • 2012-05-29
    • 2020-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多