]> gitweb.fperrin.net Git - GpsPrune.git/blob - src/tim/prune/function/srtm/SrtmGl1Source.java
Refactor and add comments
[GpsPrune.git] / src / tim / prune / function / srtm / SrtmGl1Source.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.CookieHandler;
8 import java.net.CookieManager;
9 import java.net.CookiePolicy;
10 import java.net.MalformedURLException;
11 import java.net.URL;
12 import java.net.HttpURLConnection;
13
14 import tim.prune.App;
15 import tim.prune.GpsPrune;
16 import tim.prune.config.Config;
17
18 /**
19  * Create an account at: https://urs.earthdata.nasa.gov/users/new
20  * Data policy: https://lpdaac.usgs.gov/data/data-citation-and-policies/
21  *
22  */
23
24 public class SrtmGl1Source extends SrtmSource {
25         /** URL prefix for all tiles */
26         private static final String URL_PREFIX = "https://e4ftl01.cr.usgs.gov/MEASURES/SRTMGL1.003/2000.02.11/";
27         /** Auth URL */
28         private static final String AUTH_URL = "urs.earthdata.nasa.gov";
29
30
31         public SrtmGl1Source()
32         {
33                 CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
34         }
35
36         public String getName()
37         {
38                 return "SRTMGL1_v003";
39         }
40
41         protected String getSourceExtension()
42         {
43                 return ".SRTMGL1.hgt.zip";
44         }
45
46         private URL buildUrl(SrtmTile inTile)
47                 throws SrtmSourceException
48         {
49                 try {
50                         return new URL(URL_PREFIX + inTile.getTileName() + getSourceExtension());
51                 }
52                 catch (MalformedURLException e)
53                 {
54                         throw new SrtmSourceException(e.getMessage());
55                 }
56         }
57
58         public boolean isReadyToUse()
59         {
60                 return getAuth() != null;
61         }
62
63         private String getAuth()
64         {
65                 String authString = Config.getConfigString(Config.KEY_EARTHDATA_AUTH);
66                 if (authString != null)
67                 {
68                         return "Basic " + authString; 
69                 }
70                 else
71                 {
72                         return null;
73                 }
74         }
75
76         public boolean downloadTile(SrtmTile inTile)
77                 throws SrtmSourceException
78         {
79                 return downloadTile(inTile, getAuth());
80         }
81
82         private boolean downloadTile(SrtmTile inTile, String auth)
83                 throws SrtmSourceException
84         {
85                 URL tileUrl = buildUrl(inTile);
86                 File outputFile = getCacheFileName(inTile);
87                 try
88                 {
89                         HttpURLConnection conn = (HttpURLConnection) tileUrl.openConnection();
90                         // Define streams
91                         InputStream inStream = null;
92
93                         // Documentation about HTTP interface at:
94                         // https://wiki.earthdata.nasa.gov/display/EL/How+To+Access+Data+With+Java
95                         int redirects = 0;
96
97                         while (redirects < 10) {
98                                 redirects++;
99
100                                 conn.setRequestProperty("User-Agent", "GpsPrune v" + GpsPrune.VERSION_NUMBER);
101                                 conn.setInstanceFollowRedirects(false);
102                                 conn.setUseCaches(false);
103                                 if (conn.getURL().getHost().equals(AUTH_URL))
104                                 {
105                                         conn.setRequestProperty("Authorization", auth);
106                                 }
107
108                                 int status = conn.getResponseCode();
109                                 if (status == 200)
110                                 {
111                                         // Found the tile, we're good
112                                         inStream = conn.getInputStream();
113                                         break;
114                                 }
115                                 else if (status == 302)
116                                 {
117                                         // redirected to SSO server then back to original resource
118                                         String newUrl = conn.getHeaderField("Location");
119                                         conn = (HttpURLConnection) (new URL(newUrl)).openConnection();
120                                 }
121                                 else if (status == 404)
122                                 {
123                                         throw new SrtmSourceException("Tile " + inTile.getTileName() + " not found at " + conn.getURL());
124                                 }
125                                 else
126                                 {
127                                         throw new SrtmSourceException("Invalid response from server: " + status + conn.getResponseMessage());
128                                 }
129                         }
130
131                         return readToFile(inStream, outputFile);
132                 }
133                 catch (IOException e)
134                 {
135                         throw new SrtmSourceException("Error while downloading tile " + inTile.getTileName() + ": "+e.getMessage());
136                 }
137         }
138
139         public boolean testAuth(String auth)
140                 throws SrtmSourceException
141         {
142                 // The only thing special about this tile is that it's the smallest tile
143                 // It covers small islands in Malaysia
144                 SrtmTile testTile = new SrtmTile(7, 117);
145                 if (isCached(testTile))
146                 {
147                         getCacheFileName(testTile).delete();
148                 }
149                 return downloadTile(testTile, auth);
150         }
151
152         public int getRowSize(SrtmTile inTile)
153         {
154                 return 3601;
155         }
156 }