1 package tim.prune.function.srtm;
3 import java.util.ArrayList;
5 import javax.swing.JOptionPane;
8 import tim.prune.GenericFunction;
9 import tim.prune.GpsPrune;
10 import tim.prune.I18nManager;
11 import tim.prune.data.DoubleRange;
12 import tim.prune.gui.ProgressDialog;
15 * Class to provide a download function for the Space Shuttle's SRTM data files.
16 * HGT files are downloaded into memory via HTTP and stored in the map cache.
18 public class DownloadSrtmFunction extends GenericFunction implements Runnable
20 /** Progress dialog */
21 private ProgressDialog _progress = null;
22 /** Flag to check whether this function is currently running or not */
23 private boolean _running = false;
24 private SrtmSource _srtmSource = null;
28 * @param inApp App object
30 public DownloadSrtmFunction(App inApp, SrtmSource inSrtmSource) {
32 _srtmSource = inSrtmSource;
35 /** @return name key */
36 public String getNameKey() {
37 return "function.downloadsrtm."+_srtmSource.getName();
45 if (! SrtmDiskCache.ensureCacheIsUsable())
47 _app.showErrorMessage(getNameKey(), "error.downloadsrtm.nocache");
50 if (! _srtmSource.isReadyToUse())
52 _app.showErrorMessage(getNameKey(), getNameKey() + ".needsetup");
57 if (_progress == null) {
58 _progress = new ProgressDialog(_parentFrame, getNameKey());
61 // start new thread for time-consuming part
62 new Thread(this).start();
66 * Run method using separate thread
70 ArrayList<SrtmTile> tileList = buildCoveringTiles();
71 downloadTiles(tileList);
76 private ArrayList<SrtmTile> buildCoveringTiles()
78 // Compile list of tiles to get
79 ArrayList<SrtmTile> tileList = new ArrayList<SrtmTile>();
81 // First, loop to see which tiles are needed
82 DoubleRange lonRange = _app.getTrackInfo().getTrack().getLonRange();
83 DoubleRange latRange = _app.getTrackInfo().getTrack().getLatRange();
84 final int minLon = (int) Math.floor(lonRange.getMinimum());
85 final int maxLon = (int) Math.floor(lonRange.getMaximum());
86 final int minLat = (int) Math.floor(latRange.getMinimum());
87 final int maxLat = (int) Math.floor(latRange.getMaximum());
89 for (int lon=minLon; lon<= maxLon; lon++)
91 for (int lat=minLat; lat <= maxLat; lat++)
93 SrtmTile tile = new SrtmTile(lat, lon);
94 boolean alreadyGot = false;
95 for (int t = 0; t < tileList.size(); t++)
97 if (tileList.get(t).equals(tile)) {
101 if (!alreadyGot) {tileList.add(tile);}
109 * Download the tiles of SRTM data
110 * @param inTileList list of tiles to get
112 private void downloadTiles(ArrayList<SrtmTile> inTileList)
114 String errorMessage = "";
115 // Update progress bar
116 if (_progress != null)
118 _progress.setMaximum(inTileList.size());
119 _progress.setValue(0);
122 int numDownloaded = 0;
123 for (int t=0; t<inTileList.size() && !_progress.isCancelled(); t++)
125 if (_srtmSource.isCached(inTileList.get(t)))
127 System.out.println(inTileList.get(t).getTileName()+" already in cache, nothing to do");
134 success = _srtmSource.downloadTile(inTileList.get(t));
140 _progress.setValue(t + 1);
142 catch (SrtmSourceException e)
144 errorMessage += e.getMessage();
149 if (_progress.isCancelled()) {
153 if (! errorMessage.equals("")) {
154 _app.showErrorMessageNoLookup(getNameKey(), errorMessage);
157 if (numDownloaded == 1)
159 JOptionPane.showMessageDialog(_parentFrame, I18nManager.getTextWithNumber("confirm.downloadsrtm.1", numDownloaded),
160 I18nManager.getText(getNameKey()), JOptionPane.INFORMATION_MESSAGE);
162 else if (numDownloaded > 1)
164 JOptionPane.showMessageDialog(_parentFrame, I18nManager.getTextWithNumber("confirm.downloadsrtm", numDownloaded),
165 I18nManager.getText(getNameKey()), JOptionPane.INFORMATION_MESSAGE);
167 else if (inTileList.size() > 0) {
168 JOptionPane.showMessageDialog(_parentFrame, I18nManager.getText("confirm.downloadsrtm.none"));