Ignore:
Timestamp:
2013-08-25T20:50:29+02:00 (11 years ago)
Author:
oliverw
Message:

Check for HGT buffer size and data voids.

Location:
applications/editors/josm/plugins/ElevationProfile/src/org/openstreetmap/josm/plugins/elevation
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/ElevationProfile/src/org/openstreetmap/josm/plugins/elevation/HgtReader.java

    r29876 r29877  
    2020 */
    2121public class HgtReader {
     22    private static final int SECONDS_PER_MINUTE = 60;
     23
    2224    public static final String HGT_EXT = ".hgt";
    2325   
     
    2527    public static final int HGT_RES = 3; // resolution in arc seconds
    2628    public static final int HGT_ROW_LENGTH = 1201; // number of elevation values per line
     29    public static final int HGT_VOID = -32768; // magic number which indicates 'void data' in HGT file
    2730   
    2831    private HashMap<String, ShortBuffer> cache = new HashMap<String, ShortBuffer>();
     
    3841                // but is not there'
    3942                cache.put(file, null);
    40                  // Try all other resource directories
     43                 // Try all resource directories
    4144                for (String location : Main.pref.getAllPossiblePreferenceDirs()) {                 
    4245                        String fullPath = new File(location + File.separator + "elevation", file).getPath();
    4346                                 
    44                         System.out.println("Search in " + location);
     47                        System.out.println(fullPath);
    4548                        File f = new File(fullPath);
    4649                        if (f.exists()) {
     
    108111       
    109112        // see http://gis.stackexchange.com/questions/43743/how-to-extract-elevation-from-hgt-file
    110         double fLat = frac(coor.lat()) * 60;
    111         double fLon = frac(coor.lon()) * 60;
     113        double fLat = frac(coor.lat()) * SECONDS_PER_MINUTE;
     114        double fLon = frac(coor.lon()) * SECONDS_PER_MINUTE;
    112115       
    113        
    114         int row = (int)Math.round(fLat * 60 / HGT_RES);
    115         int col = (int)Math.round(fLon * 60 / HGT_RES);
     116        // compute offset within HGT file
     117        int row = (int)Math.round(fLat * SECONDS_PER_MINUTE / HGT_RES);
     118        int col = (int)Math.round(fLon * SECONDS_PER_MINUTE / HGT_RES);
    116119       
    117120        row = HGT_ROW_LENGTH - row;
    118121        int cell = (HGT_ROW_LENGTH*  (row - 1)) + col;
    119122
    120         return sb.get(cell);
     123        // valid position in buffer?
     124        if (cell < sb.limit()) {
     125            short ele = sb.get(cell);
     126            // check for data voids
     127            if (ele == HGT_VOID) {
     128                return Double.NaN;
     129            } else {
     130                return ele;
     131            }
     132        } else {
     133            return Double.NaN;
     134        }
    121135    }
    122136   
  • applications/editors/josm/plugins/ElevationProfile/src/org/openstreetmap/josm/plugins/elevation/tests/HgtReaderTest.java

    r29876 r29877  
    3838        double d = hr.getElevationFromHgt(l);
    3939        System.out.println(d);
    40         assert(!Double.isNaN(d));
     40        assertFalse("Data missing or void for coor " + l, Double.isNaN(d));
     41       
    4142        assertEquals((int)d, expHeight);
    4243    }
Note: See TracChangeset for help on using the changeset viewer.