]> gitweb.fperrin.net Git - GpsPrune.git/blob - src/tim/prune/function/srtm/SrtmViewfinderSource.java
wip viewfinder
[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.FileReader;
5 import java.io.FileOutputStream;
6 import java.io.InputStream;
7 import java.io.IOException;
8 import java.io.BufferedReader;
9 import java.io.InputStreamReader;
10 import java.net.MalformedURLException;
11 import java.net.URL;
12 import java.net.HttpURLConnection;
13 import java.util.HashMap;
14
15 import tim.prune.GpsPrune;
16 import tim.prune.I18nManager;
17
18 public class SrtmViewfinderSource extends SrtmSource {
19         /** URL prefix for all tiles */
20         private static final String URL_PREFIX = "http://viewfinderpanoramas.org/";
21         private HashMap<SrtmTile, String> _tile_lookup = null;
22
23         public SrtmViewfinderSource()
24         {
25         }
26
27         public String getNameKey()
28         {
29                 return "function.downloadsrtm." + getName();
30         }
31
32         public String getName()
33         {
34                 return "SRTM_Viewfinder";
35         }
36
37         protected String getSourceExtension()
38         {
39                 return ".zip";
40         }
41
42         /**
43          * Read the dat file and get the contents
44          * @return byte array containing file contents
45          */
46         private void populateLookup()
47                 throws SrtmSourceException
48         {
49                 BufferedReader in = null;
50                 try
51                 {
52                         // in = SrtmViewfinderSource.class.getResourceAsStream("viewfinder/tiles.db");
53                         _tile_lookup = new HashMap<SrtmTile, String>();
54                         in = new BufferedReader(new InputStreamReader(SrtmViewfinderSource.class.getResourceAsStream("/tim/prune/function/srtm/viewfinder/tiles.dat")));
55                         String line;
56                         while ((line = in.readLine()) != null)
57                         {
58                                 String[] parts = line.split(" ");
59                                 SrtmTile tile = new SrtmTile(Integer.parseInt(parts[0]), Integer.parseInt(parts[1]));
60                                 _tile_lookup.put(tile, parts[2]);
61                         }
62                 }
63                 catch (Exception e) {
64                         throw new SrtmSourceException("Exception trying to read tiles.db: " + e);
65                 }
66                 finally
67                 {
68                         try {
69                                 in.close();
70                         }
71                         catch (Exception e) {} // ignore
72                 }
73         }
74
75         /**
76          * Get the Url for the given tile
77          * @param inTile Tile to get
78          * @return URL
79          */
80         private URL buildUrl(SrtmTile inTile)
81                 throws SrtmSourceException
82         {
83                 if (_tile_lookup == null)
84                 {
85                         populateLookup();
86                 }
87                 System.out.println(_tile_lookup.get(inTile));
88                 String path = _tile_lookup.get(inTile);
89                 if (path == null)
90                 {
91                         int key = inTile.hashCode();
92                         System.out.println("inTile hashcode " + key);
93                         for (HashMap.Entry<SrtmTile, String> e : _tile_lookup.entrySet())
94                         {
95                                 if (e.getKey().getLatitude() == 51)
96                                 {
97                                         System.out.println(e.getKey().getTileName() + " - " + e.getKey().hashCode() + " -> " + e.getValue());
98                                 }
99                         }
100                         throw new SrtmSourceException("tile not in database "+inTile.getTileName());
101                 }
102                 try
103                 {
104                         return new URL(URL_PREFIX + path + getSourceExtension());
105                 }
106                 catch (MalformedURLException e)
107                 {
108                         e.printStackTrace();
109                         throw new SrtmSourceException(e.getMessage());
110                 }
111         }
112
113         public boolean isReadyToUse()
114         {
115                 return true;
116         }
117
118         public boolean downloadTile(SrtmTile inTile)
119                 throws SrtmSourceException
120         {
121                 int redirects = 5;
122                 URL tileUrl = buildUrl(inTile);
123                 File outputFile = getCacheFileName(inTile);
124                 System.out.println("Download: Need to download: " + tileUrl);
125
126                 try
127                 {
128                         HttpURLConnection conn = (HttpURLConnection) tileUrl.openConnection();
129
130                         // Define streams
131                         FileOutputStream outStream = null;
132                         InputStream inStream = null;
133
134                         conn.setRequestProperty("User-Agent", "GpsPrune v" + GpsPrune.VERSION_NUMBER);
135
136                         int status = conn.getResponseCode();
137                         if (status == 200)
138                         {
139                                 inStream = conn.getInputStream();
140                         }
141                         else if (status == 404)
142                         {
143                                 throw new SrtmSourceException("Tile not found: "+conn.getURL());
144                         }
145                         else
146                         {
147                                 throw new SrtmSourceException("Invalid response from server: " +status+conn.getContent());
148                         }
149
150                         outStream = new FileOutputStream(outputFile);
151
152                         int c;
153                         while ((c = inStream.read()) != -1)
154                         {
155                                 outStream.write(c);
156                         }
157                         // Make sure streams are closed
158                         try {inStream.close();} catch (Exception e) {}
159                         try {outStream.close();} catch (Exception e) {}
160                         return true;
161                 }
162                 catch (IOException e)
163                 {
164                         throw new SrtmSourceException("Error while downloading tile "+inTile.getTileName()+": "+e.getMessage());
165                 }
166         }
167
168         public int getRowSize(SrtmTile inTile)
169         {
170                 return 1201;
171         }
172 }