1 package tim.prune.function.srtm;
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;
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();
18 public int[] getTileHeights(SrtmTile inTile)
19 throws SrtmSourceException
21 File cacheFileName = getCacheFileName(inTile);
22 if (cacheFileName == null)
24 throw new SrtmSourceException("Tile "+inTile.getTileName()+" not in cache");
28 ZipInputStream inStream = new ZipInputStream(new FileInputStream(cacheFileName));
29 ZipEntry entry = inStream.getNextEntry();
30 int rowSize = getRowSize(inTile);
31 int tileSize = rowSize * rowSize;
32 if (entry.getSize() != 2 * tileSize)
34 throw new SrtmSourceException("Tile file "+cacheFileName+" does not have the expected size");
36 int[] heights = new int[tileSize];
37 // Read entire file contents into one byte array
38 for (int i = 0; i < heights.length; i++)
40 heights[i] = inStream.read() * 256 + inStream.read();
41 if (heights[i] >= 32768) {heights[i] -= 65536;}
49 throw new SrtmSourceException("Failure opening "+cacheFileName+" for reading:"+e.getMessage());
54 protected File getCacheDir()
56 return SrtmDiskCache.getCacheDir(getName());
59 protected File getCacheFileName(SrtmTile inTile)
61 String fileName = inTile.getTileName() + getSourceExtension();
62 return new File(getCacheDir(), fileName);
65 public boolean isCached(SrtmTile inTile)
67 return getCacheFileName(inTile).exists();