From: Frédéric Perrin Date: Sat, 15 Dec 2018 22:40:07 +0000 (+0000) Subject: Allow to overwrite altitudes from SRTM data X-Git-Url: http://gitweb.fperrin.net/?p=GpsPrune.git;a=commitdiff_plain;h=refs%2Fheads%2Fperso_changes Allow to overwrite altitudes from SRTM data --- diff --git a/src/tim/prune/FunctionLibrary.java b/src/tim/prune/FunctionLibrary.java index 0441826..ef3d573 100644 --- a/src/tim/prune/FunctionLibrary.java +++ b/src/tim/prune/FunctionLibrary.java @@ -103,6 +103,7 @@ public abstract class FunctionLibrary public static GenericFunction FUNCTION_DELETE_BY_DATE = null; public static SingleNumericParameterFunction FUNCTION_INTERPOLATE = null; public static GenericFunction FUNCTION_LOOKUP_SRTM = null; + public static GenericFunction FUNCTION_OVERWRITE_SRTM = null; public static GenericFunction FUNCTION_DOWNLOAD_SRTM = null; public static GenericFunction FUNCTION_NEARBY_WIKIPEDIA = null; public static GenericFunction FUNCTION_SEARCH_WIKIPEDIA = null; @@ -182,7 +183,8 @@ public abstract class FunctionLibrary FUNCTION_MARK_IN_RECTANGLE = new MarkPointsInRectangleFunction(inApp); FUNCTION_DELETE_BY_DATE = new DeleteByDateFunction(inApp); FUNCTION_INTERPOLATE = new InterpolateFunction(inApp); - FUNCTION_LOOKUP_SRTM = new LookupSrtmFunction(inApp); + FUNCTION_LOOKUP_SRTM = new LookupSrtmFunction(inApp, false); + FUNCTION_OVERWRITE_SRTM = new LookupSrtmFunction(inApp, true); FUNCTION_DOWNLOAD_SRTM = new DownloadSrtmFunction(inApp); FUNCTION_NEARBY_WIKIPEDIA = new GetWikipediaFunction(inApp); FUNCTION_SEARCH_WIKIPEDIA = new SearchWikipediaNames(inApp); diff --git a/src/tim/prune/function/srtm/LookupSrtmFunction.java b/src/tim/prune/function/srtm/LookupSrtmFunction.java index 0ae2e45..51c587e 100644 --- a/src/tim/prune/function/srtm/LookupSrtmFunction.java +++ b/src/tim/prune/function/srtm/LookupSrtmFunction.java @@ -32,6 +32,8 @@ import tim.prune.undo.UndoLookupSrtm; */ public class LookupSrtmFunction extends GenericFunction implements Runnable { + /** Whether to overwrite all altitudes, or only fill in missing data */ + private boolean _overwriteAlt = false; /** Progress dialog */ private ProgressDialog _progress = null; /** Track to process */ @@ -53,13 +55,18 @@ public class LookupSrtmFunction extends GenericFunction implements Runnable * Constructor * @param inApp App object */ - public LookupSrtmFunction(App inApp) { + public LookupSrtmFunction(App inApp, boolean overwriteAlt) { super(inApp); + _overwriteAlt = overwriteAlt; } /** @return name key */ public String getNameKey() { - return "function.lookupsrtm"; + if (_overwriteAlt) { + return "function.overwritesrtm"; + } else { + return "function.lookupsrtm"; + } } /** @@ -103,37 +110,14 @@ 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; - } + + boolean overwriteZeros = overwriteZeros(); // 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 (!_track.getPoint(i).hasAltitude() - || (overwriteZeros && _track.getPoint(i).getAltitude().getValue() == 0)) + if (needsAltitude(_track.getPoint(i), overwriteZeros)) { SrtmTile tile = new SrtmTile(_track.getPoint(i)); boolean alreadyGot = false; @@ -155,15 +139,69 @@ public class LookupSrtmFunction extends GenericFunction implements Runnable } } + /** + * true if we need to set the altitude of this point + */ + private boolean needsAltitude(DataPoint point, boolean overwriteZeros) + { + if (_overwriteAlt) + { + return true; + } + if (!point.hasAltitude()) + { + return true; + } + if (overwriteZeros && point.getAltitude().getValue() == 0) + { + return true; + } + return false; + } + + private boolean overwriteZeros() + { + // nothing to do if we overwrite all altitudes anyway + if (_overwriteAlt) { + return true; + } + + 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; + } + return overwriteZeros; + } + /** * 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, boolean overwriteZeros) { - UndoLookupSrtm undo = new UndoLookupSrtm(_app.getTrackInfo()); + UndoLookupSrtm undo = new UndoLookupSrtm(_app.getTrackInfo(), _overwriteAlt); int numAltitudesFound = 0; // Update progress bar if (_progress != null) @@ -214,8 +252,7 @@ public class LookupSrtmFunction extends GenericFunction implements Runnable for (int p = 0; p < _track.getNumPoints(); p++) { DataPoint point = _track.getPoint(p); - if (!point.hasAltitude() - || (inOverwriteZeros && point.getAltitude().getValue() == 0)) + if (needsAltitude(point, overwriteZeros)) { if (new SrtmTile(point).equals(tile)) { diff --git a/src/tim/prune/gui/MenuManager.java b/src/tim/prune/gui/MenuManager.java index bce3b4d..7270400 100644 --- a/src/tim/prune/gui/MenuManager.java +++ b/src/tim/prune/gui/MenuManager.java @@ -93,6 +93,7 @@ public class MenuManager implements DataSubscriber private JMenuItem _getGpsiesItem = null; private JMenuItem _uploadGpsiesItem = null; private JMenuItem _lookupSrtmItem = null; + private JMenuItem _overwriteSrtmItem = null; private JMenuItem _downloadSrtmItem = null; private JMenuItem _nearbyWikipediaItem = null; private JMenuItem _nearbyOsmPoiItem = null; @@ -257,6 +258,8 @@ public class MenuManager implements DataSubscriber // SRTM _lookupSrtmItem = makeMenuItem(FunctionLibrary.FUNCTION_LOOKUP_SRTM, false); onlineMenu.add(_lookupSrtmItem); + _overwriteSrtmItem = makeMenuItem(FunctionLibrary.FUNCTION_OVERWRITE_SRTM, false); + onlineMenu.add(_overwriteSrtmItem); _downloadSrtmItem = makeMenuItem(FunctionLibrary.FUNCTION_DOWNLOAD_SRTM, false); onlineMenu.add(_downloadSrtmItem); // Get gpsies tracks @@ -883,6 +886,7 @@ public class MenuManager implements DataSubscriber _getGpsiesItem.setEnabled(hasData); _uploadGpsiesItem.setEnabled(hasData && _track.hasTrackPoints()); _lookupSrtmItem.setEnabled(hasData); + _overwriteSrtmItem.setEnabled(hasData); _nearbyWikipediaItem.setEnabled(hasData); _nearbyOsmPoiItem.setEnabled(hasData); _downloadOsmItem.setEnabled(hasData); diff --git a/src/tim/prune/lang/prune-texts_en.properties b/src/tim/prune/lang/prune-texts_en.properties index 7693926..7f4673d 100644 --- a/src/tim/prune/lang/prune-texts_en.properties +++ b/src/tim/prune/lang/prune-texts_en.properties @@ -112,7 +112,8 @@ function.sewsegments=Sew track segments together function.createmarkerwaypoints=Create marker waypoints function.getgpsies=Get Gpsies tracks function.uploadgpsies=Upload track to Gpsies -function.lookupsrtm=Get altitudes from SRTM +function.lookupsrtm=Set missing altitudes from SRTM +function.overwritesrtm=Set all altitudes from STRM function.downloadsrtm=Download SRTM tiles function.getwikipedia=Get nearby Wikipedia articles function.searchwikipedianames=Search Wikipedia by name diff --git a/src/tim/prune/undo/UndoLookupSrtm.java b/src/tim/prune/undo/UndoLookupSrtm.java index d028b82..b51e334 100644 --- a/src/tim/prune/undo/UndoLookupSrtm.java +++ b/src/tim/prune/undo/UndoLookupSrtm.java @@ -16,13 +16,16 @@ public class UndoLookupSrtm implements UndoOperation private DataPoint[] _points; /** Altitude strings if present */ private String[] _altitudes; + /** Whether all altitudes were overwriten (in which case + * _points will have the full track) */ + boolean overwriteAlt; /** * Constructor * @param inTrackInfo track info object */ - public UndoLookupSrtm(TrackInfo inTrackInfo) + public UndoLookupSrtm(TrackInfo inTrackInfo, boolean overwriteAlt) { Track track = inTrackInfo.getTrack(); int numPoints = track.getNumPoints(); @@ -32,7 +35,7 @@ public class UndoLookupSrtm implements UndoOperation for (int i=0; i