]> gitweb.fperrin.net Git - GpsPrune.git/blobdiff - tim/prune/function/srtm/LookupSrtmFunction.java
Version 15, March 2013
[GpsPrune.git] / tim / prune / function / srtm / LookupSrtmFunction.java
index 30183d2d6cd13c0e780a018bc4ca6dd1ba2444b5..4d13348d3c4be742656f3b3690c7a9db09609ca8 100644 (file)
@@ -1,22 +1,12 @@
 package tim.prune.function.srtm;
 
-import java.awt.BorderLayout;
-import java.awt.Component;
-import java.awt.Dimension;
-import java.awt.FlowLayout;
-import java.awt.event.ActionEvent;
-import java.awt.event.ActionListener;
 import java.io.IOException;
 import java.net.URL;
 import java.util.ArrayList;
 import java.util.zip.ZipEntry;
 import java.util.zip.ZipInputStream;
 
-import javax.swing.JButton;
-import javax.swing.JDialog;
-import javax.swing.JLabel;
-import javax.swing.JPanel;
-import javax.swing.JProgressBar;
+import javax.swing.JOptionPane;
 
 import tim.prune.App;
 import tim.prune.DataSubscriber;
@@ -26,6 +16,7 @@ import tim.prune.UpdateMessageBroker;
 import tim.prune.data.DataPoint;
 import tim.prune.data.Field;
 import tim.prune.data.Track;
+import tim.prune.gui.ProgressDialog;
 import tim.prune.undo.UndoLookupSrtm;
 
 /**
@@ -36,18 +27,15 @@ import tim.prune.undo.UndoLookupSrtm;
  */
 public class LookupSrtmFunction extends GenericFunction implements Runnable
 {
-       /** function dialog */
-       private JDialog _dialog = null;
-       /** Progress bar for function */
-       private JProgressBar _progressBar = null;
-       /** Cancel flag */
-       private boolean _cancelled = false;
+       /** Progress dialog */
+       ProgressDialog _progress = null;
 
        /** Expected size of hgt file in bytes */
        private static final long HGT_SIZE = 2884802L;
        /** Altitude below which is considered void */
        private static final int VOID_VAL = -32768;
 
+
        /**
         * Constructor
         * @param inApp App object
@@ -67,61 +55,54 @@ public class LookupSrtmFunction extends GenericFunction implements Runnable
         */
        public void begin()
        {
-               if (_dialog == null)
+               if (_progress == null)
                {
-                       _dialog = new JDialog(_parentFrame, I18nManager.getText(getNameKey()), false);
-                       _dialog.setLocationRelativeTo(_parentFrame);
-                       _dialog.getContentPane().add(makeDialogComponents());
-                       _dialog.pack();
+                       _progress = new ProgressDialog(_parentFrame, getNameKey());
                }
-               _progressBar.setMinimum(0);
-               _progressBar.setMaximum(100);
-               _progressBar.setValue(20);
-               _cancelled = false;
+               _progress.show();
                // start new thread for time-consuming part
                new Thread(this).start();
        }
 
 
-       /**
-        * Make the dialog components
-        * @return the GUI components for the dialog
-        */
-       private Component makeDialogComponents()
-       {
-               JPanel dialogPanel = new JPanel();
-               dialogPanel.setLayout(new BorderLayout());
-               dialogPanel.add(new JLabel(I18nManager.getText("confirm.running")), BorderLayout.NORTH);
-               _progressBar = new JProgressBar();
-               _progressBar.setPreferredSize(new Dimension(250, 30));
-               dialogPanel.add(_progressBar, BorderLayout.CENTER);
-               // Cancel button at the bottom
-               JPanel buttonPanel = new JPanel();
-               buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
-               JButton cancelButton = new JButton(I18nManager.getText("button.cancel"));
-               cancelButton.addActionListener(new ActionListener() {
-                       public void actionPerformed(ActionEvent e) {
-                               _cancelled = true;
-                       }
-               });
-               buttonPanel.add(cancelButton);
-               dialogPanel.add(buttonPanel, BorderLayout.SOUTH);
-               return dialogPanel;
-       }
-
        /**
         * Run method using separate thread
         */
        public void run()
        {
-               _dialog.setVisible(true);
                // Compile list of tiles to get
                Track track = _app.getTrackInfo().getTrack();
                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++)
                {
-                       // Only consider points which don't have altitudes already
-                       if (!track.getPoint(i).hasAltitude())
+                       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 (!track.getPoint(i).hasAltitude() || (overwriteZeros && track.getPoint(i).getAltitude().getValue() == 0))
                        {
                                SrtmTile tile = new SrtmTile(track.getPoint(i));
                                boolean alreadyGot = false;
@@ -133,22 +114,33 @@ public class LookupSrtmFunction extends GenericFunction implements Runnable
                                if (!alreadyGot) {tileList.add(tile);}
                        }
                }
+               lookupValues(tileList, 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)
+       {
+               Track track = _app.getTrackInfo().getTrack();
                UndoLookupSrtm undo = new UndoLookupSrtm(_app.getTrackInfo());
                int numAltitudesFound = 0;
                // Update progress bar
-               _progressBar.setMaximum(tileList.size());
-               _progressBar.setIndeterminate(tileList.size() <= 1);
-               _progressBar.setValue(0);
+               _progress.setMaximum(inTileList.size());
+               _progress.setValue(0);
+               String errorMessage = null;
                // Get urls for each tile
-               URL[] urls = TileFinder.getUrls(tileList);
-               for (int t=0; t<tileList.size() && !_cancelled; t++)
+               URL[] urls = TileFinder.getUrls(inTileList);
+               for (int t=0; t<inTileList.size() && !_progress.isCancelled(); t++)
                {
                        if (urls[t] != null)
                        {
-                               SrtmTile tile = tileList.get(t);
-                               // System.out.println("tile " + t + " of " + tileList.size() + " = " + urls[t].toString());
-                               try {
-                                       _progressBar.setValue(t);
+                               SrtmTile tile = inTileList.get(t);
+                               try
+                               {
+                                       _progress.setValue(t);
                                        final int ARRLENGTH = 1201*1201;
                                        int[] heights = new int[ARRLENGTH];
                                        // Open zipinputstream on url and check size
@@ -175,7 +167,7 @@ public class LookupSrtmFunction extends GenericFunction implements Runnable
                                                for (int p=0; p<track.getNumPoints(); p++)
                                                {
                                                        DataPoint point = track.getPoint(p);
-                                                       if (!point.hasAltitude() || point.getAltitude().getValue() == 0) {
+                                                       if (!point.hasAltitude() || (inOverwriteZeros && point.getAltitude().getValue() == 0)) {
                                                                if (new SrtmTile(point).equals(tile))
                                                                {
                                                                        double x = (point.getLongitude().getDouble() - tile.getLongitude()) * 1200;
@@ -208,11 +200,12 @@ public class LookupSrtmFunction extends GenericFunction implements Runnable
                                        }
                                }
                                catch (IOException ioe) {
-                                       //System.err.println("eek - " + ioe.getMessage());
+                                       errorMessage = ioe.getClass().getName() + " - " + ioe.getMessage();
                                }
                        }
                }
-               _dialog.dispose();
+               _progress.dispose();
+               if (_progress.isCancelled()) {return;}
                if (numAltitudesFound > 0)
                {
                        // Inform app including undo information
@@ -221,8 +214,14 @@ public class LookupSrtmFunction extends GenericFunction implements Runnable
                        _app.completeFunction(undo, I18nManager.getText("confirm.lookupsrtm1") + " " + numAltitudesFound
                                + " " + I18nManager.getText("confirm.lookupsrtm2"));
                }
+               else if (errorMessage != null) {
+                       _app.showErrorMessageNoLookup(getNameKey(), errorMessage);
+               }
+               else if (inTileList.size() > 0) {
+                       _app.showErrorMessage(getNameKey(), "error.lookupsrtm.nonefound");
+               }
                else {
-                       _app.showErrorMessage(getNameKey(), "error.lookupsrtm.none");
+                       _app.showErrorMessage(getNameKey(), "error.lookupsrtm.nonerequired");
                }
        }