use of com.graphhopper.util.shapes.GHPoint in project graphhopper by graphhopper.
the class LocationIndexTree method calculateRMin.
/**
* Calculates the distance to the nearest tile border, where the tile border is the rectangular
* region with dimension 2*paddingTiles + 1 and where the center tile contains the given lat/lon
* coordinate
*/
final double calculateRMin(double lat, double lon, int paddingTiles) {
GHPoint query = new GHPoint(lat, lon);
long key = keyAlgo.encode(query);
GHPoint center = new GHPoint();
keyAlgo.decode(key, center);
// deltaLat and deltaLon comes from the LocationIndex:
double minLat = center.lat - (0.5 + paddingTiles) * deltaLat;
double maxLat = center.lat + (0.5 + paddingTiles) * deltaLat;
double minLon = center.lon - (0.5 + paddingTiles) * deltaLon;
double maxLon = center.lon + (0.5 + paddingTiles) * deltaLon;
double dSouthernLat = query.lat - minLat;
double dNorthernLat = maxLat - query.lat;
double dWesternLon = query.lon - minLon;
double dEasternLon = maxLon - query.lon;
// convert degree deltas into a radius in meter
double dMinLat, dMinLon;
if (dSouthernLat < dNorthernLat) {
dMinLat = distCalc.calcDist(query.lat, query.lon, minLat, query.lon);
} else {
dMinLat = distCalc.calcDist(query.lat, query.lon, maxLat, query.lon);
}
if (dWesternLon < dEasternLon) {
dMinLon = distCalc.calcDist(query.lat, query.lon, query.lat, minLon);
} else {
dMinLon = distCalc.calcDist(query.lat, query.lon, query.lat, maxLon);
}
double rMin = Math.min(dMinLat, dMinLon);
return rMin;
}
use of com.graphhopper.util.shapes.GHPoint in project graphhopper by graphhopper.
the class QueryResult method calcSnappedPoint.
/**
* Calculates the closet point on the edge from the query point.
*/
public void calcSnappedPoint(DistanceCalc distCalc) {
if (closestEdge == null)
throw new IllegalStateException("No closest edge?");
if (snappedPoint != null)
throw new IllegalStateException("Calculate snapped point only once");
PointList fullPL = getClosestEdge().fetchWayGeometry(3);
double tmpLat = fullPL.getLatitude(wayIndex);
double tmpLon = fullPL.getLongitude(wayIndex);
double tmpEle = fullPL.getElevation(wayIndex);
if (snappedPosition != Position.EDGE) {
snappedPoint = new GHPoint3D(tmpLat, tmpLon, tmpEle);
return;
}
double queryLat = getQueryPoint().lat, queryLon = getQueryPoint().lon;
double adjLat = fullPL.getLatitude(wayIndex + 1), adjLon = fullPL.getLongitude(wayIndex + 1);
if (distCalc.validEdgeDistance(queryLat, queryLon, tmpLat, tmpLon, adjLat, adjLon)) {
GHPoint tmpPoint = distCalc.calcCrossingPointToEdge(queryLat, queryLon, tmpLat, tmpLon, adjLat, adjLon);
double adjEle = fullPL.getElevation(wayIndex + 1);
snappedPoint = new GHPoint3D(tmpPoint.lat, tmpPoint.lon, (tmpEle + adjEle) / 2);
} else
// outside of edge boundaries
snappedPoint = new GHPoint3D(tmpLat, tmpLon, tmpEle);
}
use of com.graphhopper.util.shapes.GHPoint in project graphhopper by graphhopper.
the class SpatialKeyAlgoTest method testBijectionBug2.
@Test
public void testBijectionBug2() {
for (long i = 4; i <= 64; i += 4) {
SpatialKeyAlgo algo = new SpatialKeyAlgo((int) i);
long keyX = algo.encode(1, 1);
GHPoint coord = new GHPoint();
algo.decode(keyX, coord);
long keyY = algo.encode(coord.lat, coord.lon);
GHPoint coord2 = new GHPoint();
algo.decode(keyY, coord2);
double dist = new DistanceCalcEarth().calcDist(coord.lat, coord.lon, coord2.lat, coord2.lon);
assertEquals(0, dist, 1e-5);
// System.out.println("\n\n##" + i + "\nkeyX:" + BitUtil.BIG.toBitString(keyX));
// System.out.println("keyY:" + BitUtil.BIG.toBitString(keyY));
// System.out.println("distanceX:" + dist + " precision:" + precision + " difference:" + (dist - precision) + " factor:" + dist / precision);
}
}
use of com.graphhopper.util.shapes.GHPoint in project graphhopper by graphhopper.
the class QueryGraphTest method testMultipleVirtualNodes.
@Test
public void testMultipleVirtualNodes() {
initGraph(g);
// snap to edge which has pillar nodes
EdgeIterator iter = g.createEdgeExplorer().setBaseNode(1);
iter.next();
QueryResult res1 = createLocationResult(2, 1.7, iter, 1, PILLAR);
QueryGraph queryGraph = new QueryGraph(g);
queryGraph.lookup(Arrays.asList(res1));
assertEquals(new GHPoint(1.5, 1.5), res1.getSnappedPoint());
assertEquals(3, res1.getClosestNode());
assertEquals(4, getPoints(queryGraph, 0, 3).getSize());
PointList pl = getPoints(queryGraph, 3, 1);
assertEquals(2, pl.getSize());
assertEquals(new GHPoint(1.5, 1.5), pl.toGHPoint(0));
assertEquals(new GHPoint(1, 2.5), pl.toGHPoint(1));
EdgeIteratorState edge = GHUtility.getEdge(queryGraph, 3, 1);
assertNotNull(queryGraph.getEdgeIteratorState(edge.getEdge(), 3));
assertNotNull(queryGraph.getEdgeIteratorState(edge.getEdge(), 1));
edge = GHUtility.getEdge(queryGraph, 3, 0);
assertNotNull(queryGraph.getEdgeIteratorState(edge.getEdge(), 3));
assertNotNull(queryGraph.getEdgeIteratorState(edge.getEdge(), 0));
// snap again => new virtual node on same edge!
iter = g.createEdgeExplorer().setBaseNode(1);
iter.next();
res1 = createLocationResult(2, 1.7, iter, 1, PILLAR);
QueryResult res2 = createLocationResult(1.5, 2, iter, 0, EDGE);
queryGraph = new QueryGraph(g);
queryGraph.lookup(Arrays.asList(res1, res2));
assertEquals(4, res2.getClosestNode());
assertEquals(new GHPoint(1.300019, 1.899962), res2.getSnappedPoint());
assertEquals(3, res1.getClosestNode());
assertEquals(new GHPoint(1.5, 1.5), res1.getSnappedPoint());
assertEquals(4, getPoints(queryGraph, 3, 0).getSize());
assertEquals(2, getPoints(queryGraph, 3, 4).getSize());
assertEquals(2, getPoints(queryGraph, 4, 1).getSize());
assertNull(GHUtility.getEdge(queryGraph, 4, 0));
assertNull(GHUtility.getEdge(queryGraph, 3, 1));
}
use of com.graphhopper.util.shapes.GHPoint in project graphhopper by graphhopper.
the class QueryGraphTest method testOneVirtualNode.
@Test
public void testOneVirtualNode() {
initGraph(g);
EdgeExplorer expl = g.createEdgeExplorer();
// snap directly to tower node => pointList could get of size 1?!?
// a)
EdgeIterator iter = expl.setBaseNode(2);
iter.next();
QueryGraph queryGraph = new QueryGraph(g);
QueryResult res = createLocationResult(1, -1, iter, 0, TOWER);
queryGraph.lookup(Arrays.asList(res));
assertEquals(new GHPoint(0, 0), res.getSnappedPoint());
// b)
res = createLocationResult(1, -1, iter, 1, TOWER);
queryGraph = new QueryGraph(g);
queryGraph.lookup(Arrays.asList(res));
assertEquals(new GHPoint(1, 0), res.getSnappedPoint());
// c)
iter = expl.setBaseNode(1);
iter.next();
res = createLocationResult(1.2, 2.7, iter, 0, TOWER);
queryGraph = new QueryGraph(g);
queryGraph.lookup(Arrays.asList(res));
assertEquals(new GHPoint(1, 2.5), res.getSnappedPoint());
// node number stays
assertEquals(3, queryGraph.getNodes());
// snap directly to pillar node
queryGraph = new QueryGraph(g);
iter = expl.setBaseNode(1);
iter.next();
res = createLocationResult(2, 1.5, iter, 1, PILLAR);
queryGraph.lookup(Arrays.asList(res));
assertEquals(new GHPoint(1.5, 1.5), res.getSnappedPoint());
assertEquals(3, res.getClosestNode());
assertEquals(4, getPoints(queryGraph, 0, 3).getSize());
assertEquals(2, getPoints(queryGraph, 3, 1).getSize());
queryGraph = new QueryGraph(g);
res = createLocationResult(2, 1.7, iter, 1, PILLAR);
queryGraph.lookup(Arrays.asList(res));
assertEquals(new GHPoint(1.5, 1.5), res.getSnappedPoint());
assertEquals(3, res.getClosestNode());
assertEquals(4, getPoints(queryGraph, 0, 3).getSize());
assertEquals(2, getPoints(queryGraph, 3, 1).getSize());
// snap to edge which has pillar nodes
queryGraph = new QueryGraph(g);
res = createLocationResult(1.5, 2, iter, 0, EDGE);
queryGraph.lookup(Arrays.asList(res));
assertEquals(new GHPoint(1.300019, 1.899962), res.getSnappedPoint());
assertEquals(3, res.getClosestNode());
assertEquals(4, getPoints(queryGraph, 0, 3).getSize());
assertEquals(2, getPoints(queryGraph, 3, 1).getSize());
// snap to edge which has no pillar nodes
queryGraph = new QueryGraph(g);
iter = expl.setBaseNode(2);
iter.next();
res = createLocationResult(0.5, 0.1, iter, 0, EDGE);
queryGraph.lookup(Arrays.asList(res));
assertEquals(new GHPoint(0.5, 0), res.getSnappedPoint());
assertEquals(3, res.getClosestNode());
assertEquals(2, getPoints(queryGraph, 0, 3).getSize());
assertEquals(2, getPoints(queryGraph, 3, 2).getSize());
}
Aggregations