X-Git-Url: http://gitweb.fperrin.net/?p=GpsPrune.git;a=blobdiff_plain;f=src%2Ftim%2Fprune%2Ffunction%2Fsrtm%2FLookupSrtmFunction.java;h=c5986dba48710517b5432d8a9776370764857ac5;hp=c48af016b889f0df84a094de2df84db0db0ef272;hb=38a0c12649e9104d0d40ce93a932b9086011f8c8;hpb=8b20e3e027058cdf6ff52993ee5576193d08667a diff --git a/src/tim/prune/function/srtm/LookupSrtmFunction.java b/src/tim/prune/function/srtm/LookupSrtmFunction.java index c48af01..c5986db 100644 --- a/src/tim/prune/function/srtm/LookupSrtmFunction.java +++ b/src/tim/prune/function/srtm/LookupSrtmFunction.java @@ -4,7 +4,7 @@ import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.net.URL; -import java.util.ArrayList; +import java.util.HashSet; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; @@ -40,6 +40,8 @@ public class LookupSrtmFunction extends GenericFunction implements Runnable private boolean _normalTrack = true; /** Flag set when any tiles had to be downloaded (rather than just loaded locally) */ private boolean _hadToDownload = false; + /** Count the number of tiles downloaded and cached */ + private int _numCached = 0; /** Flag to check whether this function is currently running or not */ private boolean _running = false; @@ -100,8 +102,6 @@ public class LookupSrtmFunction extends GenericFunction implements Runnable */ public void run() { - // 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 @@ -128,64 +128,63 @@ public class LookupSrtmFunction extends GenericFunction implements Runnable } // Now loop again to extract the required tiles + HashSet tileSet = new HashSet(); 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; - for (int t = 0; t < tileList.size(); t++) - { - if (tileList.get(t).equals(tile)) { - alreadyGot = true; - } - } - if (!alreadyGot) {tileList.add(tile);} + tileSet.add(new SrtmTile(_track.getPoint(i))); } } - lookupValues(tileList, overwriteZeros); + lookupValues(tileSet, overwriteZeros); // Finished _running = false; // Show tip if lots of online lookups were necessary if (_hadToDownload) { _app.showTip(TipManager.Tip_DownloadSrtm); } + else if (_numCached > 0) { + showConfirmMessage(_numCached); + } } /** * Lookup the values from SRTM data - * @param inTileList list of tiles to get + * @param inTileSet set of tiles to get * @param inOverwriteZeros true to overwrite zero altitude values */ - private void lookupValues(ArrayList inTileList, boolean inOverwriteZeros) + private void lookupValues(HashSet inTileSet, boolean inOverwriteZeros) { UndoLookupSrtm undo = new UndoLookupSrtm(_app.getTrackInfo()); int numAltitudesFound = 0; + TileFinder tileFinder = new TileFinder(); + String errorMessage = null; + final int numTiles = inTileSet.size(); + // Update progress bar if (_progress != null) { - _progress.setMaximum(inTileList.size()); + _progress.setMaximum(numTiles); _progress.setValue(0); } - String errorMessage = null; - // Get urls for each tile - URL[] urls = TileFinder.getUrls(inTileList); - for (int t=0; t 0) { + else if (numTiles > 0) { _app.showErrorMessage(getNameKey(), "error.lookupsrtm.nonefound"); } else { @@ -251,7 +250,45 @@ public class LookupSrtmFunction extends GenericFunction implements Runnable * @param inUrl URL for online resource * @return ZipInputStream either on the local file or on the downloaded zip file */ - private ZipInputStream getStreamToHgtFile(URL inUrl) + private ZipInputStream getStreamToSrtmData(URL inUrl) + throws IOException + { + ZipInputStream localData = null; + try { + localData = getStreamToLocalHgtFile(inUrl); + } + catch (IOException ioe) { + localData = null; + } + if (localData != null) + { + return localData; + } + // try to download to cache + TileDownloader cacher = new TileDownloader(); + TileDownloader.Result result = cacher.downloadTile(inUrl); + System.out.println("Result: " + result); + if (result == TileDownloader.Result.DOWNLOADED) + { + _numCached++; + return getStreamToLocalHgtFile(inUrl); + } + // If we don't have a cache, we may be able to download it temporarily + if (result != TileDownloader.Result.DOWNLOAD_FAILED) + { + _hadToDownload = true; + return new ZipInputStream(inUrl.openStream()); + } + // everything failed + return null; + } + + /** + * Get the SRTM file from the local cache, if available + * @param inUrl URL for online resource + * @return ZipInputStream on the local file or null if not there + */ + private ZipInputStream getStreamToLocalHgtFile(URL inUrl) throws IOException { String diskCachePath = Config.getConfigString(Config.KEY_DISK_CACHE); @@ -270,10 +307,7 @@ public class LookupSrtmFunction extends GenericFunction implements Runnable } } } - // System.out.println("Lookup: Trying online: " + inUrl.toString()); - _hadToDownload = true; - // MAYBE: Only download if we're in online mode? - return new ZipInputStream(inUrl.openStream()); + return null; } /** @@ -399,4 +433,18 @@ public class LookupSrtmFunction extends GenericFunction implements Runnable { return _running; } + + private void showConfirmMessage(int numDownloaded) + { + if (numDownloaded == 1) + { + JOptionPane.showMessageDialog(_parentFrame, I18nManager.getTextWithNumber("confirm.downloadsrtm.1", numDownloaded), + I18nManager.getText(getNameKey()), JOptionPane.INFORMATION_MESSAGE); + } + else if (numDownloaded > 1) + { + JOptionPane.showMessageDialog(_parentFrame, I18nManager.getTextWithNumber("confirm.downloadsrtm", numDownloaded), + I18nManager.getText(getNameKey()), JOptionPane.INFORMATION_MESSAGE); + } + } }