use of com.graphhopper.util.PointList in project graphhopper by graphhopper.
the class InstructionsHelper method getPointForOrientationCalculation.
static GHPoint getPointForOrientationCalculation(EdgeIteratorState edgeIteratorState, NodeAccess nodeAccess) {
double tmpLat;
double tmpLon;
PointList tmpWayGeo = edgeIteratorState.fetchWayGeometry(3);
if (tmpWayGeo.getSize() <= 2) {
tmpLat = nodeAccess.getLatitude(edgeIteratorState.getAdjNode());
tmpLon = nodeAccess.getLongitude(edgeIteratorState.getAdjNode());
} else {
tmpLat = tmpWayGeo.getLatitude(1);
tmpLon = tmpWayGeo.getLongitude(1);
}
return new GHPoint(tmpLat, tmpLon);
}
use of com.graphhopper.util.PointList 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.PointList in project graphhopper by graphhopper.
the class Bike2WeightFlagEncoder method applyWayTags.
@Override
public void applyWayTags(ReaderWay way, EdgeIteratorState edge) {
PointList pl = edge.fetchWayGeometry(3);
if (!pl.is3D())
throw new IllegalStateException("To support speed calculation based on elevation data it is necessary to enable import of it.");
long flags = edge.getFlags();
if (way.hasTag("tunnel", "yes") || way.hasTag("bridge", "yes") || way.hasTag("highway", "steps")) {
// do not change speed
// note: although tunnel can have a difference in elevation it is very unlikely that the elevation data is correct for a tunnel
} else {
// Decrease the speed for ele increase (incline), and decrease the speed for ele decrease (decline). The speed-decrease
// has to be bigger (compared to the speed-increase) for the same elevation difference to simulate loosing energy and avoiding hills.
// For the reverse speed this has to be the opposite but again keeping in mind that up+down difference.
double incEleSum = 0, incDist2DSum = 0;
double decEleSum = 0, decDist2DSum = 0;
// double prevLat = pl.getLatitude(0), prevLon = pl.getLongitude(0);
double prevEle = pl.getElevation(0);
double fullDist2D = edge.getDistance();
if (Double.isInfinite(fullDist2D))
throw new IllegalStateException("Infinite distance should not happen due to #435. way ID=" + way.getId());
// for short edges an incline makes no sense and for 0 distances could lead to NaN values for speed, see #432
if (fullDist2D < 1)
return;
double eleDelta = pl.getElevation(pl.size() - 1) - prevEle;
if (eleDelta > 0.1) {
incEleSum = eleDelta;
incDist2DSum = fullDist2D;
} else if (eleDelta < -0.1) {
decEleSum = -eleDelta;
decDist2DSum = fullDist2D;
}
// // get a more detailed elevation information, but due to bad SRTM data this does not make sense now.
// for (int i = 1; i < pl.size(); i++)
// {
// double lat = pl.getLatitude(i);
// double lon = pl.getLongitude(i);
// double ele = pl.getElevation(i);
// double eleDelta = ele - prevEle;
// double dist2D = distCalc.calcDist(prevLat, prevLon, lat, lon);
// if (eleDelta > 0.1)
// {
// incEleSum += eleDelta;
// incDist2DSum += dist2D;
// } else if (eleDelta < -0.1)
// {
// decEleSum += -eleDelta;
// decDist2DSum += dist2D;
// }
// fullDist2D += dist2D;
// prevLat = lat;
// prevLon = lon;
// prevEle = ele;
// }
// Calculate slop via tan(asin(height/distance)) but for rather smallish angles where we can assume tan a=a and sin a=a.
// Then calculate a factor which decreases or increases the speed.
// Do this via a simple quadratic equation where y(0)=1 and y(0.3)=1/4 for incline and y(0.3)=2 for decline
double fwdIncline = incDist2DSum > 1 ? incEleSum / incDist2DSum : 0;
double fwdDecline = decDist2DSum > 1 ? decEleSum / decDist2DSum : 0;
double restDist2D = fullDist2D - incDist2DSum - decDist2DSum;
double maxSpeed = getHighwaySpeed("cycleway");
if (isForward(flags)) {
// use weighted mean so that longer incline influences speed more than shorter
double speed = getSpeed(flags);
double fwdFaster = 1 + 2 * keepIn(fwdDecline, 0, 0.2);
fwdFaster = fwdFaster * fwdFaster;
double fwdSlower = 1 - 5 * keepIn(fwdIncline, 0, 0.2);
fwdSlower = fwdSlower * fwdSlower;
speed = speed * (fwdSlower * incDist2DSum + fwdFaster * decDist2DSum + 1 * restDist2D) / fullDist2D;
flags = this.setSpeed(flags, keepIn(speed, PUSHING_SECTION_SPEED / 2, maxSpeed));
}
if (isBackward(flags)) {
double speedReverse = getReverseSpeed(flags);
double bwFaster = 1 + 2 * keepIn(fwdIncline, 0, 0.2);
bwFaster = bwFaster * bwFaster;
double bwSlower = 1 - 5 * keepIn(fwdDecline, 0, 0.2);
bwSlower = bwSlower * bwSlower;
speedReverse = speedReverse * (bwFaster * incDist2DSum + bwSlower * decDist2DSum + 1 * restDist2D) / fullDist2D;
flags = this.setReverseSpeed(flags, keepIn(speedReverse, PUSHING_SECTION_SPEED / 2, maxSpeed));
}
}
edge.setFlags(flags);
}
use of com.graphhopper.util.PointList in project graphhopper by graphhopper.
the class SimpleRouteSerializer method toJSON.
@Override
public Map<String, Object> toJSON(GHResponse rsp, boolean calcPoints, boolean pointsEncoded, boolean includeElevation, boolean enableInstructions) {
Map<String, Object> json = new HashMap<String, Object>();
if (rsp.hasErrors()) {
json.put("message", getMessage(rsp.getErrors().get(0)));
List<Map<String, Object>> errorHintList = new ArrayList<>();
for (Throwable t : rsp.getErrors()) {
Map<String, Object> map = new HashMap<>();
map.put("message", getMessage(t));
map.put("details", t.getClass().getName());
if (t instanceof GHException) {
map.putAll(((GHException) t).getDetails());
}
errorHintList.add(map);
}
json.put("hints", errorHintList);
} else {
Map<String, Object> jsonInfo = new HashMap<String, Object>();
json.put("info", jsonInfo);
json.put("hints", rsp.getHints().toMap());
// If you replace GraphHopper with your own brand name, this is fine.
// Still it would be highly appreciated if you mention us in your about page!
jsonInfo.put("copyrights", Arrays.asList("GraphHopper", "OpenStreetMap contributors"));
List<Map<String, Object>> jsonPathList = new ArrayList<Map<String, Object>>();
for (PathWrapper ar : rsp.getAll()) {
Map<String, Object> jsonPath = new HashMap<String, Object>();
jsonPath.put("distance", Helper.round(ar.getDistance(), 3));
jsonPath.put("weight", Helper.round6(ar.getRouteWeight()));
jsonPath.put("time", ar.getTime());
jsonPath.put("transfers", ar.getNumChanges());
if (!ar.getDescription().isEmpty())
jsonPath.put("description", ar.getDescription());
if (calcPoints) {
jsonPath.put("points_encoded", pointsEncoded);
PointList points = ar.getPoints();
if (points.getSize() >= 2) {
BBox maxBounds2D = new BBox(maxBounds.minLon, maxBounds.maxLon, maxBounds.minLat, maxBounds.maxLat);
jsonPath.put("bbox", ar.calcRouteBBox(maxBounds2D).toGeoJson());
}
jsonPath.put("points", createPoints(points, pointsEncoded, includeElevation));
if (enableInstructions) {
InstructionList instructions = ar.getInstructions();
jsonPath.put("instructions", instructions.createJson());
}
jsonPath.put("legs", ar.getLegs());
jsonPath.put("details", ar.getPathDetails());
jsonPath.put("ascend", ar.getAscend());
jsonPath.put("descend", ar.getDescend());
}
jsonPath.put("snapped_waypoints", createPoints(ar.getWaypoints(), pointsEncoded, includeElevation));
if (ar.getFare() != null) {
jsonPath.put("fare", NumberFormat.getCurrencyInstance(Locale.ROOT).format(ar.getFare()));
}
jsonPathList.add(jsonPath);
}
json.put("paths", jsonPathList);
}
return json;
}
use of com.graphhopper.util.PointList in project PocketMaps by junjunguo.
the class MapHandler method startTrack.
/**
* start tracking : reset polylineTrack & trackingPointList & remove polylineTrack if exist
*/
public void startTrack() {
if (polylineTrack != null) {
removeLayer(mapView.map().layers(), polylineTrack);
}
polylineTrack = null;
trackingPointList = new PointList();
if (polylineTrack != null) {
polylineTrack.clearPath();
}
polylineTrack = updatePathLayer(polylineTrack, trackingPointList, 0x99003399, 4);
NaviEngine.getNaviEngine().startDebugSimulator(activity, true);
}
Aggregations