Search in sources :

Example 1 with DataAccess

use of com.graphhopper.storage.DataAccess in project graphhopper by graphhopper.

the class SRTMProvider method getEle.

@Override
public double getEle(double lat, double lon) {
    lat = (int) (lat * precision) / precision;
    lon = (int) (lon * precision) / precision;
    int intKey = calcIntKey(lat, lon);
    HeightTile demProvider = cacheData.get(intKey);
    if (demProvider != null)
        return demProvider.getHeight(lat, lon);
    if (!cacheDir.exists())
        cacheDir.mkdirs();
    String fileDetails = getFileString(lat, lon);
    if (fileDetails == null)
        return 0;
    DataAccess heights = getDirectory().find("dem" + intKey);
    boolean loadExisting = false;
    try {
        loadExisting = heights.loadExisting();
    } catch (Exception ex) {
        logger.warn("cannot load dem" + intKey + ", error:" + ex.getMessage());
    }
    if (!loadExisting)
        updateHeightsFromZipFile(fileDetails, heights);
    int width = (int) (Math.sqrt(heights.getHeader(WIDTH_BYTE_INDEX)) + 0.5);
    if (width == 0)
        width = DEFAULT_WIDTH;
    demProvider = new HeightTile(down(lat), down(lon), width, precision, 1);
    cacheData.put(intKey, demProvider);
    demProvider.setCalcMean(calcMean);
    demProvider.setHeights(heights);
    return demProvider.getHeight(lat, lon);
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) DataAccess(com.graphhopper.storage.DataAccess)

Example 2 with DataAccess

use of com.graphhopper.storage.DataAccess in project graphhopper by graphhopper.

the class HeightTileTest method testGetHeight.

@Test
public void testGetHeight() {
    // data access has same coordinate system as graphical or UI systems have (or the original DEM data has).
    // But HeightTile has lat,lon system ('mathematically')
    int width = 10;
    HeightTile instance = new HeightTile(0, 0, width, 1e-6, 10);
    DataAccess heights = new RAMDirectory().find("tmp");
    heights.create(2 * 10 * 10);
    instance.setHeights(heights);
    init(heights, 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(5, 5), 1e-3);
    assertEquals(70, instance.getHeight(2.5, 1.5), 1e-3);
    // edge cases for one tile with the boundaries [min,min+degree/width) for lat and lon
    assertEquals(1, instance.getHeight(3, 2), 1e-3);
    assertEquals(70, instance.getHeight(2, 1), 1e-3);
    // edge cases for the whole object        
    assertEquals(1, instance.getHeight(+1.0, 2), 1e-3);
    assertEquals(90, instance.getHeight(0.5, 2.5), 1e-3);
    assertEquals(90, instance.getHeight(0.0, 2.5), 1e-3);
    assertEquals(1, instance.getHeight(+0.0, 3), 1e-3);
    assertEquals(1, instance.getHeight(-0.5, 3.5), 1e-3);
    assertEquals(1, instance.getHeight(-0.5, 3.0), 1e-3);
    // fall back to "2,9" if within its boundaries
    assertEquals(90, instance.getHeight(-0.5, 2.5), 1e-3);
    assertEquals(1, instance.getHeight(0, 0), 1e-3);
    assertEquals(1, instance.getHeight(9, 10), 1e-3);
    assertEquals(1, instance.getHeight(10, 9), 1e-3);
    assertEquals(1, instance.getHeight(10, 10), 1e-3);
    // no error
    assertEquals(1, instance.getHeight(10.5, 5), 1e-3);
    assertEquals(1, instance.getHeight(-0.5, 5), 1e-3);
    assertEquals(1, instance.getHeight(1, -0.5), 1e-3);
    assertEquals(1, instance.getHeight(1, 10.5), 1e-3);
}
Also used : RAMDirectory(com.graphhopper.storage.RAMDirectory) DataAccess(com.graphhopper.storage.DataAccess) Test(org.junit.Test)

Example 3 with DataAccess

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, 1e-6, 10);
    DataAccess heights = new RAMDirectory().find("tmp");
    heights.create(2 * 10 * 10);
    instance.setHeights(heights);
    init(heights, 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);
}
Also used : RAMDirectory(com.graphhopper.storage.RAMDirectory) DataAccess(com.graphhopper.storage.DataAccess) Test(org.junit.Test)

Example 4 with DataAccess

use of com.graphhopper.storage.DataAccess in project graphhopper by graphhopper.

the class OSMIDMapTest method testBinSearch.

@Test
public void testBinSearch() {
    DataAccess da = new RAMDirectory().find("");
    da.create(100);
    da.setInt(0 * 4, 1);
    da.setInt(1 * 4, 0);
    da.setInt(2 * 4, 5);
    da.setInt(3 * 4, 0);
    da.setInt(4 * 4, 100);
    da.setInt(5 * 4, 0);
    da.setInt(6 * 4, 300);
    da.setInt(7 * 4, 0);
    da.setInt(8 * 4, 333);
    da.setInt(9 * 4, 0);
    assertEquals(2, OSMIDMap.binarySearch(da, 0, 5, 100));
    assertEquals(3, OSMIDMap.binarySearch(da, 0, 5, 300));
    assertEquals(~3, OSMIDMap.binarySearch(da, 0, 5, 200));
    assertEquals(0, OSMIDMap.binarySearch(da, 0, 5, 1));
    assertEquals(1, OSMIDMap.binarySearch(da, 0, 5, 5));
}
Also used : RAMDirectory(com.graphhopper.storage.RAMDirectory) DataAccess(com.graphhopper.storage.DataAccess) Test(org.junit.Test)

Example 5 with DataAccess

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);
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Raster(java.awt.image.Raster) ZipEntry(java.util.zip.ZipEntry) SeekableStream(org.apache.xmlgraphics.image.codec.util.SeekableStream) IOException(java.io.IOException) IOException(java.io.IOException) SocketTimeoutException(java.net.SocketTimeoutException) FileInputStream(java.io.FileInputStream) DataAccess(com.graphhopper.storage.DataAccess) ZipInputStream(java.util.zip.ZipInputStream) SocketTimeoutException(java.net.SocketTimeoutException) TIFFImageDecoder(org.apache.xmlgraphics.image.codec.tiff.TIFFImageDecoder) File(java.io.File) TIFFDecodeParam(org.apache.xmlgraphics.image.codec.tiff.TIFFDecodeParam)

Aggregations

DataAccess (com.graphhopper.storage.DataAccess)6 RAMDirectory (com.graphhopper.storage.RAMDirectory)4 Test (org.junit.Test)4 SocketTimeoutException (java.net.SocketTimeoutException)2 Raster (java.awt.image.Raster)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ZipEntry (java.util.zip.ZipEntry)1 ZipInputStream (java.util.zip.ZipInputStream)1 TIFFDecodeParam (org.apache.xmlgraphics.image.codec.tiff.TIFFDecodeParam)1 TIFFImageDecoder (org.apache.xmlgraphics.image.codec.tiff.TIFFImageDecoder)1 SeekableStream (org.apache.xmlgraphics.image.codec.util.SeekableStream)1