18 Jun 2013

Use R to Bulk-Download Digital Elevation Data with 1" Resolution

Here's a little r-script to convenientely download high quality digital elevation data, i.e. for the Alps, from HERE:

require(XML)

dir.create("D:/GIS_DataBase/DEM/")
setwd("D:/GIS_DataBase/DEM/")

doc <- htmlParse("http://www.viewfinderpanoramas.org/dem3.html#alps")
urls <- paste0("http://www.viewfinderpanoramas.org", xpathSApply(doc,'//*/a[contains(@href,"/dem1/N4")]/@href'))
names <- gsub(".*dem1/(\\w+\\.zip)", "\\1", urls)

for (i in 1:length(urls)) download.file(urls[i], names[i]) 

# unzip all files in dir and delete them afterwards
sapply(list.files(pattern = "*.zip"), unzip)
unlink(list.files(pattern = "*.zip"))

p.s.: Also check raster::getData which pulls SRTM data at 90m resolution for a location / region!

4 comments :

  1. how do you get R to read the *.hgt files?

    ReplyDelete
  2. I have not been able to run the code, the end of the script I get this Error

    File NA done! ..

    ..something happend with NA dataset:
    Error in .local (.Object, ...): 'Q: \ Geodatabase \ DEM \ NA.tif' does not exist in the file system,
    and is not Recognised as a supported dataset name.

    ReplyDelete
    Replies
    1. Sorry Alvaro, I can't reproduce your error - for me everything's working fine...

      Delete