use of org.apache.xmlgraphics.image.codec.tiff.TIFFImageDecoder in project graphhopper by graphhopper.
the class CGIARProvider method getEle.
@Override
public double getEle(double lat, double lon) {
// no data we can avoid the trouble
if (lat > 60 || lat < -60)
return 0;
lat = (int) (lat * precision) / precision;
lon = (int) (lon * precision) / precision;
String name = getFileName(lat, lon);
HeightTile demProvider = cacheData.get(name);
if (demProvider == null) {
if (!cacheDir.exists())
cacheDir.mkdirs();
int minLat = down(lat);
int minLon = down(lon);
// less restrictive against boundary checking
demProvider = new HeightTile(minLat, minLon, WIDTH, degree * precision, degree);
demProvider.setCalcMean(calcMean);
cacheData.put(name, demProvider);
DataAccess heights = getDirectory().find(name + ".gh");
demProvider.setHeights(heights);
boolean loadExisting = false;
try {
loadExisting = heights.loadExisting();
} catch (Exception ex) {
logger.warn("cannot load " + name + ", error: " + ex.getMessage());
}
if (!loadExisting) {
String tifName = name + ".tif";
String zippedURL = baseUrl + "/" + name + ".zip";
File file = new File(cacheDir, new File(zippedURL).getName());
// get zip file if not already in cacheDir - unzip later and in-memory only!
if (!file.exists()) {
try {
int max = 3;
for (int trial = 0; trial < max; trial++) {
try {
downloader.downloadFile(zippedURL, file.getAbsolutePath());
break;
} catch (SocketTimeoutException ex) {
// just try again after a little nap
Thread.sleep(sleep);
if (trial >= max - 1)
throw ex;
continue;
} catch (IOException ex) {
demProvider.setSeaLevel(true);
// use small size on disc and in-memory
heights.setSegmentSize(100).create(10).flush();
return 0;
}
}
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
// short == 2 bytes
heights.create(2 * WIDTH * WIDTH);
// logger.info("start decoding");
// decode tiff data
Raster raster;
SeekableStream ss = null;
try {
InputStream is = new FileInputStream(file);
ZipInputStream zis = new ZipInputStream(is);
// find tif file in zip
ZipEntry entry = zis.getNextEntry();
while (entry != null && !entry.getName().equals(tifName)) {
entry = zis.getNextEntry();
}
ss = SeekableStream.wrapInputStream(zis, true);
TIFFImageDecoder imageDecoder = new TIFFImageDecoder(ss, new TIFFDecodeParam());
raster = imageDecoder.decodeAsRaster();
} catch (Exception e) {
throw new RuntimeException("Can't decode " + tifName, e);
} finally {
if (ss != null)
Helper.close(ss);
}
// logger.info("start converting to our format");
final int height = raster.getHeight();
final int width = raster.getWidth();
int x = 0, y = 0;
try {
for (y = 0; y < height; y++) {
for (x = 0; x < width; x++) {
short val = (short) raster.getPixel(x, y, (int[]) null)[0];
if (val < -1000 || val > 12000)
val = Short.MIN_VALUE;
heights.setShort(2 * (y * WIDTH + x), val);
}
}
heights.flush();
// TODO remove tifName and zip?
} catch (Exception ex) {
throw new RuntimeException("Problem at x:" + x + ", y:" + y, ex);
}
}
// loadExisting
}
if (demProvider.isSeaLevel())
return 0;
return demProvider.getHeight(lat, lon);
}
Aggregations