use of com.graphhopper.util.shapes.Circle in project graphhopper by graphhopper.
the class GraphEdgeIdFinder method parseStringHints.
/**
* This method reads string values from the hints about blocked areas and fills the configMap with either the
* created shapes or the found edges if area is small enough.
*/
public ConfigMap parseStringHints(ConfigMap configMap, HintsMap hints, EdgeFilter filter) {
final String objectSeparator = ";";
final String innerObjSep = ",";
// use shapes if bigger than 1km^2
final double shapeArea = 1000 * 1000;
final GHIntHashSet blockedEdges = new GHIntHashSet();
final List<Shape> blockedShapes = new ArrayList<>();
// Add blocked circular areas or points
String blockedCircularAreasStr = hints.get(BLOCK_AREA, "");
if (!blockedCircularAreasStr.isEmpty()) {
String[] blockedCircularAreasArr = blockedCircularAreasStr.split(objectSeparator);
for (int i = 0; i < blockedCircularAreasArr.length; i++) {
String objectAsString = blockedCircularAreasArr[i];
String[] splittedObject = objectAsString.split(innerObjSep);
if (splittedObject.length == 4) {
final BBox bbox = BBox.parseTwoPoints(objectAsString);
if (bbox.calculateArea() > shapeArea)
blockedShapes.add(bbox);
else
findEdgesInShape(blockedEdges, bbox, filter);
} else if (splittedObject.length == 3) {
double lat = Double.parseDouble(splittedObject[0]);
double lon = Double.parseDouble(splittedObject[1]);
int radius = Integer.parseInt(splittedObject[2]);
Circle circle = new Circle(lat, lon, radius);
if (circle.calculateArea() > shapeArea) {
blockedShapes.add(circle);
} else {
findEdgesInShape(blockedEdges, circle, filter);
}
} else if (splittedObject.length == 2) {
double lat = Double.parseDouble(splittedObject[0]);
double lon = Double.parseDouble(splittedObject[1]);
findClosestEdge(blockedEdges, lat, lon, filter);
} else {
throw new IllegalArgumentException(objectAsString + " at index " + i + " need to be defined as lat,lon " + "or as a circle lat,lon,radius or rectangular lat1,lon1,lat2,lon2");
}
}
}
configMap.put(BLOCKED_EDGES, blockedEdges);
configMap.put(BLOCKED_SHAPES, blockedShapes);
return configMap;
}
use of com.graphhopper.util.shapes.Circle in project graphhopper by graphhopper.
the class GenericWeightingTest method testBlockedByShape.
@Test
public void testBlockedByShape() {
EdgeIteratorState edge = graph.getEdgeIteratorState(0, 1);
ConfigMap cMap = encoder.readStringMap(new PMap());
GenericWeighting instance = new GenericWeighting(encoder, cMap);
assertEquals(edgeWeight, instance.calcWeight(edge, false, EdgeIterator.NO_EDGE), 1e-8);
List<Shape> shapes = new ArrayList<>(1);
shapes.add(new Circle(0.01, 0.01, 100));
cMap.put(BLOCKED_SHAPES, shapes);
instance = new GenericWeighting(encoder, cMap);
instance.setGraph(graph);
assertEquals(Double.POSITIVE_INFINITY, instance.calcWeight(edge, false, EdgeIterator.NO_EDGE), 1e-8);
shapes.clear();
// Do not match 1,1 of edge
shapes.add(new Circle(0.1, 0.1, 100));
cMap.put(BLOCKED_SHAPES, shapes);
instance = new GenericWeighting(encoder, cMap);
instance.setGraph(graph);
assertEquals(edgeWeight, instance.calcWeight(edge, false, EdgeIterator.NO_EDGE), 1e-8);
}
use of com.graphhopper.util.shapes.Circle in project graphhopper by graphhopper.
the class Location2IDFullIndex method findClosest.
@Override
public QueryResult findClosest(double queryLat, double queryLon, EdgeFilter edgeFilter) {
if (isClosed())
throw new IllegalStateException("You need to create a new LocationIndex instance as it is already closed");
QueryResult res = new QueryResult(queryLat, queryLon);
Circle circle = null;
AllEdgesIterator iter = graph.getAllEdges();
while (iter.next()) {
if (!edgeFilter.accept(iter))
continue;
for (int node, i = 0; i < 2; i++) {
if (i == 0) {
node = iter.getBaseNode();
} else {
node = iter.getAdjNode();
}
double tmpLat = nodeAccess.getLatitude(node);
double tmpLon = nodeAccess.getLongitude(node);
double dist = calc.calcDist(tmpLat, tmpLon, queryLat, queryLon);
if (circle == null || dist < calc.calcDist(circle.getLat(), circle.getLon(), queryLat, queryLon)) {
res.setClosestEdge(iter.detach(false));
res.setClosestNode(node);
res.setQueryDistance(dist);
if (dist <= 0)
break;
circle = new Circle(tmpLat, tmpLon, dist, calc);
}
}
}
return res;
}
use of com.graphhopper.util.shapes.Circle 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();
HintsMap hints = new HintsMap();
hints.put(Parameters.Routing.BLOCK_AREA, "0.01,0.005,1");
ConfigMap cMap = new ConfigMap();
GraphEdgeIdFinder graphFinder = new GraphEdgeIdFinder(graph, locationIndex);
ConfigMap result = graphFinder.parseStringHints(cMap, hints, new DefaultEdgeFilter(encoder));
GHIntHashSet blockedEdges = new GHIntHashSet();
blockedEdges.add(0);
assertEquals(blockedEdges, result.get(BLOCKED_EDGES, new GHIntHashSet()));
List<Shape> blockedShapes = new ArrayList<>();
assertEquals(blockedShapes, result.get(BLOCKED_SHAPES, new ArrayList<>()));
// big area converts into shapes
hints.put(Parameters.Routing.BLOCK_AREA, "0,0,1000");
result = graphFinder.parseStringHints(cMap, hints, new DefaultEdgeFilter(encoder));
blockedEdges.clear();
assertEquals(blockedEdges, result.get(BLOCKED_EDGES, new GHIntHashSet()));
blockedShapes.add(new Circle(0, 0, 1000));
assertEquals(blockedShapes, result.get(BLOCKED_SHAPES, new ArrayList<>()));
}
Aggregations