use of com.graphhopper.storage.index.QueryResult in project graphhopper by graphhopper.
the class AbstractRoutingAlgorithmTester method newQR.
/**
* Creates query result on edge (node1-node2) very close to node1.
*/
QueryResult newQR(Graph graph, int node1, int node2) {
EdgeIteratorState edge = GHUtility.getEdge(graph, node1, node2);
if (edge == null)
throw new IllegalStateException("edge not found? " + node1 + "-" + node2);
NodeAccess na = graph.getNodeAccess();
double lat = na.getLatitude(edge.getBaseNode());
double lon = na.getLongitude(edge.getBaseNode());
double latAdj = na.getLatitude(edge.getAdjNode());
double lonAdj = na.getLongitude(edge.getAdjNode());
// calculate query point near the base node but not directly on it!
QueryResult res = new QueryResult(lat + (latAdj - lat) * .1, lon + (lonAdj - lon) * .1);
res.setClosestNode(edge.getBaseNode());
res.setClosestEdge(edge);
res.setWayIndex(0);
res.setSnappedPosition(QueryResult.Position.EDGE);
res.calcSnappedPoint(distCalc);
return res;
}
use of com.graphhopper.storage.index.QueryResult 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.storage.index.QueryResult 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());
}
use of com.graphhopper.storage.index.QueryResult in project graphhopper by graphhopper.
the class NearestServlet method doGet.
@Override
public void doGet(HttpServletRequest httpReq, HttpServletResponse httpRes) throws ServletException, IOException {
String pointStr = getParam(httpReq, "point", null);
boolean enabledElevation = getBooleanParam(httpReq, "elevation", false);
ObjectNode result = objectMapper.createObjectNode();
if (pointStr != null && !pointStr.equalsIgnoreCase("")) {
GHPoint place = GHPoint.parse(pointStr);
QueryResult qr = index.findClosest(place.lat, place.lon, EdgeFilter.ALL_EDGES);
if (!qr.isValid()) {
result.put("error", "Nearest point cannot be found!");
} else {
GHPoint3D snappedPoint = qr.getSnappedPoint();
result.put("type", "Point");
ArrayNode coord = result.putArray("coordinates");
coord.add(snappedPoint.lon);
coord.add(snappedPoint.lat);
if (hasElevation && enabledElevation)
coord.add(snappedPoint.ele);
// Distance from input to snapped point in meters
result.put("distance", calc.calcDist(place.lat, place.lon, snappedPoint.lat, snappedPoint.lon));
}
} else {
result.put("error", "No lat/lon specified!");
}
writeJson(httpReq, httpRes, result);
}
Aggregations