use of com.graphhopper.storage.DataAccess in project graphhopper by graphhopper.
the class HeightTileTest method testGetHeightForNegativeTile.
@Test
public void testGetHeightForNegativeTile() {
int width = 10;
HeightTile instance = new HeightTile(-20, -20, width, width, 1e-6, 10, 10);
DataAccess heights = new RAMDirectory().find("tmp");
heights.create(2 * 10 * 10);
instance.setHeights(heights);
init(heights, width, width, 1);
// x,y=1,7
heights.setShort(2 * (7 * width + 1), (short) 70);
// x,y=2,9
heights.setShort(2 * (9 * width + 2), (short) 90);
assertEquals(1, instance.getHeight(-15, -15), 1e-3);
assertEquals(70, instance.getHeight(-17.5, -18.5), 1e-3);
// edge cases for one tile with the boundaries [min,min+degree/width) for lat and lon
assertEquals(1, instance.getHeight(-17, -18), 1e-3);
assertEquals(70, instance.getHeight(-18, -19), 1e-3);
}
use of com.graphhopper.storage.DataAccess in project graphhopper by graphhopper.
the class HeightTileTest method testCalcMean.
@Test
public void testCalcMean() {
int width = 10;
HeightTile instance = new HeightTile(0, 0, width, width, 1e-6, 10, 10).setCalcMean(true);
DataAccess heights = new RAMDirectory().find("tmp");
heights.create(2 * 10 * 10);
instance.setHeights(heights);
init(heights, width, width, 1);
// x,y=0,9
heights.setShort(2 * (9 * width + 0), (short) 10);
// x,y=1,7
heights.setShort(2 * (7 * width + 1), (short) 70);
// x,y=2,8
heights.setShort(2 * (8 * width + 2), (short) 90);
assertEquals((70 + 4) / 5d, instance.getHeight(2, 1), 1e-3);
assertEquals((70 + 90 + 3) / 5d, instance.getHeight(2.5, 2.5), 1e-3);
assertEquals((90 + 3) / 4d, instance.getHeight(-0.5, 2.5), 1e-3);
assertEquals((10 + 2) / 3d, instance.getHeight(-0.5, -0.5), 1e-3);
}
use of com.graphhopper.storage.DataAccess 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