]> gitweb.fperrin.net Git - GpsPrune.git/blob - src/tim/prune/function/srtm/Srtm3Source.java
Refactor and add comments
[GpsPrune.git] / src / tim / prune / function / srtm / Srtm3Source.java
1 package tim.prune.function.srtm;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.net.MalformedURLException;
7 import java.net.URL;
8
9 import tim.prune.I18nManager;
10
11 public class Srtm3Source extends SrtmSource {
12         /** URL prefix for all tiles */
13         private static final String URL_PREFIX = "https://dds.cr.usgs.gov/srtm/version2_1/SRTM3/";
14         /** Directory names for each continent */
15         private static final String[] CONTINENTS = {"", "Eurasia", "North_America", "Australia",
16                                                     "Islands", "South_America", "Africa"};
17         private byte[] _continents_lookup;
18
19
20         public Srtm3Source()
21         {
22                 _continents_lookup = populateContinents();
23         }
24
25         public String getNameKey()
26         {
27                 return "function.downloadsrtm." + getName();
28         }
29
30         public String getName()
31         {
32                 return "SRTM3_v21";
33         }
34
35         protected String getSourceExtension()
36         {
37                 return ".hgt.zip";
38         }
39
40         /**
41          * Read the dat file and get the contents
42          * @return byte array containing file contents
43          */
44         private static byte[] populateContinents()
45         {
46                 InputStream in = null;
47                 try
48                 {
49                         // Need absolute path to dat file
50                         in = Srtm3Source.class.getResourceAsStream("/tim/prune/function/srtm/srtmtiles.dat");
51                         if (in != null)
52                         {
53                                 byte[] buffer = new byte[in.available()];
54                                 in.read(buffer);
55                                 in.close();
56                                 return buffer;
57                         }
58                 }
59                 catch (IOException e) {
60                         System.err.println("Exception trying to read srtmtiles.dat : " + e.getMessage());
61                 }
62                 finally
63                 {
64                         try {
65                                 in.close();
66                         }
67                         catch (Exception e) {} // ignore
68                 }
69                 return null;
70         }
71
72         /**
73          * Get the Url for the given tile
74          * @param inTile Tile to get
75          * @return URL
76          */
77         private URL buildUrl(SrtmTile inTile)
78                 throws SrtmSourceException
79         {
80                 
81                 // Get byte from lookup array
82                 int idx = (inTile.getLatitude() + 59)*360 + (inTile.getLongitude() + 180);
83                 int dir;
84                 try
85                 {
86                         dir = _continents_lookup[idx];
87                 }
88                 catch (ArrayIndexOutOfBoundsException e)
89                 {
90                         throw new SrtmSourceException("Could not find continent for tile "+inTile.getTileName());
91                 }
92                 try
93                 {
94                         return new URL(URL_PREFIX + CONTINENTS[dir] + "/" + inTile.getTileName() + getSourceExtension());
95                 }
96                 catch (MalformedURLException e)
97                 {
98                         throw new SrtmSourceException("Could not build URL for tile "+inTile.getTileName());
99                 }
100         }
101
102         public boolean isReadyToUse()
103         {
104                 return true;
105         }
106
107         public boolean downloadTile(SrtmTile inTile)
108                 throws SrtmSourceException
109         {
110                 URL tileUrl = buildUrl(inTile);
111                 InputStream inStream = getStreamToUrl(tileUrl);
112                 return readToFile(inStream, getCacheFileName(inTile));
113         }
114
115         public int getRowSize(SrtmTile inTile)
116         {
117                 return 1201;
118         }
119 }