]> gitweb.fperrin.net Git - GpsPrune.git/blob - src/tim/prune/function/srtm/SrtmSource.java
Download tiles in chunks
[GpsPrune.git] / src / tim / prune / function / srtm / SrtmSource.java
1 package tim.prune.function.srtm;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.FileOutputStream;
6 import java.io.InputStream;
7 import java.io.IOException;
8 import java.util.zip.ZipEntry;
9 import java.util.zip.ZipInputStream;
10
11 public abstract class SrtmSource {
12         // methods implemented by each source
13         public abstract String getName();
14         public abstract boolean isReadyToUse();
15         public abstract boolean downloadTile(SrtmTile inTile)
16                 throws SrtmSourceException;
17         public abstract int getRowSize(SrtmTile inTile);
18         protected abstract String getSourceExtension();
19
20         protected boolean downloadToFile(InputStream inStream, File outputFile)
21                 throws IOException
22         {
23                 FileOutputStream outStream = new FileOutputStream(outputFile);
24
25                 byte[] buffer = new byte[4096];
26                 int read = 0;
27                 while ((read = inStream.read(buffer)) != -1)
28                 {
29                         outStream.write(buffer, 0, read);
30                 }
31                 // Make sure streams are closed
32                 try {inStream.close();} catch (Exception e) {}
33                 try {outStream.close();} catch (Exception e) {}
34                 return true;
35
36         }
37
38         protected int[] slurpTileHeigths(ZipInputStream inStream, int tileSize)
39                 throws IOException
40         {
41                 int[] heights = new int[tileSize];
42                 int dataSize = 2 * tileSize;
43                 byte[] buffer = new byte[dataSize];
44                 // Read entire file contents into one byte array
45                 int alreadyRead = 0;
46                 while (alreadyRead < dataSize)
47                 {
48                         alreadyRead += inStream.read(buffer, alreadyRead, dataSize - alreadyRead);
49                 }
50                 for (int i = 0; i < tileSize; i++)
51                 {
52                         // Bytes are signed. Cast high-order to int with sign
53                         // extension, and clamp low-order to its unsigned range
54                         heights[i] = buffer[2 * i] * 256 + (0xff & buffer[2 * i + 1]);
55                 }
56                 // Close stream
57                 inStream.close();
58                 return heights;
59         }
60
61         public int[] getTileHeights(SrtmTile inTile)
62                 throws SrtmSourceException
63         {
64                 File cacheFileName = getCacheFileName(inTile);
65                 if (cacheFileName == null)
66                 {
67                         throw new SrtmSourceException("Tile "+inTile.getTileName()+" not in cache");
68                 }
69                 try
70                 {
71                         ZipInputStream inStream = new ZipInputStream(new FileInputStream(cacheFileName));
72                         ZipEntry entry = inStream.getNextEntry();
73                         int rowSize = getRowSize(inTile);
74                         int tileSize = rowSize * rowSize;
75                         if (entry.getSize() != 2 * tileSize)
76                         {
77                                 throw new SrtmSourceException("Tile file "+cacheFileName+" does not have the expected size");
78                         }
79                         return slurpTileHeigths(inStream, tileSize);
80                 }
81                 catch (IOException e)
82                 {
83                         throw new SrtmSourceException("Failure opening "+cacheFileName+" for reading:"+e.getMessage());
84                 }
85
86         }
87
88         protected File getCacheDir()
89         {
90                 return SrtmDiskCache.getCacheDir(getName());
91         }
92
93         protected File getCacheFileName(SrtmTile inTile)
94         {
95                 String fileName = inTile.getTileName() + getSourceExtension();
96                 return new File(getCacheDir(), fileName);
97         }
98
99         public boolean isCached(SrtmTile inTile)
100         {
101                 File cachedFileName = getCacheFileName(inTile);
102                 return cachedFileName != null &&
103                         getCacheFileName(inTile).exists();
104         }
105 }