I'm trying to get all street segments out of osm2po graph, given a geo point and a certain radius. Right now I just do a breadth first search out of the street that's closest to my point, but is there a better way?
class AreaIterator implements Iterator<DirectedStreet> {
private Set<Integer> visited = new HashSet<>();
private List<Integer> queue = new LinkedList<>();
private final Graph graph;
private final GeoPoint center;
private final float radius;
private final boolean twoSided;
public AreaIterator(Graph graph, GeoPoint center, float radius) {
this.graph = graph;
this.center = center;
this.radius = radius;
List<Integer> edges = getEdgesForPoint((float) center.lat(), (float) center.lon(), graph);
queue.addAll(edges);
}
private int step() {
int edgeIdx = 0;
if (!queue.isEmpty()) {
edgeIdx = queue.remove(0);
DirectedStreet street = getDirectedStreetFromEdgeIdx(graph, edgeIdx);
if (!visited.contains(edgeIdx)) {
visited.add(edgeIdx);
if (street.getGeom().distance(center.getGeom()) <= radius) {
int sourceId = graph.getEdgeSourceIds()[edgeIdx];
int targetId = graph.getEdgeTargetIds()[edgeIdx];
int [] next = graph.findOutgoingEdges(targetId);
int [] in1 = graph.findIncomingEdges(targetId);
int [] in2 = graph.findIncomingEdges(sourceId);
queueAll(next);
queueAll(in1);
queueAll(in2);
}
}
}
return edgeIdx;
}
private void queueAll(int[] in) {
for (int edgeIdx : in) {
queue.add(edgeIdx);
}
}
@Override
public boolean hasNext() {
return !queue.isEmpty();
}
@Override
public DirectedStreet next() {
return getDirectedStreetFromEdgeIdx(graph, step());
}
}