I\'m sure this is really simple if you know anything about binary files, but I\'m a newbie on that score.
How would I extract the data from NASA .hgt files? Here is
A tested numpy example:
import os
import math
import numpy
fn = 'DMV/N51E000.hgt'
siz = os.path.getsize(fn)
dim = int(math.sqrt(siz/2))
assert dim*dim*2 == siz, 'Invalid file size'
data = numpy.fromfile(fn, numpy.dtype('>i2'), dim*dim).reshape((dim, dim))
https://gdal.org/drivers/raster/srtmhgt.html
Input_HGT = 'N30E120.hgt'
import gdal
Raster = gdal.Open(Input_HGT)
All functions available with GDAL on raster files could be applied on this 'Raster' like Functions available with the variable, 'Raster'
If you have photoshop you might be able to play around with the raw import to get it to read these files and save them out to something more useful. I have had some success doing this sort of thing in the past.
If you want a little more speed than you get from millions of calls to struct.unpack, have a look at array.array. While the "struct-and-for-loop" implementation takes several seconds on my admittedly slow laptop, the following is near instantaneous:
from array import array
f = open(filename, 'rb')
format = 'h'
row_length = 1201
data = array(format)
data.fromfile(f, row_length*row_length)
data.byteswap()
f.close()
Since the records are fixed length (16-bit signed integers) and you know the grid size (1201 x 1201 or 3601x3601), Python's struct module seems ideally suited (untested code):
from struct import unpack,calcsize
# 'row_length' being 1201 or 3601 and 'row' being the raw data for one row
def read_row( row, row_length ):
format = 'h' # h stands for signed short
for i in range(0, row_length):
offset = i * calcsize(format)
(height,) = unpack(format, row[offset : offset+calcsize(format))
# do something with the height
Describing it in more generic terms, basically you want to read the file in 2 bytes at a time, parse the bytes read as a 16-bit signed integer and process it. Since you know the grid size already you can read it in row by row or in any other manner that is convenient to your application. It also means you can randomly seek to specific coordinates inside the data file.
The NASA SRTM data files are in Big-Endian format, so depending on what platform you are reading the data on, you may have to do a conversion from Big-Endian to Little-Endian.
There are numerous sources on how to do this, I have no experience with Python so I can't help you there.
But if you forget this, your values are going to be all messed up.