[ACCEPTED]-how to read NASA .hgt binary files-binary

Accepted answer
Score: 12

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))


Score: 8

Since the records are fixed length (16-bit 12 signed integers) and you know the grid size 11 (1201 x 1201 or 3601x3601), Python's struct module 10 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 9 it in more generic terms, basically you 8 want to read the file in 2 bytes at a time, parse 7 the bytes read as a 16-bit signed integer 6 and process it. Since you know the grid 5 size already you can read it in row by row 4 or in any other manner that is convenient 3 to your application. It also means you can 2 randomly seek to specific coordinates inside 1 the data file.

Score: 5

If you want a little more speed than you 4 get from millions of calls to struct.unpack, have 3 a look at array.array. While the "struct-and-for-loop" implementation 2 takes several seconds on my admittedly slow 1 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)
Score: 1


    Input_HGT = 'N30E120.hgt'
    import gdal
    Raster = gdal.Open(Input_HGT) 

All functions available with GDAL on raster 1 files could be applied on this 'Raster' like Functions available with the variable, 'Raster'

Score: 0

The NASA SRTM data files are in Big-Endian 7 format, so depending on what platform you 6 are reading the data on, you may have to 5 do a conversion from Big-Endian to Little-Endian.

There 4 are numerous sources on how to do this, I 3 have no experience with Python so I can't 2 help you there.

But if you forget this, your 1 values are going to be all messed up.

More Related questions