[GIS] Extracting data from hgt files

demhgtjsonnode-js

SRTM3 .hgt files have 1201*1201 DEM elevation data, I extract .hgt files data elevation into file Elevation.json with index.js with the help from this GitHub project.

Then I rename Elevation.json into Elevation.txt, replacing all ],[ from inside the converted .hgt file in json array into ]^l[ with MS Word to get each row from json at new line.

Finally, I import this .txt file into .xlsx file with "comma" as separating delimiter.

I expect as final result in Excel, just 2101 rows and 2101 columns.

Why is there an error always at these cells?

I shortcuted the first and the last 3 columns and the first and the last 2 rows to show you the result

enter image description here.

Note: blank cells = no found data.

I tried different .hgt file from world wide, the problem still exist.

Best Answer

I think it is bug in index.js. I just replicate what you did but differently, and got to the same result:

I run index.js in a .hgt file, then replaced the "],[" by new lines and removed any extra "[" or "]" doing

cat heights.json | sed 's/\],\[/\n/g' | tr -d ']' | tr -d '[' > heights.csv

Then loading the csv in matlab in fact produced an error of inconsistency in the number of columns per row.

By counting the number of commas per line using:

sed 's/[^,]//g' heights+cr.txt | awk '{ print length }'

It indeed show that the first row has 1202 columns, the last 1200 columns and all the rest 1201 columns.

However, after loading the same .htg file directly in Matlab, it produce a nice 1201x1201 matrix.

You can load it directly in Matlab with the following code:

fileID=fopen('S40W067.hgt','r');% Replace by your HTG file
data = fread(fileID,[1201,1300],'uint16',0,'b');
fclose(fileID);

If you rather python, this question show some code examples.

By looking at the data from index.js and from Matlab, it seem that if you move the extra element at the end of row one, to be the first of row two, and then the last of two to be the first of the third row and so on, you will end up with the correct 1201x1201 data matrix.

Related Question