how to read NASA .hgt binary files

后端 未结 6 964
你的背包
你的背包 2020-12-07 19:26

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

6条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-07 19:49

    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.

提交回复
热议问题