Search in sources :

Example 6 with DistanceCalcEarth

use of com.graphhopper.util.DistanceCalcEarth in project graphhopper by graphhopper.

the class SpatialKeyAlgoTest method testEncode4BytesPrecision.

@Test
public void testEncode4BytesPrecision() {
    int bits = 4 * 8;
    SpatialKeyAlgo algo = new SpatialKeyAlgo(bits);
    float lat = 24.235345f;
    float lon = 47.234234f;
    long val = algo.encode(lat, lon);
    assertEquals("11001100000010010110101100111110", BitUtil.BIG.toLastBitString(val, bits));
    GHPoint fl = new GHPoint();
    algo.decode(val, fl);
    assertEquals(lat, fl.lat, 1e-2);
    assertEquals(lon, fl.lon, 1e-2);
    double expectedDist = ((float) DistanceCalcEarth.C / (1 << bits / 2));
    double d = new DistanceCalcEarth().calcDist(lat, lon, fl.lat, fl.lon);
    assertTrue("Returned point shouldn't be more far away than " + expectedDist + " -> It was " + d, d < expectedDist);
}
Also used : GHPoint(com.graphhopper.util.shapes.GHPoint) GHPoint(com.graphhopper.util.shapes.GHPoint) DistanceCalcEarth(com.graphhopper.util.DistanceCalcEarth) Test(org.junit.Test)

Example 7 with DistanceCalcEarth

use of com.graphhopper.util.DistanceCalcEarth in project graphhopper by graphhopper.

the class AbstractLocationIndexTester method testGrid.

@Test
public void testGrid() {
    Graph g = createSampleGraph(new EncodingManager("car"));
    int locs = g.getNodes();
    idx = createIndex(g, -1);
    // if we would use less array entries then some points gets the same key so avoid that for this test
    // e.g. for 16 we get "expected 6 but was 9" i.e 6 was overwritten by node j9 which is a bit closer to the grid center        
    // go through every point of the graph if all points are reachable
    NodeAccess na = g.getNodeAccess();
    for (int i = 0; i < locs; i++) {
        double lat = na.getLatitude(i);
        double lon = na.getLongitude(i);
        assertEquals("nodeId:" + i + " " + (float) lat + "," + (float) lon, i, findID(idx, lat, lon));
    }
    // hit random lat,lon and compare result to full index
    Random rand = new Random(12);
    LocationIndex fullIndex;
    if (hasEdgeSupport())
        fullIndex = new Location2IDFullWithEdgesIndex(g);
    else
        fullIndex = new Location2IDFullIndex(g);
    DistanceCalc dist = new DistanceCalcEarth();
    for (int i = 0; i < 100; i++) {
        double lat = rand.nextDouble() * 5;
        double lon = rand.nextDouble() * 5;
        int fullId = findID(fullIndex, lat, lon);
        double fullLat = na.getLatitude(fullId);
        double fullLon = na.getLongitude(fullId);
        float fullDist = (float) dist.calcDist(lat, lon, fullLat, fullLon);
        int newId = findID(idx, lat, lon);
        double newLat = na.getLatitude(newId);
        double newLon = na.getLongitude(newId);
        float newDist = (float) dist.calcDist(lat, lon, newLat, newLon);
        if (testGridIgnore(i)) {
            continue;
        }
        assertTrue(i + " orig:" + (float) lat + "," + (float) lon + " full:" + fullLat + "," + fullLon + " fullDist:" + fullDist + " found:" + newLat + "," + newLon + " foundDist:" + newDist, Math.abs(fullDist - newDist) < 50000);
    }
    fullIndex.close();
    Helper.close((Closeable) g);
}
Also used : DistanceCalc(com.graphhopper.util.DistanceCalc) Random(java.util.Random) DistanceCalcEarth(com.graphhopper.util.DistanceCalcEarth) Test(org.junit.Test)

Aggregations

DistanceCalcEarth (com.graphhopper.util.DistanceCalcEarth)7 Test (org.junit.Test)6 GHPoint (com.graphhopper.util.shapes.GHPoint)5 DistanceCalc (com.graphhopper.util.DistanceCalc)2 Random (java.util.Random)1