Search code examples
rmapsrastergeotiff

collecting elevation data - extract geolocations from a geotiff file


I am trying to add elevation data to a plot using the rayshader package. I can plot the area in which I want to find elevation data using the leaflet package.

library(whitebox)
library(leaflet)
library(rayshader)
library(rayrender)
library(raster)

# define bounding box with longitude/latitude coordinates
bbox <- list(
  p1 = list(long = -3.6525599, lat = 40.4065001),
  p2 = list(long = -3.7525599, lat = 40.4965001)
)

leaflet() %>%
  addTiles() %>% 
  addRectangles(
    lng1 = bbox$p1$long, lat1 = bbox$p1$lat,
    lng2 = bbox$p2$long, lat2 = bbox$p2$lat,
    fillColor = "transparent"
  ) %>%
  fitBounds(
    lng1 = bbox$p1$long, lat1 = bbox$p1$lat,
    lng2 = bbox$p2$long, lat2 = bbox$p2$lat,
  )

I am following this blog: https://wcmbishop.github.io/rayshader-demo/ and currently at the part:

"Downloading Elevation Data"

The author uses the USGS data collected from this link:

https://elevation.nationalmap.gov/arcgis/rest/services/3DEPElevation/ImageServer/exportImage?bbox=-122.522%2C37.707%2C-122.354%2C37.84&bboxSR=4326&size=600%2C480&imageSR=4326&time=&format=jpgpng&pixelType=F32&noData=&noDataInterpretation=esriNoDataMatchAny&interpolation=+RSP_BilinearInterpolation&compression=&compressionQuality=&bandIds=&mosaicRule=&renderingRule=&f=html

I replaced the lat, long coordinates in the link with mine:

https://elevation.nationalmap.gov/arcgis/rest/services/3DEPElevation/ImageServer/exportImage?bbox=-3.6525599%2C40.4065001%2C-3.7525599%2C40.4965001&bboxSR=4326&size=600%2C480&imageSR=4326&time=&format=jpgpng&pixelType=F32&noData=&noDataInterpretation=esriNoDataMatchAny&interpolation=+RSP_BilinearInterpolation&compression=&compressionQuality=&bandIds=&mosaicRule=&renderingRule=&f=html

Which returned an empty image - which is no surprise since the USGS is a U.S. data provider. So I download the following file:

The DTM Spain Mainland 20m http://data.opendataportal.at/dataset/dtm-spain/resource/38816df0-9d50-476f-832e-0f7f5fa21771

The file is 2.4 GB in size and is a file for the whole of Spain but I only want the elevation data for the bounds of my lat & long points.

rgdal::GDALinfo("DTM Spain_Mainland (2019) 20m.tif")
rows        48394 
columns     58187 
bands       1 
lower left origin.x        -23380 
lower left origin.y        3901190 
res.x       20 
res.y       20 
ysign       -1 
oblique.x   0 
oblique.y   0 
driver      GTiff 
projection  +proj=utm +zone=30 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m
+no_defs 
file        DTM Spain_Mainland (2019) 20m.tif 
apparent band summary:
   GDType hasNoDataValue NoDataValue blockSize1 blockSize2
1 Float32           TRUE      -32767          1      58187
apparent band statistics:
         Bmin       Bmax Bmean Bsd
1 -4294967295 4294967295    NA  NA
Metadata:
AREA_OR_POINT=Point 

My question is, how can I either get the geotiff for the long and lat positions in bbox or filter the current DTM Spain_Mainland (2019) 20m.tif file down to just the long, lat coordinates?


Solution

  • You could have a look at the elevatr package, which will give you the raster you want:

    library(elevatr)
    library(raster)
    
    bbox2 <- data.frame(x = c(-3.6525599, -3.7525599), y = c(40.4065001, 40.4965001)) 
    
    elev <- get_elev_raster(bbox2, z = 13, clip = "bbox",
                            prj = "+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0")
    

    enter image description here