use of com.graphhopper.util.shapes.GHPoint in project graphhopper by graphhopper.
the class SpatialKeyAlgoTest method testBijection.
public void testBijection(int bits) {
SpatialKeyAlgo algo = new SpatialKeyAlgo(bits);
GHPoint coord11 = new GHPoint();
long key = algo.encode(1, 1);
algo.decode(key, coord11);
long resKey = algo.encode(coord11.lat, coord11.lon);
GHPoint coord2 = new GHPoint();
algo.decode(resKey, coord2);
assertEquals(key, resKey);
GHPoint coord = new GHPoint(50.022846, 9.2123575);
key = algo.encode(coord);
algo.decode(key, coord2);
assertEquals(key, algo.encode(coord2));
double dist = new DistanceCalcEarth().calcDist(coord.lat, coord.lon, coord2.lat, coord2.lon);
// and ensure small distance
assertTrue(dist + "", dist < 5);
long queriedKey = 246557819640268L;
long storedKey = 246557819640269L;
algo.decode(queriedKey, coord);
algo.decode(storedKey, coord2);
// 2. fix bijection precision problem
assertEquals(storedKey, algo.encode(coord2));
dist = new DistanceCalcEarth().calcDist(coord.lat, coord.lon, coord2.lat, coord2.lon);
// and ensure small distance
assertTrue(dist + "", dist < 5);
coord = new GHPoint(50.0606072, 9.6277542);
key = algo.encode(coord);
algo.decode(key, coord2);
assertEquals(key, algo.encode(coord2));
dist = new DistanceCalcEarth().calcDist(coord.lat, coord.lon, coord2.lat, coord2.lon);
// and ensure small distance
assertTrue(dist + "", dist < 5);
coord = new GHPoint(0.01, 0.01);
key = algo.encode(coord);
algo.decode(key, coord2);
assertEquals(key, algo.encode(coord2));
dist = new DistanceCalcEarth().calcDist(coord.lat, coord.lon, coord2.lat, coord2.lon);
// and ensure small distance
assertTrue(dist + "", dist < 5);
}
use of com.graphhopper.util.shapes.GHPoint in project graphhopper by graphhopper.
the class SpatialKeyAlgoTest method testEncode3BytesPrecision.
@Test
public void testEncode3BytesPrecision() {
// 3 bytes => c / 1^12 = ~10km
int bits = 3 * 8;
SpatialKeyAlgo algo = new SpatialKeyAlgo(bits);
float lat = 24.235345f;
float lon = 47.234234f;
long val = algo.encode(lat, lon);
assertEquals("00000000" + "110011000000100101101011", BitUtil.BIG.toLastBitString(val, 32));
GHPoint fl = new GHPoint();
algo.decode(val, fl);
// normally 10km are expected here we have only 100meters ... (?)
assertEquals(lat, fl.lat, .1);
assertEquals(lon, fl.lon, .1);
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);
}
use of com.graphhopper.util.shapes.GHPoint in project graphhopper by graphhopper.
the class SpatialKeyAlgoTest method testNoFurtherIterationIfBitsIs1.
@Test
public void testNoFurtherIterationIfBitsIs1() {
SpatialKeyAlgo algo = new SpatialKeyAlgo(4).setBounds(0, 5, 0, 5);
// 1001
GHPoint coord = new GHPoint();
algo.decode(9, coord);
assertEquals(3.125, coord.lat, 1e-4);
assertEquals(1.875, coord.lon, 1e-4);
}
use of com.graphhopper.util.shapes.GHPoint in project graphhopper by graphhopper.
the class SpatialKeyAlgoTest method testEncode6BytesPrecision.
@Test
public void testEncode6BytesPrecision() {
int bits = 6 * 8;
SpatialKeyAlgo algo = new SpatialKeyAlgo(bits);
float lat = 24.235345f;
float lon = 47.234234f;
long val = algo.encode(lat, lon);
assertEquals("11001100000010010110101100111110" + "11100111" + "01000110", BitUtil.BIG.toLastBitString(val, bits));
GHPoint fl = new GHPoint();
algo.decode(val, fl);
assertEquals(lat, fl.lat, 1e-4);
assertEquals(lon, fl.lon, 1e-4);
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);
}
use of com.graphhopper.util.shapes.GHPoint 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);
}
Aggregations