When reading SRTM3 data from a .hgt file I get the wrong elevation of a specific latitude longitude and I can't see why. Maybe someone can see the problem ?

My coordinate is near Lissabon in the sea and I expect an elevation around 0 or no elevation data, which means 0 as well. But I get an elevation of 32 meters. Something is slightly shifted, but I cant see what.

My coordinate : enter image description here

My code in Java with values at the end of the lines for analysing:

enter image description here

Can someone see, what I am calculating wrong ?

Java code as text:

public double readElevation(LatLon coor) {
    String tag = getHgtFileName(coor);

    ShortBuffer sb = cache.get(tag);

    if (sb == null) {
        return NO_ELEVATION;
    // see http://gis.stackexchange.com/questions/43743/how-to-extract-elevation-from-hgt-file
    double fLat = frac(coor.getLatitude()) * SECONDS_PER_MINUTE;
    double fLon = frac(coor.getLongitude()) * SECONDS_PER_MINUTE;

    // compute offset within HGT file
    int row = (int) Math.round(fLat * SECONDS_PER_MINUTE / HGT_RES);
    if(fLat < 0){
        row = row * -1;
        row = HGT_ROW_LENGTH - row;
    int col = (int) Math.round(fLon * SECONDS_PER_MINUTE / HGT_RES);

    int cell = (HGT_ROW_LENGTH * (row - 1)) + col;

    // valid position in buffer?
    if (cell < sb.limit()) {
        short ele = sb.get(cell);
        System.out.println("==> Read SRTM elevation data from row/col/cell " + row + "," + col + ", " + cell + " = " + ele);
        // check for data voids
        if (ele == HGT_VOID) {
            return NO_ELEVATION;
        } else {
            return ele;
    } else {
        return NO_ELEVATION;

closed as off-topic by user30184, user2856, Jochen Schwarze, whyzar, Vince Sep 19 '18 at 23:48

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This problem cannot or can no longer be reproduced. Changes to the system or to the asker's circumstances may have rendered the question obsolete, or the question does not include a procedure to enable potential answerers to reproduce the same symptoms. Such questions are off-topic as they are unlikely to help future readers, but editing them to include more details can lead to re-opening." – user30184, user2856, Jochen Schwarze, Vince
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    SRTM in DTED format is column-major from the southwest, not row major from the northeast, so you probably want to double-check your math. – Vince Sep 16 '18 at 13:09
  • I am very unsure what this means. Do you think "int cell = (HGT_ROW_LENGTH * (row - 1)) + col;" is wrong, right. I guess row and col are correct but cell is wrong. Do you think too ? – user1344545 Sep 16 '18 at 14:12
  • I'm not going to do Java math in my head before noon, but your cell calculation is row-major. This is the sort of thing that diagnostic print statements and debugging are for. – Vince Sep 16 '18 at 14:22

I found the issue in my case. if I have a negativ longitude I have to read from a previous .hgt. In my case I read from N38W009.hgt, which is wrong.

I have to read from N38W010.hgt.

Not the answer you're looking for? Browse other questions tagged or ask your own question.