]> gitweb.fperrin.net Git - GpsPrune.git/blob - src/tim/prune/function/srtm/SrtmSource.java
0791cd833c28fc7a2acd14cb4901a1a675d3cae2
[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.IOException;
7 import java.util.zip.ZipEntry;
8 import java.util.zip.ZipInputStream;
9
10 public abstract class SrtmSource {
11         public abstract String getName();
12         public abstract boolean isReadyToUse();
13         public abstract boolean downloadTile(SrtmTile inTile)
14                 throws SrtmSourceException;
15         public abstract int getRowSize(SrtmTile inTile);
16         protected abstract String getSourceExtension();
17
18         protected int[] slurpTileHeigths(ZipInputStream inStream, int tileSize)
19                 throws IOException
20         {
21                 int[] heights = new int[tileSize];
22                 // Read entire file contents into one byte array
23                 for (int i = 0; i < heights.length; i++)
24                 {
25                         heights[i] = inStream.read() * 256 + inStream.read();
26                         if (heights[i] >= 32768) {heights[i] -= 65536;}
27                 }
28                 // Close stream
29                 inStream.close();
30                 return heights;
31         }
32
33         public int[] getTileHeights(SrtmTile inTile)
34                 throws SrtmSourceException
35         {
36                 File cacheFileName = getCacheFileName(inTile);
37                 if (cacheFileName == null)
38                 {
39                         throw new SrtmSourceException("Tile "+inTile.getTileName()+" not in cache");
40                 }
41                 try
42                 {
43                         ZipInputStream inStream = new ZipInputStream(new FileInputStream(cacheFileName));
44                         ZipEntry entry = inStream.getNextEntry();
45                         int rowSize = getRowSize(inTile);
46                         int tileSize = rowSize * rowSize;
47                         if (entry.getSize() != 2 * tileSize)
48                         {
49                                 throw new SrtmSourceException("Tile file "+cacheFileName+" does not have the expected size");
50                         }
51                         return slurpTileHeigths(inStream, tileSize);
52                 }
53                 catch (IOException e)
54                 {
55                         throw new SrtmSourceException("Failure opening "+cacheFileName+" for reading:"+e.getMessage());
56                 }
57
58         }
59
60         protected File getCacheDir()
61         {
62                 return SrtmDiskCache.getCacheDir(getName());
63         }
64
65         protected File getCacheFileName(SrtmTile inTile)
66         {
67                 String fileName = inTile.getTileName() + getSourceExtension();
68                 return new File(getCacheDir(), fileName);
69         }
70
71         public boolean isCached(SrtmTile inTile)
72         {
73                 File cachedFileName = getCacheFileName(inTile);
74                 return cachedFileName != null &&
75                         getCacheFileName(inTile).exists();
76         }
77 }