Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

Expand Down Expand Up @@ -71,6 +73,12 @@ public class LocationIndexTree implements LocationIndex {
private double deltaLon;
private int initSizeLeafEntries = 4;
private boolean initialized = false;
private static final Comparator<QueryResult> QR_COMPARATOR = new Comparator<QueryResult>() {
@Override
public int compare(QueryResult o1, QueryResult o2) {
return Double.compare(o1.getQueryDistance(), o2.getQueryDistance());
}
};
/**
* If normed distance is smaller than this value the node or edge is 'identical' and the
* algorithm can stop search.
Expand Down Expand Up @@ -563,7 +571,120 @@ protected boolean check(int node, double normedDist, int wayIndex, EdgeIteratorS

return closestMatch;
}

/**
* Returns all edges that are within the specified radius around the queried position.
* Searches at most 9 cells to avoid performance problems. Hence, if the radius is larger than
* the cell width then not all edges might be returned.
*
* TODO: either clarify the method name and description (to only search e.g. 9 tiles) or
* refactor so it can handle a radius larger than 9 tiles. Also remove reference to 'NClosest',
* which is misleading, and don't always return at least one value. See map-matching #65.
* TODO: tidy up logic - see comments in graphhopper #994.
*
* @param radius in meters
*/
public List<QueryResult> findNClosest(final double queryLat, final double queryLon,
final EdgeFilter edgeFilter, double radius) {
// Return ALL results which are very close and e.g. within the GPS signal accuracy.
// Also important to get all edges if GPS point is close to a junction.
final double returnAllResultsWithin = distCalc.calcNormalizedDist(radius);

// implement a cheap priority queue via List, sublist and Collections.sort
final List<QueryResult> queryResults = new ArrayList<QueryResult>();
GHIntHashSet set = new GHIntHashSet();

// Doing 2 iterations means searching 9 tiles.
for (int iteration = 0; iteration < 2; iteration++) {
// should we use the return value of earlyFinish?
findNetworkEntries(queryLat, queryLon, set, iteration);

final GHBitSet exploredNodes = new GHTBitSet(new GHIntHashSet(set));
final EdgeExplorer explorer = graph.createEdgeExplorer(edgeFilter);

set.forEach(new IntPredicate() {

@Override
public boolean apply(int node) {
new XFirstSearchCheck(queryLat, queryLon, exploredNodes, edgeFilter) {
@Override
protected double getQueryDistance() {
// do not skip search if distance is 0 or near zero (equalNormedDelta)
return Double.MAX_VALUE;
}

@Override
protected boolean check(int node, double normedDist, int wayIndex, EdgeIteratorState edge, QueryResult.Position pos) {
if (normedDist < returnAllResultsWithin
|| queryResults.isEmpty()
|| queryResults.get(0).getQueryDistance() > normedDist) {
// TODO: refactor below:
// - should only add edges within search radius (below allows the
// returning of a candidate outside search radius if it's the only
// one). Removing this test would simplify it a lot and probably
// match the expected behaviour (see method description)
// - create QueryResult first and the add/set as required - clean up
// the index tracking business.

int index = -1;
for (int qrIndex = 0; qrIndex < queryResults.size(); qrIndex++) {
QueryResult qr = queryResults.get(qrIndex);
// overwrite older queryResults which are potentially more far away than returnAllResultsWithin
if (qr.getQueryDistance() > returnAllResultsWithin) {
index = qrIndex;
break;
}

// avoid duplicate edges
if (qr.getClosestEdge().getEdge() == edge.getEdge()) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why aren't we using a set/map here and filter potential unwanted results after the for loop?

With for loop I mean:
for (int iteration = 0; iteration < _maxIterations; iteration++) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree (I think) as per my comment about 'clean[ing] up the index tracking business'. However, again, I wanted to port the functionality and tests before tweaking. We can rewrite if you prefer ...

if (qr.getQueryDistance() < normedDist) {
// do not add current edge
return true;
} else {
// overwrite old edge with current
index = qrIndex;
break;
}
}
}

QueryResult qr = new QueryResult(queryLat, queryLon);
qr.setQueryDistance(normedDist);
qr.setClosestNode(node);
qr.setClosestEdge(edge.detach(false));
qr.setWayIndex(wayIndex);
qr.setSnappedPosition(pos);

if (index < 0) {
queryResults.add(qr);
} else {
queryResults.set(index, qr);
}
}
return true;
}
}.start(explorer, node);
return true;
}
});
}

// TODO: pass boolean argument for whether or not to sort? Can be expensive if not required.
Collections.sort(queryResults, QR_COMPARATOR);

for (QueryResult qr : queryResults) {
if (qr.isValid()) {
// denormalize distance
qr.setQueryDistance(distCalc.calcDenormalizedDist(qr.getQueryDistance()));
qr.calcSnappedPoint(distCalc);
} else {
throw new IllegalStateException("Invalid QueryResult should not happen here: " + qr);
}
}

return queryResults;
}

// make entries static as otherwise we get an additional reference to this class (memory waste)
interface InMemEntry {
boolean isLeaf();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@
import com.graphhopper.util.shapes.GHPoint;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
Expand Down Expand Up @@ -515,4 +518,56 @@ public void testCrossBoundaryNetwork_issue667() {
assertEquals(i, qr.getClosestNode());
}
}

// 0---1---2
// | | |
// |10 | |
// | | | |
// 3-9-4---5
// | | |
// 6---7---8
@Test
public void testFindNClosest() {
Graph graph = createGHStorage(new RAMDirectory(), encodingManager, false);
NodeAccess na = graph.getNodeAccess();
na.setNode(0, 0.0010, 0.0000);
na.setNode(1, 0.0010, 0.0005);
na.setNode(2, 0.0010, 0.0010);
na.setNode(3, 0.0005, 0.0000);
na.setNode(4, 0.0005, 0.0005);
na.setNode(5, 0.0005, 0.0010);
na.setNode(6, 0.0000, 0.0000);
na.setNode(7, 0.0000, 0.0005);
na.setNode(8, 0.0000, 0.0010);
na.setNode(9, 0.0005, 0.0002);
na.setNode(10, 0.0007, 0.0002);
graph.edge(0, 1);
graph.edge(1, 2);
graph.edge(0, 3);
graph.edge(1, 4);
graph.edge(2, 5);
graph.edge(3, 9);
graph.edge(9, 4);
EdgeIteratorState edge4_5 = graph.edge(4, 5);
graph.edge(10, 9);
graph.edge(3, 6);
EdgeIteratorState edge4_7 = graph.edge(4, 7);
graph.edge(5, 8);
graph.edge(6, 7);
graph.edge(7, 8);

LocationIndexTree index = createIndexNoPrepare(graph, 500);
index.prepareIndex();

// query node 4 => get at least 4-5, 4-7
List<QueryResult> result = index.findNClosest(0.0004, 0.0006, EdgeFilter.ALL_EDGES, 15);
List<Integer> ids = new ArrayList<Integer>();
for (QueryResult qr : result) {
ids.add(qr.getClosestEdge().getEdge());
}
Collections.sort(ids);
assertEquals("edge ids do not match",
Arrays.asList(edge4_5.getEdge(), edge4_7.getEdge()), ids);
}

}