Using SRTM tif file in R

为君一笑 提交于 2019-12-11 16:28:38

问题


I'm trying to import a SRTM dataset into R. I've downloaded the data in a tif file however am having trouble reading it in "R". Ive tried using the following code:

t = readTIFF("srtm_56_06/srtm_56_06.tif", as.is=TRUE)
load('srtm_56_06/srtm_56_06.tif')
read_file<-as.matrix(raster("srtm_56_06/srtm_56_06.tif")

However I am still getting error messages:

load('srtm_56_06/srtm_56_06.tif')
# Error: bad restore file magic number (file may be corrupted) -- no data loaded
# In addition: Warning message:
# file ‘srtm_56_06.tif’ has magic number 'II*'
#   Use of save versions prior to 2 is deprecated 

library(raster)
t = readTIFF("srtm_56_06/srtm_56_06.tif", as.is=TRUE)
# Error: could not find function "readTIFF"

read_file<-as.matrix(raster("srtm_56_06/srtm_56_06.tif") + min(read_file)
# Error: unexpected symbol in:
# "read_file<-as.matrix(raster("srtm_56_06/srtm_56_06.tif")
# min"

Can anyone help me with the commands to import this data. I'm a novice at "R" and a little lost.


回答1:


Just read it with raster, but note you depend on rgdal being installed as well to read a .tif.

library(raster)
library(rgdal)
r <- raster("srtm_56_06/srtm_56_06.tif")

If that works, try

plot(r)
r

If it's really a "TIFF" then that should be fine, if it's really a GeoTIFF then you'll have a sensible map as well. (If it's something else that GDAL can read you might get a good result anyway, remember the extension of a file is not a reliable indicator of its contents).

The SRTM clue suggests that this is a single band DEM file from the tiled global SRTM data set. If it's somehow a "multi-band image" then you could read that with brick and plot with plotRGB (but I really doubt that is the case here). Note that there is a native binary format for SRTM that raster/rgdal could read as well but either they distributed .tif as well or someone else converted it.

There are a number of misconceptions in your code:

  • load is for a particular file type created from R (not these .tifs)
  • readTIFF is not in package raster
  • read_file would be a sensible matrix, if you have rgdal installed (which raster must use to load a .tif), but why throw away the spatial metadata?


来源:https://stackoverflow.com/questions/24789159/using-srtm-tif-file-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!