Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Downloading SRTM data with raster package?

Tags:

r

zip

raster

srtm

I'm trying to get SRTM data with "raster" package in R, but as soon as I'm choosing SRTM in getData command, I would get the following error:

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

Any Idea what is this error for ?

like image 656
user9112767 Avatar asked Dec 27 '17 19:12

user9112767


Video Answer


1 Answers

I have the same problem, it seems to be a bug. The getData function in raster package checks for availability of the raster file in three different url's.

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

The first two of them (as of today) are not working or cannot be accessible. However for some reason a small bit data is getting transferred across the server, so the package assumes it to be a available file only to reach an error by utils. The third url however is most trusted one among the three.

I did some digging and came up with the following function after slightly modifying the raster package itself so that it uses the third url. You can input Longitude and Latitude values here. Note that this is only useful if you want to download the files based on Latitude & Longitude.

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

Example:

SRTM(lon=16, lat=48)

This will result in a file named srtm_40_03.zip in your folder which would normally contain a tif, a hdr and a tfw file of the same name. Use them for further process as usual.

EDIT DATE 22-JAN-19: The srtm link as changed (as well), the above code has been adapted to reflect this.

like image 115
SamAct Avatar answered Sep 30 '22 21:09

SamAct