use of com.graphhopper.util.shapes.GHPoint in project graphhopper by graphhopper.
the class RoundTripRoutingTemplate method lookup.
@Override
public List<QueryResult> lookup(List<GHPoint> points, FlagEncoder encoder) {
if (points.size() != 1 || ghRequest.getPoints().size() != 1)
throw new IllegalArgumentException("For round trip calculation exactly one point is required");
final double distanceInMeter = ghRequest.getHints().getDouble(RoundTrip.DISTANCE, 10000);
final long seed = ghRequest.getHints().getLong(RoundTrip.SEED, 0L);
double initialHeading = ghRequest.getFavoredHeading(0);
final int roundTripPointCount = Math.min(20, ghRequest.getHints().getInt(RoundTrip.POINTS, 2 + (int) (distanceInMeter / 50000)));
final GHPoint start = points.get(0);
TourStrategy strategy = new MultiPointTour(new Random(seed), distanceInMeter, roundTripPointCount, initialHeading);
queryResults = new ArrayList<>(2 + strategy.getNumberOfGeneratedPoints());
EdgeFilter edgeFilter = new DefaultEdgeFilter(encoder);
QueryResult startQR = locationIndex.findClosest(start.lat, start.lon, edgeFilter);
if (!startQR.isValid())
throw new PointNotFoundException("Cannot find point 0: " + start, 0);
queryResults.add(startQR);
GHPoint last = start;
for (int i = 0; i < strategy.getNumberOfGeneratedPoints(); i++) {
double heading = strategy.getHeadingForIteration(i);
QueryResult result = generateValidPoint(last, strategy.getDistanceForIteration(i), heading, edgeFilter);
if (result == null) {
ghResponse.addError(new IllegalStateException("Could not find a valid point after " + maxRetries + " tries, for the point:" + last));
return Collections.emptyList();
}
last = result.getSnappedPoint();
queryResults.add(result);
}
queryResults.add(startQR);
return queryResults;
}
use of com.graphhopper.util.shapes.GHPoint in project graphhopper by graphhopper.
the class LandmarkStorageTest method testWithBorderBlocking.
@Test
public void testWithBorderBlocking() {
AbstractRoutingAlgorithmTester.initBiGraph(ghStorage);
LandmarkStorage storage = new LandmarkStorage(ghStorage, new RAMDirectory(), new FastestWeighting(encoder), 2);
final SpatialRule ruleRight = new DefaultSpatialRule() {
@Override
public String getId() {
return "right";
}
};
final SpatialRule ruleLeft = new DefaultSpatialRule() {
@Override
public String getId() {
return "left";
}
};
final SpatialRuleLookup lookup = new SpatialRuleLookup() {
@Override
public SpatialRule lookupRule(double lat, double lon) {
if (lon > 0.00105)
return ruleRight;
return ruleLeft;
}
@Override
public SpatialRule lookupRule(GHPoint point) {
return lookupRule(point.lat, point.lon);
}
@Override
public int getSpatialId(SpatialRule rule) {
throw new IllegalStateException();
}
@Override
public int size() {
return 2;
}
@Override
public BBox getBounds() {
return new BBox(-180, 180, -90, 90);
}
};
storage.setSpatialRuleLookup(lookup);
storage.setMinimumNodes(2);
storage.createLandmarks();
assertEquals(3, storage.getSubnetworksWithLandmarks());
}
use of com.graphhopper.util.shapes.GHPoint in project graphhopper by graphhopper.
the class DataFlagEncoderTest method testSpatialId.
@Test
public void testSpatialId() {
final GermanySpatialRule germany = new GermanySpatialRule();
germany.setBorders(Collections.singletonList(new Polygon(new double[] { 0, 0, 1, 1 }, new double[] { 0, 1, 1, 0 })));
SpatialRuleLookup index = new SpatialRuleLookup() {
@Override
public SpatialRule lookupRule(double lat, double lon) {
for (Polygon polygon : germany.getBorders()) {
if (polygon.contains(lat, lon)) {
return germany;
}
}
return SpatialRule.EMPTY;
}
@Override
public SpatialRule lookupRule(GHPoint point) {
return lookupRule(point.lat, point.lon);
}
@Override
public int getSpatialId(SpatialRule rule) {
if (germany.equals(rule)) {
return 1;
} else {
return 0;
}
}
@Override
public int size() {
return 2;
}
@Override
public BBox getBounds() {
return new BBox(-180, 180, -90, 90);
}
};
DataFlagEncoder encoder = new DataFlagEncoder(new PMap());
encoder.setSpatialRuleLookup(index);
EncodingManager em = new EncodingManager(encoder);
ReaderWay way = new ReaderWay(27l);
way.setTag("highway", "track");
way.setTag("estimated_center", new GHPoint(0.005, 0.005));
ReaderWay way2 = new ReaderWay(28l);
way2.setTag("highway", "track");
way2.setTag("estimated_center", new GHPoint(-0.005, -0.005));
ReaderWay livingStreet = new ReaderWay(29l);
livingStreet.setTag("highway", "living_street");
livingStreet.setTag("estimated_center", new GHPoint(0.005, 0.005));
ReaderWay livingStreet2 = new ReaderWay(30l);
livingStreet2.setTag("highway", "living_street");
livingStreet2.setTag("estimated_center", new GHPoint(-0.005, -0.005));
Graph graph = new GraphBuilder(em).create();
EdgeIteratorState e1 = graph.edge(0, 1, 1, true);
EdgeIteratorState e2 = graph.edge(0, 2, 1, true);
EdgeIteratorState e3 = graph.edge(0, 3, 1, true);
EdgeIteratorState e4 = graph.edge(0, 4, 1, true);
AbstractRoutingAlgorithmTester.updateDistancesFor(graph, 0, 0.00, 0.00);
AbstractRoutingAlgorithmTester.updateDistancesFor(graph, 1, 0.01, 0.01);
AbstractRoutingAlgorithmTester.updateDistancesFor(graph, 2, -0.01, -0.01);
AbstractRoutingAlgorithmTester.updateDistancesFor(graph, 3, 0.01, 0.01);
AbstractRoutingAlgorithmTester.updateDistancesFor(graph, 4, -0.01, -0.01);
e1.setFlags(encoder.handleWayTags(way, 1, 0));
e2.setFlags(encoder.handleWayTags(way2, 1, 0));
e3.setFlags(encoder.handleWayTags(livingStreet, 1, 0));
e4.setFlags(encoder.handleWayTags(livingStreet2, 1, 0));
assertEquals(index.getSpatialId(new GermanySpatialRule()), encoder.getSpatialId(e1.getFlags()));
assertEquals(index.getSpatialId(SpatialRule.EMPTY), encoder.getSpatialId(e2.getFlags()));
assertEquals(AccessValue.EVENTUALLY_ACCESSIBLE, encoder.getAccessValue(e1.getFlags()));
assertEquals(AccessValue.ACCESSIBLE, encoder.getAccessValue(e2.getFlags()));
assertEquals(5, encoder.getMaxspeed(e3, -1, false), .1);
assertEquals(-1, encoder.getMaxspeed(e4, -1, false), .1);
}
use of com.graphhopper.util.shapes.GHPoint in project graphhopper by graphhopper.
the class SpatialRuleLookupArray method addRuleInternal.
private void addRuleInternal(SpatialRule rule) {
if (rule == null)
throw new IllegalArgumentException("rule cannot be null");
if (rule.equals(SpatialRule.EMPTY))
throw new IllegalArgumentException("rule cannot be EMPTY");
addSingleRule(rule);
int ruleContainerIndex = addRuleContainer(new SpatialRuleContainer().addRule(rule));
for (Polygon polygon : rule.getBorders()) {
for (int xIdx = getXIndexForLon(polygon.getMinLon()); xIdx < getXIndexForLon(polygon.getMaxLon()) + 1; xIdx++) {
for (int yIdx = getYIndexForLat(polygon.getMinLat()); yIdx < getYIndexForLat(polygon.getMaxLat()) + 1; yIdx++) {
if (xIdx >= lookupArray.length || yIdx >= lookupArray[0].length) {
continue;
}
GHPoint center = getCoordinatesForIndex(xIdx, yIdx);
// TODO: Consider creating a new method in Polygon that does the 5 checks - p.partOfTile?
if (polygon.contains(center) || polygon.contains(center.getLat() - checkDiff, center.getLon() - checkDiff) || polygon.contains(center.getLat() - checkDiff, center.getLon() + checkDiff) || polygon.contains(center.getLat() + checkDiff, center.getLon() - checkDiff) || polygon.contains(center.getLat() + checkDiff, center.getLon() + checkDiff)) {
if (lookupArray[xIdx][yIdx] == EMPTY_RULE_INDEX) {
lookupArray[xIdx][yIdx] = (byte) ruleContainerIndex;
} else {
// Merge Rules
SpatialRuleContainer curContainer = getContainerFor2DIndex(xIdx, yIdx);
SpatialRuleContainer newContainer = curContainer.copy().addRule(rule);
int newRuleContainerIndex = addRuleContainer(newContainer);
lookupArray[xIdx][yIdx] = (byte) newRuleContainerIndex;
}
}
}
}
}
}
use of com.graphhopper.util.shapes.GHPoint in project graphhopper by graphhopper.
the class GraphEdgeIdFinder method fillEdgeIDs.
/**
* This method fills the edgeIds hash with edgeIds found inside the specified geometry
*/
public void fillEdgeIDs(GHIntHashSet edgeIds, Geometry geometry, EdgeFilter filter) {
if (geometry instanceof Point) {
GHPoint point = GHPoint.create((Point) geometry);
findClosestEdgeToPoint(edgeIds, point, filter);
} else if (geometry instanceof LineString) {
PointList pl = PointList.from((LineString) geometry);
// TODO do map matching or routing
int lastIdx = pl.size() - 1;
if (pl.size() >= 2) {
double meanLat = (pl.getLatitude(0) + pl.getLatitude(lastIdx)) / 2;
double meanLon = (pl.getLongitude(0) + pl.getLongitude(lastIdx)) / 2;
findClosestEdge(edgeIds, meanLat, meanLon, filter);
}
} else if (geometry instanceof MultiPoint) {
for (Coordinate coordinate : geometry.getCoordinates()) {
findClosestEdge(edgeIds, coordinate.y, coordinate.x, filter);
}
}
}
Aggregations