]> gitweb.fperrin.net Git - GpsPrune.git/blob - src/tim/prune/function/srtm/SrtmSource.java
Use SRTM 1deg data from NASA servers
[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         public int[] getTileHeights(SrtmTile inTile)
19                 throws SrtmSourceException
20         {
21                 File cacheFileName = getCacheFileName(inTile);
22                 if (cacheFileName == null)
23                 {
24                         throw new SrtmSourceException("Tile "+inTile.getTileName()+" not in cache");
25                 }
26                 try
27                 {
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)
33                         {
34                                 throw new SrtmSourceException("Tile file "+cacheFileName+" does not have the expected size");
35                         }
36                         int[] heights = new int[tileSize];
37                         // Read entire file contents into one byte array
38                         for (int i = 0; i < heights.length; i++)
39                         {
40                                 heights[i] = inStream.read() * 256 + inStream.read();
41                                 if (heights[i] >= 32768) {heights[i] -= 65536;}
42                         }
43                         // Close stream
44                         inStream.close();
45                         return heights;
46                 }
47                 catch (IOException e)
48                 {
49                         throw new SrtmSourceException("Failure opening "+cacheFileName+" for reading:"+e.getMessage());
50                 }
51
52         }
53
54         protected File getCacheDir()
55         {
56                 return SrtmDiskCache.getCacheDir(getName());
57         }
58
59         protected File getCacheFileName(SrtmTile inTile)
60         {
61                 String fileName = inTile.getTileName() + getSourceExtension();
62                 return new File(getCacheDir(), fileName);
63         }
64
65         public boolean isCached(SrtmTile inTile)
66         {
67                 return getCacheFileName(inTile).exists();
68         }
69 }