Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read .HGT files in R

I'm trying to read elevation data from NASA stored in .HGT files into R. I've checked rgdal library but apparently it does not read this type of file.

ps. The Stack Overflow community shows how to read this typee of file in Python and C++, but I was looking for a pure R solution.

More Info on topographic data in .HGT files:

In 2014, the topographic data generated from NASA's Shuttle Radar Topography Mission (SRTM) was released globally. The resolution of SRTM data for regions outside the United States is 1 arc-second, or about 30 meters (98 feet). You can read more information here and download the data here.

At the present moment, the data is available for these regions of the world: enter image description here

Here is a description from www2.jpl.nasa.gov/srtm/faq.html:

The SRTM data files have names like "N34W119.hgt". What do the letters and numbers refer to, and what is ".hgt" format?

Each data file covers a one-degree-of-latitude by one-degree-of-longitude block of Earth's surface. The first seven characters indicate the southwest corner of the block, with N, S, E, and W referring to north, south, east, and west. Thus, the "N34W119.hgt" file covers latitudes 34 to 35 North and longitudes 118-119 West (this file includes downtown Los Angeles, California). The filename extension ".hgt" simply stands for the word "height", meaning elevation. It is NOT a format type. These files are in "raw" format (no headers and not compressed), 16-bit signed integers, elevation measured in meters above sea level, in a "geographic" (latitude and longitude array) projection, with data voids indicated by -32768. International 3-arc-second files have 1201 columns and 1201 rows of data, with a total filesize of 2,884,802 bytes ( = 1201 x 1201 x 2). United States 1-arc-second files have 3601 columns and 3601 rows of data, with a total filesize of 25,934,402 bytes ( = 3601 x 3601 x 2). For more information read the text file "SRTM_Topo.txt" at http://edcftp.cr.usgs.gov/pub/data/srtm/Readme.html

like image 543
rafa.pereira Avatar asked Aug 28 '15 09:08

rafa.pereira


People also ask

How to read data from a HGT file?

The data is big-endian, which means that you need to swap the two bytes on e.g. Intel platforms. Note that efficient data retrieval would have to look a little more sophisticated (e.g. not opening the file for each and every sample). You could also use a program which can read the .hgt files out of the box.

What is the best way to store data in R?

So those files can be stored in various formats. It may be stored in .txt (tab-separated value) file, or in a tabular format i.e .csv (comma-separated value) file or it may be on internet or cloud. R provides very easier methods to read those files. One of the important formats to store a file is in a text file.

How do I read data from a tabular file in R?

R provides various methods that one can read data from a tabular formatted data file. read.table (): read.table () is a general function that can be used to read a file in table format. The data will be imported as a data frame. read.table (file, header = FALSE, sep = “”, dec = “.”)

How to choose a file in R?

file.choose (): In R it’s also possible to choose a file interactively using the function file.choose (), and if you’re a beginner in R programming then this method is very useful for you.


1 Answers

The answer is quite simple using the raster package (thanks to the comments of @Pascal and @hrbrmstr). The rgdal package must be installed as well.

# Load libraries
  library(raster)
  library(rgdal)

# read file
  elevation <- raster("S23W044.hgt") 
# view image
  image(elevation)
like image 122
rafa.pereira Avatar answered Oct 03 '22 09:10

rafa.pereira