X-Git-Url: https://gitweb.fperrin.net/?a=blobdiff_plain;f=src%2Ftim%2Fprune%2Ffunction%2Fsrtm%2FLookupSrtmFunction.java;fp=src%2Ftim%2Fprune%2Ffunction%2Fsrtm%2FLookupSrtmFunction.java;h=8f4dc2e01767fb18237dd39380e6943bfb29567d;hb=9b2386d166007f68fefcaba8c27a1eefcd67af8b;hp=947cca9294b53c54a529684255cd063fcdd3c296;hpb=086dcad158175a0b424f277f3cf37f21a2b44241;p=GpsPrune.git diff --git a/src/tim/prune/function/srtm/LookupSrtmFunction.java b/src/tim/prune/function/srtm/LookupSrtmFunction.java index 947cca9..8f4dc2e 100644 --- a/src/tim/prune/function/srtm/LookupSrtmFunction.java +++ b/src/tim/prune/function/srtm/LookupSrtmFunction.java @@ -202,50 +202,7 @@ public class LookupSrtmFunction extends GenericFunction implements Runnable errorMessage += "Tile "+tile.getTileName()+" is corrupted"; } - // Loop over all points in track, try to apply altitude from array - for (int p = 0; p < _track.getNumPoints(); p++) - { - DataPoint point = _track.getPoint(p); - if (needsAltitude(point, inOverwriteZeros)) - { - if (new SrtmTile(point).equals(tile)) - { - double x = (point.getLongitude().getDouble() - tile.getLongitude()) * (rowSize - 1); - double y = rowSize - (point.getLatitude().getDouble() - tile.getLatitude()) * (rowSize - 1); - int idx1 = ((int)y)*rowSize + (int)x; - try - { - int[] fouralts = {heights[idx1], heights[idx1+1], heights[idx1-rowSize], heights[idx1-rowSize+1]}; - int numVoids = (fouralts[0]==VOID_VAL?1:0) + (fouralts[1]==VOID_VAL?1:0) - + (fouralts[2]==VOID_VAL?1:0) + (fouralts[3]==VOID_VAL?1:0); - // if (numVoids > 0) System.out.println(numVoids + " voids found"); - double altitude = 0.0; - switch (numVoids) - { - case 0: altitude = bilinearInterpolate(fouralts, x, y); break; - case 1: altitude = bilinearInterpolate(fixVoid(fouralts), x, y); break; - case 2: - case 3: altitude = averageNonVoid(fouralts); break; - default: altitude = VOID_VAL; - } - // Special case for terrain tracks, don't interpolate voids yet - if (!_normalTrack && numVoids > 0) { - altitude = VOID_VAL; - } - if (altitude != VOID_VAL) - { - point.setFieldValue(Field.ALTITUDE, ""+altitude, false); - // depending on settings, this value may have been added as feet, we need to force metres - point.getAltitude().reset(new Altitude((int)altitude, UnitSetLibrary.UNITS_METRES)); - numAltitudesFound++; - } - } - catch (ArrayIndexOutOfBoundsException obe) { - errorMessage += "Point not in tile? lat=" + point.getLatitude().getDouble() + ", x=" + x + ", y=" + y + ", idx=" + idx1+"\n"; - } - } - } - } + numAltitudesFound += applySrtmTimeToWholeTrack(tile, heights, rowSize, inOverwriteZeros); } _progress.dispose(); @@ -277,6 +234,64 @@ public class LookupSrtmFunction extends GenericFunction implements Runnable } } + /** + * Given the height data read in from file, apply the given tile to all points + * in the track with missing altitude + * @param inTile tile being applied + * @param inHeights height data read in from file + * @param inOverwriteZeros true to overwrite zero altitude values + * @return number of altitudes found + */ + private int applySrtmTimeToWholeTrack(SrtmTile inTile, int[] inHeights, int inRowSize, boolean inOverwriteZeros) + { + int numAltitudesFound = 0; + // Loop over all points in track, try to apply altitude from array + for (int p = 0; p < _track.getNumPoints(); p++) + { + DataPoint point = _track.getPoint(p); + if (needsAltitude(point, inOverwriteZeros)) + { + if (new SrtmTile(point).equals(inTile)) + { + double x = (point.getLongitude().getDouble() - inTile.getLongitude()) * (inRowSize - 1); + double y = inRowSize - (point.getLatitude().getDouble() - inTile.getLatitude()) * (inRowSize - 1); + int idx1 = ((int)y)*inRowSize + (int)x; + try + { + int[] fouralts = {inHeights[idx1], inHeights[idx1+1], inHeights[idx1-inRowSize], inHeights[idx1-inRowSize+1]}; + int numVoids = (fouralts[0]==VOID_VAL?1:0) + (fouralts[1]==VOID_VAL?1:0) + + (fouralts[2]==VOID_VAL?1:0) + (fouralts[3]==VOID_VAL?1:0); + // if (numVoids > 0) System.out.println(numVoids + " voids found"); + double altitude = 0.0; + switch (numVoids) + { + case 0: altitude = bilinearInterpolate(fouralts, x, y); break; + case 1: altitude = bilinearInterpolate(fixVoid(fouralts), x, y); break; + case 2: + case 3: altitude = averageNonVoid(fouralts); break; + default: altitude = VOID_VAL; + } + // Special case for terrain tracks, don't interpolate voids yet + if (!_normalTrack && numVoids > 0) { + altitude = VOID_VAL; + } + if (altitude != VOID_VAL) + { + point.setFieldValue(Field.ALTITUDE, ""+altitude, false); + // depending on settings, this value may have been added as feet, we need to force metres + point.getAltitude().reset(new Altitude((int)altitude, UnitSetLibrary.UNITS_METRES)); + numAltitudesFound++; + } + } + catch (ArrayIndexOutOfBoundsException obe) { + System.err.println("Point not in tile? lat=" + point.getLatitude().getDouble() + ", x=" + x + ", y=" + y + ", idx=" + idx1+"\n"); + } + } + } + } + return numAltitudesFound; + } + /** * Perform a bilinear interpolation on the given altitude array * @param inAltitudes array of four altitude values on corners of square (bl, br, tl, tr)