use of com.graphhopper.coll.GHIntHashSet in project graphhopper by graphhopper.
the class QueryGraph method createUncachedEdgeExplorer.
private EdgeExplorer createUncachedEdgeExplorer(EdgeFilter edgeFilter) {
// Iteration over virtual nodes needs to be thread safe if done from different explorer
// so we need to create the mapping on EVERY call!
// This needs to be a HashMap (and cannot be an array) as we also need to tweak edges for some mainNodes!
// The more query points we have the more inefficient this map could be. Hmmh.
final IntObjectMap<VirtualEdgeIterator> node2EdgeMap = new GHIntObjectHashMap<VirtualEdgeIterator>(queryResults.size() * 3);
final EdgeExplorer mainExplorer = mainGraph.createEdgeExplorer(edgeFilter);
final GHIntHashSet towerNodesToChange = new GHIntHashSet(queryResults.size());
// 1. virtualEdges should also get fresh EdgeIterators on every createEdgeExplorer call!
for (int i = 0; i < queryResults.size(); i++) {
// create outgoing edges
VirtualEdgeIterator virtEdgeIter = new VirtualEdgeIterator(2);
EdgeIteratorState baseRevEdge = virtualEdges.get(i * 4 + VE_BASE_REV);
if (edgeFilter.accept(baseRevEdge))
virtEdgeIter.add(baseRevEdge);
EdgeIteratorState adjEdge = virtualEdges.get(i * 4 + VE_ADJ);
if (edgeFilter.accept(adjEdge))
virtEdgeIter.add(adjEdge);
int virtNode = mainNodes + i;
node2EdgeMap.put(virtNode, virtEdgeIter);
// replace edge list of neighboring tower nodes:
// add virtual edges only and collect tower nodes where real edges will be added in step 2.
//
// base node
int towerNode = baseRevEdge.getAdjNode();
if (!isVirtualNode(towerNode)) {
towerNodesToChange.add(towerNode);
addVirtualEdges(node2EdgeMap, edgeFilter, true, towerNode, i);
}
// adj node
towerNode = adjEdge.getAdjNode();
if (!isVirtualNode(towerNode)) {
towerNodesToChange.add(towerNode);
addVirtualEdges(node2EdgeMap, edgeFilter, false, towerNode, i);
}
}
// 2. the connected tower nodes from mainGraph need fresh EdgeIterators with possible fakes
// where 'fresh' means independent of previous call and respecting the edgeFilter
// -> setup fake iterators of detected tower nodes (virtual edges are already added)
towerNodesToChange.forEach(new IntProcedure() {
@Override
public void apply(int value) {
fillVirtualEdges(node2EdgeMap, value, mainExplorer);
}
});
return new EdgeExplorer() {
@Override
public EdgeIterator setBaseNode(int baseNode) {
VirtualEdgeIterator iter = node2EdgeMap.get(baseNode);
if (iter != null)
return iter.reset();
return mainExplorer.setBaseNode(baseNode);
}
};
}
use of com.graphhopper.coll.GHIntHashSet in project graphhopper by graphhopper.
the class GraphEdgeIdFinderTest method testParseStringHints.
@Test
public void testParseStringHints() {
FlagEncoder encoder = new CarFlagEncoder();
EncodingManager em = new EncodingManager(encoder);
GraphHopperStorage graph = new GraphBuilder(em).create();
// 0-1-2
// | |
// 3-4
graph.edge(0, 1, 1, true);
graph.edge(1, 2, 1, true);
graph.edge(3, 4, 1, true);
graph.edge(0, 3, 1, true);
graph.edge(1, 4, 1, true);
AbstractRoutingAlgorithmTester.updateDistancesFor(graph, 0, 0.01, 0.00);
AbstractRoutingAlgorithmTester.updateDistancesFor(graph, 1, 0.01, 0.01);
AbstractRoutingAlgorithmTester.updateDistancesFor(graph, 2, 0.01, 0.02);
AbstractRoutingAlgorithmTester.updateDistancesFor(graph, 3, 0.00, 0.00);
AbstractRoutingAlgorithmTester.updateDistancesFor(graph, 4, 0.00, 0.01);
LocationIndex locationIndex = new LocationIndexTree(graph, new RAMDirectory()).prepareIndex();
GraphEdgeIdFinder graphFinder = new GraphEdgeIdFinder(graph, locationIndex);
GraphEdgeIdFinder.BlockArea blockArea = graphFinder.parseBlockArea("0.01,0.005,1", new DefaultEdgeFilter(encoder), 1000 * 1000);
GHIntHashSet blockedEdges = new GHIntHashSet();
blockedEdges.add(0);
assertEquals(blockedEdges, blockArea.blockedEdges);
List<Shape> blockedShapes = new ArrayList<>();
assertEquals(blockedShapes, blockArea.blockedShapes);
// big area converts into shapes
graphFinder = new GraphEdgeIdFinder(graph, locationIndex);
blockArea = graphFinder.parseBlockArea("0,0,1000", new DefaultEdgeFilter(encoder), 1000 * 1000);
blockedEdges.clear();
assertEquals(blockedEdges, blockArea.blockedEdges);
blockedShapes.add(new Circle(0, 0, 1000));
assertEquals(blockedShapes, blockArea.blockedShapes);
}
use of com.graphhopper.coll.GHIntHashSet in project graphhopper by graphhopper.
the class LocationIndexTreeTest method testInMemIndex2.
@Test
public void testInMemIndex2() {
Graph graph = createTestGraph2();
LocationIndexTree index = createIndexNoPrepare(graph, 500);
index.prepareAlgo();
LocationIndexTree.InMemConstructionIndex inMemIndex = index.getPrepareInMemIndex();
assertEquals(Helper.createTList(4, 4), index.getEntries());
assertEquals(3, inMemIndex.getEntriesOf(0).size());
assertEquals(5, inMemIndex.getEntriesOf(1).size());
assertEquals(0, inMemIndex.getEntriesOf(2).size());
index.dataAccess.create(10);
inMemIndex.store(inMemIndex.root, LocationIndexTree.START_POINTER);
// 0
assertEquals(2L, index.keyAlgo.encode(49.94653, 11.57114));
// 1
assertEquals(3L, index.keyAlgo.encode(49.94653, 11.57214));
// 28
assertEquals(6L, index.keyAlgo.encode(49.95053, 11.57714));
// 29
assertEquals(6L, index.keyAlgo.encode(49.95053, 11.57814));
// 8
assertEquals(1L, index.keyAlgo.encode(49.94553, 11.57214));
// 34
assertEquals(12L, index.keyAlgo.encode(49.95153, 11.57714));
// Query near point 25 (49.95053, 11.57314).
// If we would have a perfect compaction (takes a lot longer) we would
// get only 0 or any node in the lefter greater subgraph.
// The other subnetwork is already perfect {26}.
// For compaction see: https://github.com/graphhopper/graphhopper/blob/5594f7f9d98d932f365557dc37b4b2d3b7abf698/core/src/main/java/com/graphhopper/storage/index/Location2NodesNtree.java#L277
GHIntHashSet set = new GHIntHashSet();
set.addAll(28, 27, 26, 24, 23, 21, 19, 18, 16, 14, 6, 5, 4, 3, 2, 1, 0);
GHIntHashSet foundIds = new GHIntHashSet();
index.findNetworkEntries(49.950, 11.5732, foundIds, 0);
assertEquals(set, foundIds);
}
use of com.graphhopper.coll.GHIntHashSet in project graphhopper by graphhopper.
the class BridgeElevationInterpolatorTest method interpolatesElevationOfPillarNodes.
@Test
public void interpolatesElevationOfPillarNodes() {
// @formatter:off
/*
* Graph structure:
* 0-----1-----2-----3-----4
* \ | /
* \ | /
* T T T
* \ | /
* \|/
* 5-----6--T--7--T--8-----9
*/
// @formatter:on
NodeAccess na = graph.getNodeAccess();
na.setNode(0, 0, 0, 0);
na.setNode(1, 10, 0, 10);
na.setNode(2, 20, 0, 20);
na.setNode(3, 30, 0, 30);
na.setNode(4, 40, 0, 40);
na.setNode(5, 0, 10, 40);
na.setNode(6, 10, 10, 30);
na.setNode(7, 20, 10, 1000);
na.setNode(8, 30, 10, 10);
na.setNode(9, 40, 10, 0);
EdgeIteratorState edge01 = graph.edge(0, 1, 10, true);
EdgeIteratorState edge12 = graph.edge(1, 2, 10, true);
EdgeIteratorState edge23 = graph.edge(2, 3, 10, true);
EdgeIteratorState edge34 = graph.edge(3, 4, 10, true);
EdgeIteratorState edge56 = graph.edge(5, 6, 10, true);
EdgeIteratorState edge67 = graph.edge(6, 7, 10, true);
EdgeIteratorState edge78 = graph.edge(7, 8, 10, true);
EdgeIteratorState edge89 = graph.edge(8, 9, 10, true);
EdgeIteratorState edge17 = graph.edge(1, 7, 10, true);
EdgeIteratorState edge27 = graph.edge(2, 7, 10, true);
EdgeIteratorState edge37 = graph.edge(3, 7, 10, true);
edge17.setWayGeometry(Helper.createPointList3D(12, 2, 200, 14, 4, 400, 16, 6, 600, 18, 8, 800));
edge01.setFlags(dataFlagEncoder.handleWayTags(normalWay, 1, 0));
edge12.setFlags(dataFlagEncoder.handleWayTags(normalWay, 1, 0));
edge23.setFlags(dataFlagEncoder.handleWayTags(normalWay, 1, 0));
edge34.setFlags(dataFlagEncoder.handleWayTags(normalWay, 1, 0));
edge56.setFlags(dataFlagEncoder.handleWayTags(normalWay, 1, 0));
edge67.setFlags(dataFlagEncoder.handleWayTags(interpolatableWay, 1, 0));
edge78.setFlags(dataFlagEncoder.handleWayTags(interpolatableWay, 1, 0));
edge89.setFlags(dataFlagEncoder.handleWayTags(normalWay, 1, 0));
edge17.setFlags(dataFlagEncoder.handleWayTags(interpolatableWay, 1, 0));
edge27.setFlags(dataFlagEncoder.handleWayTags(interpolatableWay, 1, 0));
edge37.setFlags(dataFlagEncoder.handleWayTags(interpolatableWay, 1, 0));
final GHIntHashSet outerNodeIds = new GHIntHashSet();
final GHIntHashSet innerNodeIds = new GHIntHashSet();
gatherOuterAndInnerNodeIdsOfStructure(edge27, outerNodeIds, innerNodeIds);
assertEquals(GHIntHashSet.from(1, 2, 3, 6, 8), outerNodeIds);
assertEquals(GHIntHashSet.from(7), innerNodeIds);
edgeElevationInterpolator.execute();
assertEquals(0, na.getElevation(0), PRECISION);
assertEquals(10, na.getElevation(1), PRECISION);
assertEquals(20, na.getElevation(2), PRECISION);
assertEquals(30, na.getElevation(3), PRECISION);
assertEquals(40, na.getElevation(4), PRECISION);
assertEquals(40, na.getElevation(5), PRECISION);
assertEquals(30, na.getElevation(6), PRECISION);
assertEquals(20, na.getElevation(7), PRECISION);
assertEquals(10, na.getElevation(8), PRECISION);
assertEquals(0, na.getElevation(9), PRECISION);
final PointList edge17PointList = edge17.fetchWayGeometry(3);
assertEquals(6, edge17PointList.size());
assertEquals(10, edge17PointList.getEle(0), PRECISION);
assertEquals(12, edge17PointList.getEle(1), PRECISION);
assertEquals(14, edge17PointList.getEle(2), PRECISION);
assertEquals(16, edge17PointList.getEle(3), PRECISION);
assertEquals(18, edge17PointList.getEle(4), PRECISION);
assertEquals(20, edge17PointList.getEle(5), PRECISION);
}
use of com.graphhopper.coll.GHIntHashSet in project graphhopper by graphhopper.
the class LocationIndexTree method findClosest.
@Override
public QueryResult findClosest(final double queryLat, final double queryLon, final EdgeFilter edgeFilter) {
if (isClosed())
throw new IllegalStateException("You need to create a new LocationIndex instance as it is already closed");
GHIntHashSet allCollectedEntryIds = new GHIntHashSet();
final QueryResult closestMatch = new QueryResult(queryLat, queryLon);
for (int iteration = 0; iteration < maxRegionSearch; iteration++) {
GHIntHashSet storedNetworkEntryIds = new GHIntHashSet();
boolean earlyFinish = findNetworkEntries(queryLat, queryLon, storedNetworkEntryIds, iteration);
storedNetworkEntryIds.removeAll(allCollectedEntryIds);
allCollectedEntryIds.addAll(storedNetworkEntryIds);
// clone storedIds to avoid interference with forEach
final GHBitSet checkBitset = new GHTBitSet(new GHIntHashSet(storedNetworkEntryIds));
// find nodes from the network entries which are close to 'point'
final EdgeExplorer explorer = graph.createEdgeExplorer();
storedNetworkEntryIds.forEach(new IntPredicate() {
@Override
public boolean apply(int networkEntryNodeId) {
new XFirstSearchCheck(queryLat, queryLon, checkBitset, edgeFilter) {
@Override
protected double getQueryDistance() {
return closestMatch.getQueryDistance();
}
@Override
protected boolean check(int node, double normedDist, int wayIndex, EdgeIteratorState edge, QueryResult.Position pos) {
if (normedDist < closestMatch.getQueryDistance()) {
closestMatch.setQueryDistance(normedDist);
closestMatch.setClosestNode(node);
closestMatch.setClosestEdge(edge.detach(false));
closestMatch.setWayIndex(wayIndex);
closestMatch.setSnappedPosition(pos);
return true;
}
return false;
}
}.start(explorer, networkEntryNodeId);
return true;
}
});
// do early finish only if something was found (#318)
if (earlyFinish && closestMatch.isValid())
break;
}
// denormalize distance and calculate snapping point only if closed match was found
if (closestMatch.isValid()) {
closestMatch.setQueryDistance(distCalc.calcDenormalizedDist(closestMatch.getQueryDistance()));
closestMatch.calcSnappedPoint(distCalc);
}
return closestMatch;
}
Aggregations