use of com.graphhopper.geohash.SpatialKeyAlgo in project graphhopper by graphhopper.
the class BresenhamLineTest method testRealBresenham.
@Test
public void testRealBresenham() {
int parts = 4;
int bits = (int) (Math.log(parts * parts) / Math.log(2));
double minLon = -1, maxLon = 1.6;
double minLat = -1, maxLat = 0.5;
final KeyAlgo keyAlgo = new SpatialKeyAlgo(bits).setBounds(minLon, maxLon, minLat, maxLat);
double deltaLat = (maxLat - minLat) / parts;
double deltaLon = (maxLon - minLon) / parts;
final ArrayList<Long> keys = new ArrayList<Long>();
PointEmitter tmpEmitter = new PointEmitter() {
@Override
public void set(double lat, double lon) {
keys.add(keyAlgo.encode(lat, lon));
}
};
keys.clear();
BresenhamLine.calcPoints(.3, -.3, -0.2, 0.2, tmpEmitter, minLat, minLon, deltaLat, deltaLon);
assertEquals(Arrays.asList(11L, 9L), keys);
keys.clear();
BresenhamLine.calcPoints(.3, -.1, -0.2, 0.4, tmpEmitter, minLat, minLon, deltaLat, deltaLon);
// 11, 9, 12
assertEquals(Arrays.asList(11L, 12L), keys);
keys.clear();
BresenhamLine.calcPoints(.5, -.5, -0.1, 0.9, tmpEmitter, minLat, minLon, deltaLat, deltaLon);
// precise: 10, 11, 14, 12
assertEquals(Arrays.asList(10L, 11L, 12L), keys);
}
use of com.graphhopper.geohash.SpatialKeyAlgo in project graphhopper by graphhopper.
the class LocationIndexTree method prepareAlgo.
void prepareAlgo() {
// 0.1 meter should count as 'equal'
equalNormedDelta = distCalc.calcNormalizedDist(0.1);
// now calculate the necessary maxDepth d for our current bounds
// if we assume a minimum resolution like 0.5km for a leaf-tile
// n^(depth/2) = toMeter(dLon) / minResolution
BBox bounds = graph.getBounds();
if (graph.getNodes() == 0)
throw new IllegalStateException("Cannot create location index of empty graph!");
if (!bounds.isValid())
throw new IllegalStateException("Cannot create location index when graph has invalid bounds: " + bounds);
double lat = Math.min(Math.abs(bounds.maxLat), Math.abs(bounds.minLat));
double maxDistInMeter = Math.max((bounds.maxLat - bounds.minLat) / 360 * DistanceCalcEarth.C, (bounds.maxLon - bounds.minLon) / 360 * preciseDistCalc.calcCircumference(lat));
double tmp = maxDistInMeter / minResolutionInMeter;
tmp = tmp * tmp;
IntArrayList tmpEntries = new IntArrayList();
// the last one is always 4 to reduce costs if only a single entry
tmp /= 4;
while (tmp > 1) {
int tmpNo;
if (tmp >= 64) {
tmpNo = 64;
} else if (tmp >= 16) {
tmpNo = 16;
} else if (tmp >= 4) {
tmpNo = 4;
} else {
break;
}
tmpEntries.add(tmpNo);
tmp /= tmpNo;
}
tmpEntries.add(4);
initEntries(tmpEntries.toArray());
int shiftSum = 0;
long parts = 1;
for (int i = 0; i < shifts.length; i++) {
shiftSum += shifts[i];
parts *= entries[i];
}
if (shiftSum > 64)
throw new IllegalStateException("sum of all shifts does not fit into a long variable");
keyAlgo = new SpatialKeyAlgo(shiftSum).bounds(bounds);
parts = Math.round(Math.sqrt(parts));
deltaLat = (bounds.maxLat - bounds.minLat) / parts;
deltaLon = (bounds.maxLon - bounds.minLon) / parts;
}
Aggregations