From 8f0576f7a38dd5db3dc7c169bd4333fbad4e7ff6 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Fr=C3=A9d=C3=A9ric=20Perrin?= Date: Tue, 20 Oct 2020 20:35:30 +0100 Subject: [PATCH] Always set point altitudes in LookipSRTM --- .../function/srtm/LookupSrtmFunction.java | 126 ++++++------------ 1 file changed, 39 insertions(+), 87 deletions(-) diff --git a/src/tim/prune/function/srtm/LookupSrtmFunction.java b/src/tim/prune/function/srtm/LookupSrtmFunction.java index 8f4dc2e..5e03ac8 100644 --- a/src/tim/prune/function/srtm/LookupSrtmFunction.java +++ b/src/tim/prune/function/srtm/LookupSrtmFunction.java @@ -93,75 +93,31 @@ public class LookupSrtmFunction extends GenericFunction implements Runnable { // Compile list of tiles to get ArrayList tileList = new ArrayList(); - boolean hasZeroAltitudePoints = false; - boolean hasNonZeroAltitudePoints = false; - // First, loop to see what kind of points we have - for (int i = 0; i < _track.getNumPoints(); i++) - { - if (_track.getPoint(i).hasAltitude()) - { - if (_track.getPoint(i).getAltitude().getValue() == 0) { - hasZeroAltitudePoints = true; - } - else { - hasNonZeroAltitudePoints = true; - } - } - } - // Should we overwrite the zero altitude values? - boolean overwriteZeros = hasZeroAltitudePoints && !hasNonZeroAltitudePoints; - // If non-zero values present as well, ask user whether to overwrite the zeros or not - if (hasNonZeroAltitudePoints && hasZeroAltitudePoints && JOptionPane.showConfirmDialog(_parentFrame, - I18nManager.getText("dialog.lookupsrtm.overwritezeros"), I18nManager.getText(getNameKey()), - JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) - { - overwriteZeros = true; - } // Now loop again to extract the required tiles for (int i = 0; i < _track.getNumPoints(); i++) { - // Consider points which don't have altitudes or have zero values - if (needsAltitude(_track.getPoint(i), overwriteZeros)) + SrtmTile tile = new SrtmTile(_track.getPoint(i)); + boolean alreadyGot = false; + for (int t = 0; t < tileList.size(); t++) { - SrtmTile tile = new SrtmTile(_track.getPoint(i)); - boolean alreadyGot = false; - for (int t = 0; t < tileList.size(); t++) - { - if (tileList.get(t).equals(tile)) { - alreadyGot = true; - } + if (tileList.get(t).equals(tile)) { + alreadyGot = true; } - if (!alreadyGot) {tileList.add(tile);} } + if (!alreadyGot) {tileList.add(tile);} } - lookupValues(tileList, overwriteZeros); + lookupValues(tileList); // Finished _running = false; } - /** - * true if we need to set the altitude of this point - */ - private boolean needsAltitude(DataPoint point, boolean overwriteZeros) - { - if (!point.hasAltitude()) - { - return true; - } - if (overwriteZeros && point.getAltitude().getValue() == 0) - { - return true; - } - return false; - } - /** * Lookup the values from SRTM data * @param inTileList list of tiles to get * @param inOverwriteZeros true to overwrite zero altitude values */ - private void lookupValues(ArrayList inTileList, boolean inOverwriteZeros) + private void lookupValues(ArrayList inTileList) { UndoLookupSrtm undo = new UndoLookupSrtm(_app.getTrackInfo()); int numAltitudesFound = 0; @@ -202,7 +158,7 @@ public class LookupSrtmFunction extends GenericFunction implements Runnable errorMessage += "Tile "+tile.getTileName()+" is corrupted"; } - numAltitudesFound += applySrtmTimeToWholeTrack(tile, heights, rowSize, inOverwriteZeros); + numAltitudesFound += applySrtmTimeToWholeTrack(tile, heights, rowSize); } _progress.dispose(); @@ -239,53 +195,49 @@ public class LookupSrtmFunction extends GenericFunction implements Runnable * 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) + private int applySrtmTimeToWholeTrack(SrtmTile inTile, int[] inHeights, int inRowSize) { 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)) { - 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 { - 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) { - 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++; - } + 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; } - catch (ArrayIndexOutOfBoundsException obe) { - System.err.println("Point not in tile? lat=" + point.getLatitude().getDouble() + ", x=" + x + ", y=" + y + ", idx=" + idx1+"\n"); + // 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"); } } } -- 2.43.0