setSimplifyResponse(false). setEnableInstructions(true); PathWrapper rsp = new PathWrapper(); pathMerger.doWork(rsp, altPaths, trMap.getWithFallBack(Locale.US)); if (rsp.hasErrors()) { errors.add("response for " + algoEntry + " contains errors. Expected distance: " + oneRun.getDistance() + ", expected points: " + oneRun + ". " + queryList + ", errors:" + rsp.getErrors()); return this; PointList pointList = rsp.getPoints(); double tmpDist = pointList.calcDistance(distCalc); if (Math.abs(rsp.getDistance() - tmpDist) > 2) { errors.add(algoEntry + " path.getDistance was " + rsp.getDistance() + "\t pointList.calcDistance was " + tmpDist + "\t (expected points " + oneRun.getLocs() + ", expected distance " + oneRun.getDistance() + ") " + queryList); if (Math.abs(rsp.getDistance() - oneRun.getDistance()) > 2) { errors.add(algoEntry + " returns path not matching the expected distance of " + oneRun.getDistance() + "\t Returned was " + rsp.getDistance() + "\t (expected points " + oneRun.getLocs() + ", was " + pointList.getSize() + ") " + queryList); errors.add(algoEntry + " returns path not matching the expected points of " + oneRun.getLocs() + "\t Returned was " + pointList.getSize() + "\t (expected distance " + oneRun.getDistance() + ", was " + rsp.getDistance() + ") " + queryList);
for (PathWrapper ar : ghRsp.getAll()) { ObjectNode jsonPath = jsonPathList.addObject(); 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.putPOJO("description", ar.getDescription()); if (ar.getPoints().getSize() >= 2) { jsonPath.putPOJO("bbox", ar.calcBBox2D()); jsonPath.putPOJO("points", pointsEncoded ? encodePolyline(ar.getPoints(), enableElevation) : ar.getPoints().toLineString(enableElevation)); if (enableInstructions) { jsonPath.putPOJO("instructions", ar.getInstructions()); jsonPath.putPOJO("legs", ar.getLegs()); jsonPath.putPOJO("details", ar.getPathDetails()); jsonPath.put("ascend", ar.getAscend()); jsonPath.put("descend", ar.getDescend()); jsonPath.putPOJO("snapped_waypoints", pointsEncoded ? encodePolyline(ar.getWaypoints(), enableElevation) : ar.getWaypoints().toLineString(enableElevation)); if (ar.getFare() != null) { jsonPath.put("fare", NumberFormat.getCurrencyInstance(Locale.ROOT).format(ar.getFare()));
altRsp.addPathDetails(path.calcDetails(requestedPathDetails, pathBuilderFactory, origPoints)); origPoints = fullPoints.size(); String debug = altRsp.getDebugInfo() + ", simplify (" + origPoints + "->" + fullPoints.getSize() + ")"; altRsp.addDebugInfo(debug); if (fullPoints.is3D) calcAscendDescend(altRsp, fullPoints); altRsp.setInstructions(fullInstructions); altRsp.addError(new ConnectionNotFoundException("Connection between locations not found", Collections.<String, Object>emptyMap())); altRsp.setDescription(description). setPoints(fullPoints). setRouteWeight(fullWeight). setDistance(fullDistance). setTime(fullTimeInMillis);
public PathSimplification(PathWrapper pathWrapper, DouglasPeucker douglasPeucker, boolean enableInstructions) { this.pointList = pathWrapper.getPoints(); listsToSimplify = new ArrayList<>(); if (enableInstructions) listsToSimplify.add(pathWrapper.getInstructions()); this.pathDetails = pathWrapper.getPathDetails(); for (String name : pathDetails.keySet()) { List<PathDetail> pathDetailList = pathDetails.get(name); // If the pointList only contains one point, PathDetails have to be empty because 1 point => 0 edges if (pathDetailList.isEmpty() && pointList.size() > 1) throw new IllegalStateException("PathDetails " + name + " must not be empty"); listsToSimplify.add(pathDetailList); } this.douglasPeucker = douglasPeucker; }
protected void onPostExecute(PathWrapper resp) { if (!resp.hasErrors()) { log("from:" + fromLat + "," + fromLon + " to:" + toLat + "," + toLon + " found path with distance:" + resp.getDistance() / 1000f + ", nodes:" + resp.getPoints().getSize() + ", time:" + time + " " + resp.getDebugInfo()); logUser("the route is " + (int) (resp.getDistance() / 100) / 10f + "km long, time:" + resp.getTime() / 60000f + "min, debug:" + time); pathLayer = createPathLayer(resp); mapView.map().layers().add(pathLayer); mapView.map().updateMap(true); } else { logUser("Error:" + resp.getErrors()); } shortestPathRunning = false; } }.execute();
PathWrapper createPathWrapper(JsonNode path, boolean tmpElevation, boolean turnDescription) { PathWrapper pathWrapper = new PathWrapper(); pathWrapper.addErrors(readErrors(path)); if (pathWrapper.hasErrors()) return pathWrapper; String snappedPointStr = path.get("snapped_waypoints").asText(); PointList snappedPoints = WebHelper.decodePolyline(snappedPointStr, 5, tmpElevation); pathWrapper.setWaypoints(snappedPoints); pathWrapper.setAscend(path.get("ascend").asDouble()); pathWrapper.setDescend(path.get("descend").asDouble()); pathWrapper.setRouteWeight(path.get("weight").asDouble()); pathWrapper.setDescription(description); } else { throw new IllegalStateException("Description has to be an array"); String pointStr = path.get("points").asText(); PointList pointList = WebHelper.decodePolyline(pointStr, 100, tmpElevation); pathWrapper.setPoints(pointList); pathWrapper.setInstructions(il); pathWrapper.addPathDetails(pathDetails); pathWrapper.setDistance(distance).setTime(time); return pathWrapper;
PathWrapper path = new PathWrapper(); path.setWaypoints(waypoints); path.getLegs().addAll(legs); final InstructionList instructions = getInstructions(tr, path.getLegs()); path.setInstructions(instructions); PointList pointsList = new PointList(); for (Instruction instruction : instructions) { pointsList.add(instruction.getPoints()); path.setPoints(pointsList); path.setDistance(path.getLegs().stream().mapToDouble(Trip.Leg::getDistance).sum()); path.setTime((legs.get(legs.size() - 1).getArrivalTime().toInstant().toEpochMilli() - legs.get(0).getDepartureTime().toInstant().toEpochMilli())); path.setNumChanges((int) path.getLegs().stream() .filter(l -> l instanceof Trip.PtLeg) .filter(l -> !((Trip.PtLeg) l).isInSameVehicleAsPrevious) .count() - 1); com.graphhopper.gtfs.fare.Trip faresTrip = new com.graphhopper.gtfs.fare.Trip(); path.getLegs().stream() .filter(leg -> leg instanceof Trip.PtLeg) .map(leg -> (Trip.PtLeg) leg) .ifPresent(firstPtLeg -> { LocalDateTime firstPtDepartureTime = GtfsHelper.localDateTimeFromDate(firstPtLeg.getDepartureTime()); path.getLegs().stream() .filter(leg -> leg instanceof Trip.PtLeg) .map(leg -> (Trip.PtLeg) leg)
} else { logger.info(logStr + ", alternatives: " + ghResponse.getAll().size() + ", distance0: " + ghResponse.getBest().getDistance() + ", weight0: " + ghResponse.getBest().getRouteWeight() + ", time0: " + Math.round(ghResponse.getBest().getTime() / 60000f) + "min" + ", points0: " + ghResponse.getBest().getPoints().getSize() + ", debugInfo: " + ghResponse.getDebugInfo()); return writeGPX ?
@Override public boolean isReady(PathMerger pathMerger, Translation tr) { altResponse = new PathWrapper(); altResponse.setWaypoints(getWaypoints()); ghResponse.add(altResponse); pathMerger.doWork(altResponse, pathList, tr); // with potentially retrying, including generating new route points, for now disabled return true; }
Map<String, List<PathDetail>> details = p.calcDetails(Arrays.asList(DETAILS.AVERAGE_SPEED), new PathDetailsBuilderFactory(), 0); PathWrapper pathWrapper = new PathWrapper(); pathWrapper.setInstructions(wayList); pathWrapper.addPathDetails(details); pathWrapper.setPoints(p.calcPoints()); ps.simplify(); assertEquals(numberOfPoints, pathWrapper.getPoints().size()); pathWrapper = new PathWrapper(); pathWrapper.setInstructions(wayList); pathWrapper.addPathDetails(details); pathWrapper.setPoints(p.calcPoints()); ps.simplify(); assertTrue(numberOfPoints > pathWrapper.getPoints().size());
if (!warmup) { visitedNodesSum.addAndGet(rsp.getHints().getLong("visited_nodes.sum", 0)); long dist = (long) arsp.getDistance(); distSum.addAndGet(dist); return arsp.getPoints().getSize();
public RoutingResult getRouteBetween(NavigationPosition from, NavigationPosition to, TravelMode travelMode) { initializeHopper(); long start = currentTimeMillis(); try { GHRequest request = new GHRequest(from.getLatitude(), from.getLongitude(), to.getLatitude(), to.getLongitude()); request.setVehicle(travelMode.getName().toUpperCase()); GHResponse response = hopper.route(request); if(response.hasErrors()) { String errors = printArrayToDialogString(response.getErrors().toArray(), false); log.severe(format("Error while routing between %s and %s: %s", from, to, errors)); throw new RuntimeException(errors); } PathWrapper best = response.getBest(); return new RoutingResult(asPositions(best.getPoints()), new DistanceAndTime(best.getDistance(), best.getTime() / 1000), true); } finally { long end = currentTimeMillis(); log.info("GraphHopper: routing from " + from + " to " + to + " took " + (end - start) + " milliseconds"); } }
System.out.println("\texport results to:" + outFile); PathWrapper pathWrapper = new PathWrapper(); new PathMerger().doWork(pathWrapper, Collections.singletonList(mr.getMergedPath()), tr); try (BufferedWriter writer = new BufferedWriter(new FileWriter(outFile))) { time = measurements.get(0).getTime(); writer.append(pathWrapper.getInstructions().createGPX(gpx.trk.get(0).name != null ? gpx.trk.get(0).name : "", time, hopper.hasElevation(), withRoute, true, false, Constants.VERSION));
setDouglasPeucker(peucker). setSimplifyResponse(minPathPrecision > 0); PathWrapper pathWrapper = new PathWrapper(); pathMerger.doWork(pathWrapper, Collections.singletonList(matchResult.getMergedPath()), tr); pathWrapper.getErrors().clear(); GHResponse rsp = new GHResponse(); rsp.add(pathWrapper); time = measurements.get(0).getTime(); return Response.ok(rsp.getBest().getInstructions().createGPX(gpx.trk.get(0).name != null ? gpx.trk.get(0).name : "", time, enableElevation, withRoute, withTrack, false, Constants.VERSION), "application/gpx+xml"). header("X-GH-Took", "" + Math.round(took * 1000)). build();
private PathLayer createPathLayer(PathWrapper response) { Style style = Style.builder() .fixed(true) .generalization(Style.GENERALIZATION_SMALL) .strokeColor(0x9900cc33) .strokeWidth(4 * getResources().getDisplayMetrics().density) .build(); PathLayer pathLayer = new PathLayer(mapView.map(), style); List<GeoPoint> geoPoints = new ArrayList<>(); PointList pointList = response.getPoints(); for (int i = 0; i < pointList.getSize(); i++) geoPoints.add(new GeoPoint(pointList.getLatitude(i), pointList.getLongitude(i))); pathLayer.setPoints(geoPoints); return pathLayer; }
/** * This method returns true if one of the paths has an error or if the response itself is * erroneous. */ public boolean hasErrors() { if (!errors.isEmpty()) return true; for (PathWrapper ar : pathWrappers) { if (ar.hasErrors()) return true; } return false; }
/** * This method returns all the explicitly added errors and the errors of all paths. */ public List<Throwable> getErrors() { List<Throwable> list = new ArrayList<>(); list.addAll(errors); for (PathWrapper ar : pathWrappers) { list.addAll(ar.getErrors()); } return list; }
public String getDebugInfo() { String str = debugInfo; for (PathWrapper ar : pathWrappers) { if (!str.isEmpty()) str += "; "; str += ar.getDebugInfo(); } return str; }
private static Response.ResponseBuilder gpxSuccessResponseBuilder(GHResponse ghRsp, String timeString, String trackName, boolean enableElevation, boolean withRoute, boolean withTrack, boolean withWayPoints, String version) { if (ghRsp.getAll().size() > 1) { throw new IllegalArgumentException("Alternatives are currently not yet supported for GPX"); } long time = timeString != null ? Long.parseLong(timeString) : System.currentTimeMillis(); return Response.ok(ghRsp.getBest().getInstructions().createGPX(trackName, time, enableElevation, withRoute, withTrack, withWayPoints, version), "application/gpx+xml"). header("Content-Disposition", "attachment;filename=" + "GraphHopper.gpx"); }