Working with .HGT files to find elevation of a particular lat/long

Recently for a web and IOS application, we were asked to provide a solution that will work offline and find the elevation at a particular lat/long.
So one of the solutions was to use HGT files provided by NASA and team.(They are also called DEM files, but I haven’t done a whole lot of research on it, so can’t tell more)

Ok, so it was simple just read the HGT files as prescribed, and you are done. But the problem arose when none of the available open source libraries that we explored were able to give us the correct elevation.
Since we were working with PHP, we started off with using a PHP library and then when it wasn’t working, moved on to library in almost every language that I was able to understand.

I explored the following libraries among the few:

  1. PHP
  2. Python
  3. Java

The PHP library at #1, was giving good results, but the only problem was when we were testing it with the locations in the US.
So for something like a 42.489657, -71.887785, the results were way off from what the elevation should be.
Although a minor difference of 8 to 10 meters from what I have found is common, but the difference was really huge.

It was clear we need to fix something with the code. But to really understand the problem I had to go to through a lot of steps, like installing a tool called geomatica and loading the HGT files in it to verify is HGT files were correct.  Learning more about latitude and longitude and reading more about SRTM.

I will mention below the what made sense to me and the change I did to get it working correctly.

I was working with the following coordinates 42.489657, -71.887785    which had elevation of around 593m according to freemaptools.com and google maps was also showing the same.

But the python library showed 18m :

And the PHP library showed 0m.

So from what I had understood till now was :

  1. The tile(.hgt file) are represented by the lower left sample. To understand look at the image(this is just for simplicity):

    So the tile containing point F would be named like N30E60.hgt, instead of N30E90.hgt or N40E60.hgt etc. (lower left point is used for naming the tile)

  2. Whenever probing the libraries for a point like 42.13, -71.45 the output name from all the libraries was N42W71.hgt. (+ve => N,E, -ve => S,W).
  3. Going by #1, it seemed that a tile which has N42W71.hgt will only have data for lattitude > -71°, so something like -70.8° will be in N42W71.hgt, but -71.3° should be in N42W72.hgt. Logic being, when moving left the value increases for latitude and not decreases, as can be seen in the image above as well.

So the solution turned out to be something which was to add a -1 when calculating file names in -ve values(lat or long). so for a point like 42.13, -71.45 the name would be N42W72.hgt.

Although I am highly skeptical that I might be missing something, as multiple libraries shouldn’t have been giving incorrect output, but this works for us today. Will update if I figure out another way of approaching this.

You can find the updated PHP file here.

Posted in PHP

Leave a Reply

Your email address will not be published. Required fields are marked *