]> gitweb.fperrin.net Git - GpsPrune.git/blob - src/tim/prune/function/srtm/TileDownloader.java
Version 20.4, May 2021
[GpsPrune.git] / src / tim / prune / function / srtm / TileDownloader.java
1 package tim.prune.function.srtm;
2
3 import java.io.File;
4 import java.io.FileOutputStream;
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.net.URL;
8 import java.net.URLConnection;
9
10 import tim.prune.GpsPrune;
11 import tim.prune.config.Config;
12
13 /**
14  * Class to provide a download function for the Space Shuttle's SRTM data files.
15  * HGT files are downloaded into memory via HTTP and stored in the map cache.
16  */
17 public class TileDownloader
18 {
19         /** Possible results of the download */
20         public enum Result {DOWNLOADED, NOTHING_TO_DO, DOWNLOAD_FAILED, CACHE_FAILED};
21
22         /**
23          * Download a single tile of SRTM data
24          * @param inUrl remote URL to get
25          */
26         public Result downloadTile(URL inUrl)
27         {
28                 if (inUrl == null) {
29                         return Result.NOTHING_TO_DO;
30                 }
31
32                 // Check the cache is ok
33                 final String diskCachePath = Config.getConfigString(Config.KEY_DISK_CACHE);
34                 if (diskCachePath != null)
35                 {
36                         File srtmDir = new File(diskCachePath, "srtm");
37                         if (srtmDir.exists() && !srtmDir.isDirectory()) {
38                                 // exists but isn't a directory - can't be used
39                                 return Result.CACHE_FAILED;
40                         }
41                         if (!srtmDir.exists() && !srtmDir.mkdir()) {
42                                 // can't create the srtm directory
43                                 return Result.CACHE_FAILED;
44                         }
45                 }
46                 else {
47                         // no cache set up
48                         return Result.CACHE_FAILED;
49                 }
50
51                 // Define streams
52                 FileOutputStream outStream = null;
53                 InputStream inStream = null;
54                 Result result = Result.NOTHING_TO_DO;
55                 try
56                 {
57                         // See if we've already got this tile or not
58                         File outputFile = getFileToWrite(inUrl);
59                         if (outputFile != null)
60                         {
61                                 System.out.println("Download: Need to download: " + inUrl);
62                                 outStream = new FileOutputStream(outputFile);
63                                 URLConnection conn = inUrl.openConnection();
64                                 conn.setRequestProperty("User-Agent", "GpsPrune v" + GpsPrune.VERSION_NUMBER);
65                                 inStream = conn.getInputStream();
66                                 // Copy all the bytes to the file
67                                 int c;
68                                 while ((c = inStream.read()) != -1)
69                                 {
70                                         outStream.write(c);
71                                 }
72                                 result = Result.DOWNLOADED;
73                         }
74                 }
75                 catch (IOException ioe) {
76                         System.err.println(ioe.getClass().getName() + " - " + ioe.getMessage());
77                         result = Result.DOWNLOAD_FAILED;
78                 }
79                 // Make sure streams are closed
80                 try {inStream.close();} catch (Exception e) {}
81                 try {outStream.close();} catch (Exception e) {}
82
83                 return result;
84         }
85
86         /**
87          * See whether the SRTM file is already available locally
88          * @param inUrl URL for online resource
89          * @return file object to write to, or null if already there
90          */
91         private static File getFileToWrite(URL inUrl)
92         {
93                 String diskCachePath = Config.getConfigString(Config.KEY_DISK_CACHE);
94                 if (diskCachePath != null)
95                 {
96                         File srtmDir = new File(diskCachePath, "srtm");
97                         if (srtmDir.exists() && srtmDir.isDirectory() && srtmDir.canRead())
98                         {
99                                 File srtmFile = new File(srtmDir, new File(inUrl.getFile()).getName());
100                                 if (!srtmFile.exists() || !srtmFile.canRead() || srtmFile.length() <= 400) {
101                                         return srtmFile;
102                                 }
103                         }
104                 }
105                 return null;
106         }
107 }