]> gitweb.fperrin.net Git - GpsPrune.git/blob - src/tim/prune/function/srtm/LookupSrtmFunction.java
Version 20.4, May 2021
[GpsPrune.git] / src / tim / prune / function / srtm / LookupSrtmFunction.java
1 package tim.prune.function.srtm;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.IOException;
6 import java.net.URL;
7 import java.util.HashSet;
8 import java.util.zip.ZipEntry;
9 import java.util.zip.ZipInputStream;
10
11 import javax.swing.JOptionPane;
12
13 import tim.prune.App;
14 import tim.prune.DataSubscriber;
15 import tim.prune.GenericFunction;
16 import tim.prune.I18nManager;
17 import tim.prune.UpdateMessageBroker;
18 import tim.prune.config.Config;
19 import tim.prune.data.Altitude;
20 import tim.prune.data.DataPoint;
21 import tim.prune.data.Field;
22 import tim.prune.data.Track;
23 import tim.prune.data.UnitSetLibrary;
24 import tim.prune.gui.ProgressDialog;
25 import tim.prune.tips.TipManager;
26 import tim.prune.undo.UndoLookupSrtm;
27
28 /**
29  * Class to provide a lookup function for point altitudes using the Space
30  * Shuttle's SRTM data files. HGT files are downloaded into memory via HTTP and
31  * point altitudes can then be interpolated from the 3m grid data.
32  */
33 public class LookupSrtmFunction extends GenericFunction implements Runnable
34 {
35         /** Progress dialog */
36         private ProgressDialog _progress = null;
37         /** Track to process */
38         private Track _track = null;
39         /** Flag for whether this is a real track or a terrain one */
40         private boolean _normalTrack = true;
41         /** Flag set when any tiles had to be downloaded (rather than just loaded locally) */
42         private boolean _hadToDownload = false;
43         /** Count the number of tiles downloaded and cached */
44         private int _numCached = 0;
45         /** Flag to check whether this function is currently running or not */
46         private boolean _running = false;
47
48         /** Expected size of hgt file in bytes */
49         private static final long HGT_SIZE = 2884802L;
50         /** Altitude below which is considered void */
51         private static final int VOID_VAL = -32768;
52
53         /**
54          * Constructor
55          * @param inApp  App object
56          */
57         public LookupSrtmFunction(App inApp) {
58                 super(inApp);
59         }
60
61         /** @return name key */
62         public String getNameKey() {
63                 return "function.lookupsrtm";
64         }
65
66         /**
67          * Begin the lookup using the normal track
68          */
69         public void begin() {
70                 begin(_app.getTrackInfo().getTrack(), true);
71         }
72
73         /**
74          * Begin the lookup with an alternative track
75          * @param inAlternativeTrack
76          */
77         public void begin(Track inAlternativeTrack) {
78                 begin(inAlternativeTrack, false);
79         }
80
81         /**
82          * Begin the function with the given parameters
83          * @param inTrack track to process
84          * @param inNormalTrack true if this is a "normal" track, false for an artificially constructed one such as for terrain
85          */
86         private void begin(Track inTrack, boolean inNormalTrack)
87         {
88                 _running = true;
89                 _hadToDownload = false;
90                 if (_progress == null) {
91                         _progress = new ProgressDialog(_parentFrame, getNameKey());
92                 }
93                 _progress.show();
94                 _track = inTrack;
95                 _normalTrack = inNormalTrack;
96                 // start new thread for time-consuming part
97                 new Thread(this).start();
98         }
99
100         /**
101          * Run method using separate thread
102          */
103         public void run()
104         {
105                 boolean hasZeroAltitudePoints = false;
106                 boolean hasNonZeroAltitudePoints = false;
107                 // First, loop to see what kind of points we have
108                 for (int i = 0; i < _track.getNumPoints(); i++)
109                 {
110                         if (_track.getPoint(i).hasAltitude())
111                         {
112                                 if (_track.getPoint(i).getAltitude().getValue() == 0) {
113                                         hasZeroAltitudePoints = true;
114                                 }
115                                 else {
116                                         hasNonZeroAltitudePoints = true;
117                                 }
118                         }
119                 }
120                 // Should we overwrite the zero altitude values?
121                 boolean overwriteZeros = hasZeroAltitudePoints && !hasNonZeroAltitudePoints;
122                 // If non-zero values present as well, ask user whether to overwrite the zeros or not
123                 if (hasNonZeroAltitudePoints && hasZeroAltitudePoints && JOptionPane.showConfirmDialog(_parentFrame,
124                         I18nManager.getText("dialog.lookupsrtm.overwritezeros"), I18nManager.getText(getNameKey()),
125                         JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
126                 {
127                         overwriteZeros = true;
128                 }
129
130                 // Now loop again to extract the required tiles
131                 HashSet<SrtmTile> tileSet = new HashSet<SrtmTile>();
132                 for (int i = 0; i < _track.getNumPoints(); i++)
133                 {
134                         // Consider points which don't have altitudes or have zero values
135                         if (!_track.getPoint(i).hasAltitude()
136                                 || (overwriteZeros && _track.getPoint(i).getAltitude().getValue() == 0))
137                         {
138                                 tileSet.add(new SrtmTile(_track.getPoint(i)));
139                         }
140                 }
141                 lookupValues(tileSet, overwriteZeros);
142                 // Finished
143                 _running = false;
144                 // Show tip if lots of online lookups were necessary
145                 if (_hadToDownload) {
146                         _app.showTip(TipManager.Tip_DownloadSrtm);
147                 }
148                 else if (_numCached > 0) {
149                         showConfirmMessage(_numCached);
150                 }
151         }
152
153
154         /**
155          * Lookup the values from SRTM data
156          * @param inTileSet set of tiles to get
157          * @param inOverwriteZeros true to overwrite zero altitude values
158          */
159         private void lookupValues(HashSet<SrtmTile> inTileSet, boolean inOverwriteZeros)
160         {
161                 UndoLookupSrtm undo = new UndoLookupSrtm(_app.getTrackInfo());
162                 int numAltitudesFound = 0;
163                 TileFinder tileFinder = new TileFinder();
164                 String errorMessage = null;
165                 final int numTiles = inTileSet.size();
166
167                 // Update progress bar
168                 if (_progress != null)
169                 {
170                         _progress.setMaximum(numTiles);
171                         _progress.setValue(0);
172                 }
173                 int currentTileIndex = 0;
174                 _numCached = 0;
175                 for (SrtmTile tile : inTileSet)
176                 {
177                         URL url = tileFinder.getUrl(tile);
178                         if (url != null)
179                         {
180                                 try
181                                 {
182                                         // Set progress
183                                         _progress.setValue(currentTileIndex++);
184                                         final int ARRLENGTH = 1201 * 1201;
185                                         int[] heights = new int[ARRLENGTH];
186                                         // Open zipinputstream on url and check size
187                                         ZipInputStream inStream = getStreamToSrtmData(url);
188                                         boolean entryOk = false;
189                                         if (inStream != null)
190                                         {
191                                                 ZipEntry entry = inStream.getNextEntry();
192                                                 entryOk = (entry != null && entry.getSize() == HGT_SIZE);
193                                                 if (entryOk)
194                                                 {
195                                                         // Read entire file contents into one byte array
196                                                         for (int i = 0; i < ARRLENGTH; i++)
197                                                         {
198                                                                 heights[i] = inStream.read() * 256 + inStream.read();
199                                                                 if (heights[i] >= 32768) {heights[i] -= 65536;}
200                                                         }
201                                                 }
202                                                 // else {
203                                                 //      System.out.println("length not ok: " + entry.getSize());
204                                                 // }
205                                                 // Close stream from url
206                                                 inStream.close();
207                                         }
208
209                                         if (entryOk)
210                                         {
211                                                 numAltitudesFound += applySrtmTileToWholeTrack(tile, heights, inOverwriteZeros);
212                                         }
213                                 }
214                                 catch (IOException ioe) {
215                                         errorMessage = ioe.getClass().getName() + " - " + ioe.getMessage();
216                                 }
217                         }
218                 }
219
220                 _progress.dispose();
221                 if (_progress.isCancelled()) {
222                         return;
223                 }
224
225                 if (numAltitudesFound > 0)
226                 {
227                         // Inform app including undo information
228                         _track.requestRescale();
229                         UpdateMessageBroker.informSubscribers(DataSubscriber.DATA_ADDED_OR_REMOVED);
230                         // Don't update app if we're doing another track
231                         if (_normalTrack)
232                         {
233                                 _app.completeFunction(undo,
234                                         I18nManager.getTextWithNumber("confirm.lookupsrtm", numAltitudesFound));
235                         }
236                 }
237                 else if (errorMessage != null) {
238                         _app.showErrorMessageNoLookup(getNameKey(), errorMessage);
239                 }
240                 else if (numTiles > 0) {
241                         _app.showErrorMessage(getNameKey(), "error.lookupsrtm.nonefound");
242                 }
243                 else {
244                         _app.showErrorMessage(getNameKey(), "error.lookupsrtm.nonerequired");
245                 }
246         }
247
248         /**
249          * See whether the SRTM file is already available locally first, then try online
250          * @param inUrl URL for online resource
251          * @return ZipInputStream either on the local file or on the downloaded zip file
252          */
253         private ZipInputStream getStreamToSrtmData(URL inUrl)
254         throws IOException
255         {
256                 ZipInputStream localData = null;
257                 try {
258                         localData = getStreamToLocalHgtFile(inUrl);
259                 }
260                 catch (IOException ioe) {
261                         localData = null;
262                 }
263                 if (localData != null)
264                 {
265                         return localData;
266                 }
267                 // try to download to cache
268                 TileDownloader cacher = new TileDownloader();
269                 TileDownloader.Result result = cacher.downloadTile(inUrl);
270                 // System.out.println("Result: " + result);
271                 if (result == TileDownloader.Result.DOWNLOADED)
272                 {
273                         _numCached++;
274                         return getStreamToLocalHgtFile(inUrl);
275                 }
276                 // If we don't have a cache, we may be able to download it temporarily
277                 if (result != TileDownloader.Result.DOWNLOAD_FAILED)
278                 {
279                         _hadToDownload = true;
280                         return new ZipInputStream(inUrl.openStream());
281                 }
282                 // everything failed
283                 return null;
284         }
285
286         /**
287          * Get the SRTM file from the local cache, if available
288          * @param inUrl URL for online resource
289          * @return ZipInputStream on the local file or null if not there
290          */
291         private ZipInputStream getStreamToLocalHgtFile(URL inUrl)
292         throws IOException
293         {
294                 String diskCachePath = Config.getConfigString(Config.KEY_DISK_CACHE);
295                 if (diskCachePath != null)
296                 {
297                         File srtmDir = new File(diskCachePath, "srtm");
298                         if (srtmDir.exists() && srtmDir.isDirectory() && srtmDir.canRead())
299                         {
300                                 File srtmFile = new File(srtmDir, new File(inUrl.getFile()).getName());
301                                 if (srtmFile.exists() && srtmFile.isFile() && srtmFile.canRead()
302                                         && srtmFile.length() > 400)
303                                 {
304                                         // System.out.println("Lookup: Using file " + srtmFile.getAbsolutePath());
305                                         // File found, use this one
306                                         return new ZipInputStream(new FileInputStream(srtmFile));
307                                 }
308                         }
309                 }
310                 return null;
311         }
312
313         /**
314          * Given the height data read in from file, apply the given tile to all points
315          * in the track with missing altitude
316          * @param inTile tile being applied
317          * @param inHeights height data read in from file
318          * @param inOverwriteZeros true to overwrite zero altitude values
319          * @return number of altitudes found
320          */
321         private int applySrtmTileToWholeTrack(SrtmTile inTile, int[] inHeights, boolean inOverwriteZeros)
322         {
323                 int numAltitudesFound = 0;
324                 // Loop over all points in track, try to apply altitude from array
325                 for (int p = 0; p < _track.getNumPoints(); p++)
326                 {
327                         DataPoint point = _track.getPoint(p);
328                         if (!point.hasAltitude()
329                                 || (inOverwriteZeros && point.getAltitude().getValue() == 0))
330                         {
331                                 if (new SrtmTile(point).equals(inTile))
332                                 {
333                                         double x = (point.getLongitude().getDouble() - inTile.getLongitude()) * 1200;
334                                         double y = 1201 - (point.getLatitude().getDouble() - inTile.getLatitude()) * 1200;
335                                         int idx1 = ((int)y)*1201 + (int)x;
336                                         try
337                                         {
338                                                 int[] fouralts = {inHeights[idx1], inHeights[idx1+1], inHeights[idx1-1201], inHeights[idx1-1200]};
339                                                 int numVoids = (fouralts[0]==VOID_VAL?1:0) + (fouralts[1]==VOID_VAL?1:0)
340                                                         + (fouralts[2]==VOID_VAL?1:0) + (fouralts[3]==VOID_VAL?1:0);
341                                                 // if (numVoids > 0) System.out.println(numVoids + " voids found");
342                                                 double altitude = 0.0;
343                                                 switch (numVoids)
344                                                 {
345                                                         case 0: altitude = bilinearInterpolate(fouralts, x, y); break;
346                                                         case 1: altitude = bilinearInterpolate(fixVoid(fouralts), x, y); break;
347                                                         case 2:
348                                                         case 3: altitude = averageNonVoid(fouralts); break;
349                                                         default: altitude = VOID_VAL;
350                                                 }
351                                                 // Special case for terrain tracks, don't interpolate voids yet
352                                                 if (!_normalTrack && numVoids > 0) {
353                                                         altitude = VOID_VAL;
354                                                 }
355                                                 if (altitude != VOID_VAL)
356                                                 {
357                                                         point.setFieldValue(Field.ALTITUDE, ""+altitude, false);
358                                                         // depending on settings, this value may have been added as feet, we need to force metres
359                                                         point.getAltitude().reset(new Altitude((int)altitude, UnitSetLibrary.UNITS_METRES));
360                                                         numAltitudesFound++;
361                                                 }
362                                         }
363                                         catch (ArrayIndexOutOfBoundsException obe) {
364                                                 // System.err.println("lat=" + point.getLatitude().getDouble() + ", x=" + x + ", y=" + y + ", idx=" + idx1);
365                                         }
366                                 }
367                         }
368                 }
369                 return numAltitudesFound;
370         }
371
372         /**
373          * Perform a bilinear interpolation on the given altitude array
374          * @param inAltitudes array of four altitude values on corners of square (bl, br, tl, tr)
375          * @param inX x coordinate
376          * @param inY y coordinate
377          * @return interpolated altitude
378          */
379         private static double bilinearInterpolate(int[] inAltitudes, double inX, double inY)
380         {
381                 double alpha = inX - (int) inX;
382                 double beta  = 1 - (inY - (int) inY);
383                 double alt = (1-alpha)*(1-beta)*inAltitudes[0] + alpha*(1-beta)*inAltitudes[1]
384                         + (1-alpha)*beta*inAltitudes[2] + alpha*beta*inAltitudes[3];
385                 return alt;
386         }
387
388         /**
389          * Fix a single void in the given array by replacing it with the average of the others
390          * @param inAltitudes array of altitudes containing one void
391          * @return fixed array without voids
392          */
393         private static int[] fixVoid(int[] inAltitudes)
394         {
395                 int[] fixed = new int[inAltitudes.length];
396                 for (int i = 0; i < inAltitudes.length; i++)
397                 {
398                         if (inAltitudes[i] == VOID_VAL) {
399                                 fixed[i] = (int) Math.round(averageNonVoid(inAltitudes));
400                         }
401                         else {
402                                 fixed[i] = inAltitudes[i];
403                         }
404                 }
405                 return fixed;
406         }
407
408         /**
409          * Calculate the average of the non-void altitudes in the given array
410          * @param inAltitudes array of altitudes with one or more voids
411          * @return average of non-void altitudes
412          */
413         private static final double averageNonVoid(int[] inAltitudes)
414         {
415                 double totalAltitude = 0.0;
416                 int numAlts = 0;
417                 for (int i = 0; i < inAltitudes.length; i++)
418                 {
419                         if (inAltitudes[i] != VOID_VAL)
420                         {
421                                 totalAltitude += inAltitudes[i];
422                                 numAlts++;
423                         }
424                 }
425                 if (numAlts < 1) {return VOID_VAL;}
426                 return totalAltitude / numAlts;
427         }
428
429         /**
430          * @return true if a thread is currently running
431          */
432         public boolean isRunning()
433         {
434                 return _running;
435         }
436
437         private void showConfirmMessage(int numDownloaded)
438         {
439                 if (numDownloaded == 1)
440                 {
441                         JOptionPane.showMessageDialog(_parentFrame, I18nManager.getTextWithNumber("confirm.downloadsrtm.1", numDownloaded),
442                                 I18nManager.getText(getNameKey()), JOptionPane.INFORMATION_MESSAGE);
443                 }
444                 else if (numDownloaded > 1)
445                 {
446                         JOptionPane.showMessageDialog(_parentFrame, I18nManager.getTextWithNumber("confirm.downloadsrtm", numDownloaded),
447                                 I18nManager.getText(getNameKey()), JOptionPane.INFORMATION_MESSAGE);
448                 }
449         }
450 }