]> gitweb.fperrin.net Git - GpsPrune.git/blob - src/tim/prune/function/srtm/SrtmViewfinderSource.java
a25e9c4e95b55b68341a520df62ad19f54aac00e
[GpsPrune.git] / src / tim / prune / function / srtm / SrtmViewfinderSource.java
1 package tim.prune.function.srtm;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.FileReader;
6 import java.io.FileOutputStream;
7 import java.io.InputStream;
8 import java.io.IOException;
9 import java.io.BufferedReader;
10 import java.io.InputStreamReader;
11 import java.net.MalformedURLException;
12 import java.net.URL;
13 import java.net.HttpURLConnection;
14 import java.util.HashMap;
15 import java.util.zip.ZipEntry;
16 import java.util.zip.ZipInputStream;
17
18 import tim.prune.GpsPrune;
19 import tim.prune.I18nManager;
20
21 public class SrtmViewfinderSource extends SrtmSource {
22         /** URL prefix for all tiles */
23         private static final String URL_PREFIX = "http://viewfinderpanoramas.org/";
24         private HashMap<SrtmTile, String> _tile_lookup = null;
25         private HashMap<SrtmTile, Integer> _tile_sizes = null;
26
27         public SrtmViewfinderSource()
28         {
29                 _tile_lookup = new HashMap<SrtmTile, String>();
30                 _tile_sizes = new HashMap<SrtmTile, Integer>();
31         }
32
33         public String getNameKey()
34         {
35                 return "function.downloadsrtm." + getName();
36         }
37
38         public String getName()
39         {
40                 return "SRTM_Viewfinder";
41         }
42
43         protected String getSourceExtension()
44         {
45                 return ".zip";
46         }
47
48         /**
49          * Read the dat file and get the contents
50          * @return byte array containing file contents
51          */
52         private void populateLookup()
53                 throws SrtmSourceException
54         {
55                 BufferedReader in = null;
56                 try
57                 {
58                         // in = SrtmViewfinderSource.class.getResourceAsStream("viewfinder/tiles.db");
59                         in = new BufferedReader(new InputStreamReader(SrtmViewfinderSource.class.getResourceAsStream("/tim/prune/function/srtm/viewfinder/tiles.dat")));
60                         String line;
61                         while ((line = in.readLine()) != null)
62                         {
63                                 String[] parts = line.split(" ");
64                                 SrtmTile tile = new SrtmTile(Integer.parseInt(parts[0]), Integer.parseInt(parts[1]));
65                                 _tile_lookup.put(tile, parts[2]);
66                         }
67                 }
68                 catch (Exception e) {
69                         throw new SrtmSourceException("Exception trying to read tiles.db: " + e);
70                 }
71                 finally
72                 {
73                         try {
74                                 in.close();
75                         }
76                         catch (Exception e) {} // ignore
77                 }
78         }
79
80         /**
81          * Get the filename for the given tile
82          */
83         private String tileFileName(SrtmTile inTile)
84                 throws SrtmSourceException
85         {
86                 if (_tile_lookup.size() == 0)
87                 {
88                         populateLookup();
89                 }
90                 String path = _tile_lookup.get(inTile);
91                 if (path == null)
92                 {
93                         // int key = inTile.hashCode();
94                         // System.out.println("inTile hashcode " + key);
95                         // for (HashMap.Entry<SrtmTile, String> e : _tile_lookup.entrySet())
96                         // {
97                         //      System.out.println(e.getKey().getTileName() + " - " + e.getKey().hashCode() + " -> " + e.getValue());
98                         // }
99                         throw new SrtmSourceException("tile not in database "+inTile.getTileName());
100                 }
101                 return path + getSourceExtension();
102         }
103
104         /**
105          * Get the UTL for the given tile
106          * @param inTile Tile to get
107          * @return URL
108          */
109         private URL buildUrl(SrtmTile inTile)
110                 throws SrtmSourceException
111         {
112                 String filename = tileFileName(inTile);
113                 try
114                 {
115                         return new URL(URL_PREFIX + filename);
116                 }
117                 catch (MalformedURLException e)
118                 {
119                         e.printStackTrace();
120                         throw new SrtmSourceException(e.getMessage());
121                 }
122         }
123
124         public boolean isReadyToUse()
125         {
126                 return true;
127         }
128
129         public boolean downloadTile(SrtmTile inTile)
130                 throws SrtmSourceException
131         {
132                 URL tileUrl = buildUrl(inTile);
133                 System.out.println("Download: Need to download: " + tileUrl);
134                 try
135                 {
136                         HttpURLConnection conn = (HttpURLConnection) tileUrl.openConnection();
137
138                         // Define streams
139                         InputStream inStream = null;
140
141                         conn.setRequestProperty("User-Agent", "GpsPrune v" + GpsPrune.VERSION_NUMBER);
142
143                         int status = conn.getResponseCode();
144                         if (status == 200)
145                         {
146                                 inStream = conn.getInputStream();
147                         }
148                         else if (status == 404)
149                         {
150                                 throw new SrtmSourceException("Tile not found: "+conn.getURL());
151                         }
152                         else
153                         {
154                                 throw new SrtmSourceException("Invalid response from server: " +status+conn.getContent());
155                         }
156
157                         return downloadToFile(inStream, getCacheFileName(inTile));
158                 }
159                 catch (IOException e)
160                 {
161                         throw new SrtmSourceException("Error while downloading tile "+inTile.getTileName()+": "+e.getMessage());
162                 }
163         }
164
165         private ZipEntry advanceToEntry(ZipInputStream inStream, SrtmTile inTile)
166                 throws SrtmSourceException
167         {
168                 while (true)
169                 {
170                         ZipEntry entry;
171                         try
172                         {
173                                 entry = inStream.getNextEntry();
174                         }
175                         catch (IOException e)
176                         {
177                                 throw new SrtmSourceException("Tile file " + getCacheFileName(inTile) + " found in cache, but tile " + inTile.getTileName() + "not found inside ZIP archive");
178                         }
179                         String entryName = entry.getName().toUpperCase();
180                         if (entryName.contains(inTile.getTileName()))
181                         {
182                                 // tile size is one of 1201; 3601 depending on
183                                 // resolution
184                                 if (entry.getSize() == 2 * 1201 * 1201)
185                                 {
186                                         _tile_sizes.put(inTile, 1201);
187                                 }
188                                 else if (entry.getSize() == 2 * 3601 * 3601)
189                                 {
190                                         _tile_sizes.put(inTile, 3601);
191                                 }
192                                 else
193                                 {
194                                         throw new SrtmSourceException("Tile file "+getCacheFileName(inTile)+" does not have the expected size, it is: " + entry.getSize());
195                                 }
196                                 return entry;
197                         }
198                 }
199         }
200
201         public int[] getTileHeights(SrtmTile inTile)
202                 throws SrtmSourceException
203         {
204                 File cacheFileName = getCacheFileName(inTile);
205                 if (cacheFileName == null)
206                 {
207                         throw new SrtmSourceException("Tile "+inTile.getTileName()+" not in cache");
208                 }
209                 try
210                 {
211                         ZipInputStream inStream = new ZipInputStream(new FileInputStream(cacheFileName));
212                         ZipEntry entry = advanceToEntry(inStream, inTile);
213                         int rowSize = getRowSize(inTile);
214                         return slurpTileHeigths(inStream, rowSize * rowSize);
215                 }
216                 catch (IOException e)
217                 {
218                         throw new SrtmSourceException("Failure opening "+cacheFileName+" for reading:"+e.getMessage());
219                 }
220
221         }
222
223         public int getRowSize(SrtmTile inTile)
224         {
225                 return _tile_sizes.get(inTile);
226         }
227
228         protected File getCacheFileName(SrtmTile inTile)
229         {
230                 try {
231                         File fileName = new File(tileFileName(inTile));
232                         return new File(getCacheDir(), fileName.getName());
233                 }
234                 catch (SrtmSourceException e)
235                 {
236                         return null;
237                 }
238         }
239 }