1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
|
package tim.prune.function.srtm;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
/**
* Superclass of each of the two available sources of SRTM data
*/
public abstract class SrtmSource
{
/** Path to disk cache, or null if none */
private final String _diskPath;
/** Possible results of the download */
public enum Result {DOWNLOADED, NOTHING_TO_DO, DOWNLOAD_FAILED,
CACHE_FAILED, NOT_ENABLED};
/** Altitude below which is considered void */
public static final int VOID_VAL = -32768;
/** @param inDiskPath disk path */
SrtmSource(String inDiskPath) {
_diskPath = inDiskPath;
}
/**
* Get the Url for the given tile
* @param inTile Tile to get the data for
* @return single URL
*/
public abstract URL getUrl(SrtmTile inTile);
/**
* Download a single tile of SRTM data
* @param inTile tile to get
*/
public abstract Result downloadTile(SrtmTile inTile) throws SrtmAuthException;
/**
* @return filename with which this tile data will be cached
*/
public abstract String getFilename(SrtmTile inTile);
/**
* @return the number of bytes in a complete hgt file (after unzipping)
*/
public long getTileSizeBytes() {return getTilePixels() * getTilePixels() * 2L;}
/**
* @return number of pixels on each side of the square
*/
public abstract int getTilePixels();
/**
* @return the directory in which all tiles are cached
*/
public File getCacheDir()
{
if (_diskPath == null) {
return null;
}
return new File(_diskPath, "srtm");
}
/**
* Get the path to write the tile to
* @param inUrl URL for online resource
* @return file object to write to, or null if already there
*/
protected File getFileToWrite(URL inUrl)
{
File srtmDir = getCacheDir();
if (srtmDir != null && srtmDir.exists() && srtmDir.isDirectory() && srtmDir.canRead())
{
File srtmFile = new File(srtmDir, new File(inUrl.getFile()).getName());
if (!srtmFile.exists() || !srtmFile.canRead() || srtmFile.length() <= 400) {
return srtmFile;
}
}
return null;
}
/**
* Write the contents of the stream to file
* @param inUrl url from which the stream came
* @param inStream stream containing data
* @return true if successful
*/
protected boolean writeFileFromStream(URL inUrl, InputStream inStream)
{
if (inStream == null) {
return false;
}
int numBytesRead;
File outputFile = getFileToWrite(inUrl);
if (outputFile == null) {
return false;
}
byte[] buffer = new byte[512];
try (FileOutputStream outStream = new FileOutputStream(outputFile))
{
while ((numBytesRead = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, numBytesRead);
}
return true;
}
catch (IOException ioe) {
System.err.println(ioe.getClass().getName() + " - " + ioe.getMessage());
}
// Close output stream; input stream will be closed by creator
return false;
}
}
|