]> gitweb.fperrin.net Git - GpsPrune.git/blob - src/tim/prune/function/srtm/DownloadSrtmFunction.java
Use data from viewfinderpanoramas.org
[GpsPrune.git] / src / tim / prune / function / srtm / DownloadSrtmFunction.java
1 package tim.prune.function.srtm;
2
3 import java.util.ArrayList;
4
5 import javax.swing.JOptionPane;
6
7 import tim.prune.App;
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;
13
14 /**
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.
17  */
18 public class DownloadSrtmFunction extends GenericFunction implements Runnable
19 {
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;
25
26         /**
27          * Constructor
28          * @param inApp  App object
29          */
30         public DownloadSrtmFunction(App inApp, SrtmSource inSrtmSource) {
31                 super(inApp);
32                 _srtmSource = inSrtmSource;
33         }
34
35         /** @return name key */
36         public String getNameKey() {
37                 return "function.downloadsrtm."+_srtmSource.getName();
38         }
39
40         /**
41          * Begin the download
42          */
43         public void begin()
44         {
45                 if (! SrtmDiskCache.ensureCacheIsUsable())
46                 {
47                         _app.showErrorMessage(getNameKey(), "error.downloadsrtm.nocache");
48                         return;
49                 }
50                 if (! _srtmSource.isReadyToUse())
51                 {
52                         _app.showErrorMessage(getNameKey(), getNameKey() + ".needsetup");
53                         return;
54                 }
55
56                 _running = true;
57                 if (_progress == null) {
58                         _progress = new ProgressDialog(_parentFrame, getNameKey());
59                 }
60                 _progress.show();
61                 // start new thread for time-consuming part
62                 new Thread(this).start();
63         }
64
65         /**
66          * Run method using separate thread
67          */
68         public void run()
69         {
70                 ArrayList<SrtmTile> tileList = buildCoveringTiles();
71                 downloadTiles(tileList);
72                 // Finished
73                 _running = false;
74         }
75
76         private ArrayList<SrtmTile> buildCoveringTiles()
77         {
78                 // Compile list of tiles to get
79                 ArrayList<SrtmTile> tileList = new ArrayList<SrtmTile>();
80
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());
88
89                 for (int lon=minLon; lon<= maxLon; lon++)
90                 {
91                         for (int lat=minLat; lat <= maxLat; lat++)
92                         {
93                                 SrtmTile tile = new SrtmTile(lat, lon);
94                                 boolean alreadyGot = false;
95                                 for (int t = 0; t < tileList.size(); t++)
96                                 {
97                                         if (tileList.get(t).equals(tile)) {
98                                                 alreadyGot = true;
99                                         }
100                                 }
101                                 if (!alreadyGot) {tileList.add(tile);}
102                         }
103                 }
104
105                 return tileList;
106         }
107
108         /**
109          * Download the tiles of SRTM data
110          * @param inTileList list of tiles to get
111          */
112         private void downloadTiles(ArrayList<SrtmTile> inTileList)
113         {
114                 String errorMessage = "";
115                 // Update progress bar
116                 if (_progress != null)
117                 {
118                         _progress.setMaximum(inTileList.size());
119                         _progress.setValue(0);
120                 }
121
122                 int numDownloaded = 0;
123                 for (int t=0; t<inTileList.size() && !_progress.isCancelled(); t++)
124                 {
125                         if (_srtmSource.isCached(inTileList.get(t)))
126                         {
127                                 System.out.println(inTileList.get(t).getTileName()+" already in cache, nothing to do");
128                                 continue;
129                         }
130
131                         boolean success;
132                         try
133                         {
134                                 success = _srtmSource.downloadTile(inTileList.get(t));
135                                 if (success)
136                                 {
137                                         numDownloaded++;
138                                 }
139                                 // Set progress
140                                 _progress.setValue(t + 1);
141                         }
142                         catch (SrtmSourceException e)
143                         {
144                                 e.printStackTrace();
145                                 errorMessage += e.getMessage();
146                         }
147                 }
148
149                 _progress.dispose();
150                 if (_progress.isCancelled()) {
151                         return;
152                 }
153
154                 if (! errorMessage.equals("")) {
155                         _app.showErrorMessageNoLookup(getNameKey(), errorMessage);
156                         return;
157                 }
158                 if (numDownloaded == 1)
159                 {
160                         JOptionPane.showMessageDialog(_parentFrame, I18nManager.getTextWithNumber("confirm.downloadsrtm.1", numDownloaded),
161                                 I18nManager.getText(getNameKey()), JOptionPane.INFORMATION_MESSAGE);
162                 }
163                 else if (numDownloaded > 1)
164                 {
165                         JOptionPane.showMessageDialog(_parentFrame, I18nManager.getTextWithNumber("confirm.downloadsrtm", numDownloaded),
166                                 I18nManager.getText(getNameKey()), JOptionPane.INFORMATION_MESSAGE);
167                 }
168                 else if (inTileList.size() > 0) {
169                         JOptionPane.showMessageDialog(_parentFrame, I18nManager.getText("confirm.downloadsrtm.none"));
170                 }
171         }
172 }