]> gitweb.fperrin.net Git - GpsPrune.git/commitdiff
Allow to overwrite altitudes from SRTM data perso_changes
authorFrédéric Perrin <fred@fperrin.net>
Sat, 15 Dec 2018 22:40:07 +0000 (22:40 +0000)
committerFrédéric Perrin <fred@fperrin.net>
Sat, 5 Oct 2019 15:40:58 +0000 (16:40 +0100)
src/tim/prune/FunctionLibrary.java
src/tim/prune/function/srtm/LookupSrtmFunction.java
src/tim/prune/gui/MenuManager.java
src/tim/prune/lang/prune-texts_en.properties
src/tim/prune/undo/UndoLookupSrtm.java

index 0441826164d930de8b79acb7d45fd84a742d33ce..ef3d57338bc528024447aa6b62a9ba1135fbfbee 100644 (file)
@@ -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);
index 0ae2e45c04a42a1f76c9b22db13677b0be0656ad..51c587ee6ac9ee4fc07cf8e594ea3b49c50259a6 100644 (file)
@@ -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<SrtmTile> tileList = new ArrayList<SrtmTile>();
-               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<SrtmTile> inTileList, boolean inOverwriteZeros)
+       private void lookupValues(ArrayList<SrtmTile> 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))
                                                                {
index bce3b4d67b0a6df980b66882f175245af1134155..72704002e5811a1e000a628f9ee687d0bcd6f0ed 100644 (file)
@@ -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);
index 76939263c9d8af07fa36db1f1686cf99550180dd..7f4673d847e3bf1c43f2baf5f495abb329521731 100644 (file)
@@ -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
index d028b82a568a98da8c7b46ea8d7539f767c891c1..b51e3348485095dda2fade3aaa5b34a92632f776 100644 (file)
@@ -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<numPoints; i++)
                {
                        DataPoint point = track.getPoint(i);
-                       if (!point.hasAltitude() || point.getAltitude().getValue() == 0)
+                       if (overwriteAlt || !point.hasAltitude() || point.getAltitude().getValue() == 0)
                        {
                                _points[i] = point;
                                if (point.hasAltitude()) {