use of com.graphhopper.util.shapes.GHPoint in project graphhopper by graphhopper.
the class GraphHopperOSMTest method testGetPathsDirectionEnforcement3.
@Test
public void testGetPathsDirectionEnforcement3() {
instance = createSquareGraphInstance(false);
// Start in middle of edge 4-5
GHPoint start = new GHPoint(0.0015, 0.002);
// End at middle of edge 2-3
GHPoint end = new GHPoint(0.002, 0.0005);
// Via Point betweeen 8-7
GHPoint via = new GHPoint(0.0005, 0.001);
GHRequest req = new GHRequest().addPoint(start).addPoint(via, 0.).addPoint(end);
GHResponse response = new GHResponse();
List<Path> paths = instance.calcPaths(req, response);
assertFalse(response.hasErrors());
assertEquals(IntArrayList.from(9, 5, 6, 7, 11), paths.get(0).calcNodes());
}
use of com.graphhopper.util.shapes.GHPoint in project graphhopper by graphhopper.
the class GraphHopperOSMTest method testGetPathsDirectionEnforcement5.
@Test
public void testGetPathsDirectionEnforcement5() {
// Test independence of previous enforcement for subsequent pathes
instance = createSquareGraphInstance(false);
// Start in middle of edge 4-5
GHPoint start = new GHPoint(0.0015, 0.002);
// End at middle of edge 2-3
GHPoint end = new GHPoint(0.002, 0.0005);
// First go south and than come from west to via-point at 7-6. Then go back over previously punished (11)-4 edge
GHPoint via = new GHPoint(0.000, 0.0015);
GHRequest req = new GHRequest().addPoint(start, 0.).addPoint(via, 3.14 / 2).addPoint(end);
req.getHints().put(Routing.PASS_THROUGH, true);
GHResponse response = new GHResponse();
List<Path> paths = instance.calcPaths(req, response);
assertFalse(response.hasErrors());
assertEquals(IntArrayList.from(9, 4, 3, 8, 7, 11), paths.get(0).calcNodes());
assertEquals(IntArrayList.from(11, 6, 5, 9, 4, 3, 10), paths.get(1).calcNodes());
}
use of com.graphhopper.util.shapes.GHPoint in project graphhopper by graphhopper.
the class OSMShapeFileReader method processRoads.
@Override
void processRoads() {
DataStore dataStore = null;
FeatureIterator<SimpleFeature> roads = null;
try {
dataStore = openShapefileDataStore(roadsFile, encoding);
roads = getFeatureIterator(dataStore);
while (roads.hasNext()) {
SimpleFeature road = roads.next();
for (Coordinate[] points : getCoords(road.getDefaultGeometry())) {
// Parse all points in the geometry, splitting into
// individual graphhopper edges
// whenever we find a node in the list of points
Coordinate startTowerPnt = null;
List<Coordinate> pillars = new ArrayList<Coordinate>();
for (Coordinate point : points) {
if (startTowerPnt == null) {
startTowerPnt = point;
} else {
int state = coordState.get(point);
if (state >= FIRST_NODE_ID) {
int fromTowerNodeId = coordState.get(startTowerPnt);
int toTowerNodeId = state;
// get distance and estimated centres
double distance = getWayLength(startTowerPnt, pillars, point);
GHPoint estmCentre = new GHPoint(0.5 * (lat(startTowerPnt) + lat(point)), 0.5 * (lng(startTowerPnt) + lng(point)));
PointList pillarNodes = new PointList(pillars.size(), false);
for (Coordinate pillar : pillars) {
pillarNodes.add(lat(pillar), lng(pillar));
}
addEdge(fromTowerNodeId, toTowerNodeId, road, distance, estmCentre, pillarNodes);
startTowerPnt = point;
pillars.clear();
} else {
pillars.add(point);
}
}
}
}
}
} finally {
if (roads != null) {
roads.close();
}
if (dataStore != null) {
dataStore.dispose();
}
}
}
use of com.graphhopper.util.shapes.GHPoint in project graphhopper by graphhopper.
the class RoundTripRoutingTemplateTest method testLookupAndCalcPaths_simpleSquareGraph.
@Test
public void testLookupAndCalcPaths_simpleSquareGraph() {
Graph g = createSquareGraph();
// start at node 0 and head south, make sure the round trip is long enough to reach most southern node 6
GHPoint start = new GHPoint(1, -1);
double heading = 180.0;
int numPoints = 2;
double roundTripDistance = 670000;
GHRequest ghRequest = new GHRequest(Collections.singletonList(start), Collections.singletonList(heading));
ghRequest.getHints().put(Parameters.Algorithms.RoundTrip.POINTS, numPoints);
ghRequest.getHints().put(Parameters.Algorithms.RoundTrip.DISTANCE, roundTripDistance);
LocationIndex locationIndex = new LocationIndexTree(g, new RAMDirectory()).prepareIndex();
RoundTripRoutingTemplate routingTemplate = new RoundTripRoutingTemplate(ghRequest, new GHResponse(), locationIndex, 1);
List<QueryResult> stagePoints = routingTemplate.lookup(ghRequest.getPoints(), carFE);
assertEquals(3, stagePoints.size());
assertEquals(0, stagePoints.get(0).getClosestNode());
assertEquals(6, stagePoints.get(1).getClosestNode());
assertEquals(0, stagePoints.get(2).getClosestNode());
QueryGraph queryGraph = new QueryGraph(g);
queryGraph.lookup(stagePoints);
Weighting weighting = new FastestWeighting(carFE);
List<Path> paths = routingTemplate.calcPaths(queryGraph, new RoutingAlgorithmFactorySimple(), new AlgorithmOptions(DIJKSTRA_BI, weighting, tMode));
// make sure the resulting paths are connected and form a round trip starting and ending at the start node 0
assertEquals(2, paths.size());
assertEquals(Helper.createTList(0, 7, 6, 5), paths.get(0).calcNodes());
assertEquals(Helper.createTList(5, 4, 3, 2, 1, 0), paths.get(1).calcNodes());
}
use of com.graphhopper.util.shapes.GHPoint in project graphhopper by graphhopper.
the class DataFlagEncoder method handleWayTags.
@Override
public long handleWayTags(ReaderWay way, long allowed, long relationFlags) {
if (!isAccept(allowed))
return 0;
try {
// HIGHWAY
int hwValue = getHighwayValue(way);
// exclude any routing like if you have car and need to exclude all rails or ships
if (hwValue == 0)
return 0;
long flags = 0;
if (isFerry(allowed)) {
hwValue = highwayMap.get("ferry");
}
flags = highwayEncoder.setValue(0, hwValue);
// MAXSPEED
double maxSpeed = parseSpeed(way.getTag("maxspeed"));
if (maxSpeed < 0) {
// TODO What if no maxspeed is set, but only forward and backward, and both are higher than the usually allowed?
maxSpeed = getSpatialRule(way).getMaxSpeed(way.getTag("highway", ""), maxSpeed);
}
double fwdSpeed = parseSpeed(way.getTag("maxspeed:forward"));
if (fwdSpeed < 0 && maxSpeed > 0)
fwdSpeed = maxSpeed;
if (fwdSpeed > getMaxPossibleSpeed())
fwdSpeed = getMaxPossibleSpeed();
double bwdSpeed = parseSpeed(way.getTag("maxspeed:backward"));
if (bwdSpeed < 0 && maxSpeed > 0)
bwdSpeed = maxSpeed;
if (bwdSpeed > getMaxPossibleSpeed())
bwdSpeed = getMaxPossibleSpeed();
// TODO and 140 should be used for "none" speed limit on German Autobahn
if (fwdSpeed > 0)
flags = carFwdMaxspeedEncoder.setDoubleValue(flags, fwdSpeed);
if (bwdSpeed > 0)
flags = carBwdMaxspeedEncoder.setDoubleValue(flags, bwdSpeed);
// Road attributes (height, weight, width)
if (isStoreHeight()) {
List<String> heightTags = Arrays.asList("maxheight", "maxheight:physical");
flags = extractMeter(way, flags, heightEncoder, heightTags);
}
if (isStoreWeight()) {
List<String> weightTags = Arrays.asList("maxweight", "maxgcweight");
flags = extractTons(way, flags, weightEncoder, weightTags);
}
if (isStoreWidth()) {
List<String> widthTags = Arrays.asList("maxwidth", "maxwidth:physical");
flags = extractMeter(way, flags, widthEncoder, widthTags);
}
// SURFACE
String surfaceValue = way.getTag("surface");
Integer sValue = surfaceMap.get(surfaceValue);
if (sValue == null)
sValue = 0;
flags = surfaceEncoder.setValue(flags, sValue);
// TRANSPORT MODE
int tmValue = 0;
for (String tm : transportModeList) {
if (way.hasTag(tm)) {
tmValue = transportModeMap.get(tm);
break;
}
}
flags = transportModeEncoder.setValue(flags, tmValue);
// ROUNDABOUT
boolean isRoundabout = way.hasTag("junction", "roundabout") || way.hasTag("junction", "circular");
if (isRoundabout)
flags = setBool(flags, K_ROUNDABOUT, true);
// ONEWAY (currently car only)
boolean isOneway = way.hasTag("oneway", oneways) || way.hasTag("vehicle:backward") || way.hasTag("vehicle:forward") || way.hasTag("motor_vehicle:backward") || way.hasTag("motor_vehicle:forward");
if (isOneway || isRoundabout) {
boolean isBackward = way.hasTag("oneway", "-1") || way.hasTag("vehicle:forward", "no") || way.hasTag("motor_vehicle:forward", "no");
if (isBackward)
flags |= backwardBit;
else
flags |= forwardBit;
} else
flags |= directionBitMask;
if (!isBit0Empty(flags))
throw new IllegalStateException("bit0 has to be empty on creation");
flags = accessEncoder.setValue(flags, getAccessValue(way));
GHPoint estimatedCenter = way.getTag("estimated_center", null);
if (estimatedCenter != null) {
SpatialRule rule = spatialRuleLookup.lookupRule(estimatedCenter);
flags = spatialEncoder.setValue(flags, spatialRuleLookup.getSpatialId(rule));
}
return flags;
} catch (Exception ex) {
throw new RuntimeException("Error while parsing way " + way.toString(), ex);
}
}
Aggregations