使用GraphHopper查找路线上的点



我使用GraphHopper来查找点之间的路线。如果车辆以x的平均速度行驶,我想预测车辆在给定时间t处的位置。GraphHopper有一个用于查找等时线的模块,但我不知道如何在单个路由上运行它。下面是我目前使用的代码

List<GHPoint> points = new ArrayList<>();
points.add(origin);
for (GHPoint pnt : waypoints) {
points.add(pnt);
}
points.add(destination);
GHRequest req = new GHRequest(points).
setWeighting("shortest").
setVehicle("car").              
setLocale(Locale.US);
GHResponse rsp = graphHopper.route(req);
// first check for errors
if(rsp.hasErrors()) {
// handle them!
// rsp.getErrors()
List<Throwable> errors = rsp.getErrors();
return null;
}
PathWrapper bestPath = rsp.getBest();

要解决您的问题,您可以使用位于此处的Isochrone RequestAPI。

为了预测Point B您的车辆可能在哪里,我们需要提供下一个参数:

  1. point-指定起始坐标,required参数
  2. time_limit-指定车辆应行驶的时间。以秒为单位。这里有魔法,在这里提供t参数
  3. vehicle-应为其计算路线的车辆配置文件。默认为car
  4. distance_limit-指定车辆应行驶的距离。以米为单位。您可以使用(txv(公式计算它,因为您指定了车辆以平均速度行驶

就是这样。GraphHopperAPI以GeoJson格式返回多边形列表。

示例:

int t = 600; // in seconds
int v = 10;  // in meters per second
int s = t * v; // in meters
IsochroneApi isochrone = new IsochroneApi();
isochrone.setApiClient(GHApiUtil.createClient());
try {
// Please note: the request string for the point has the order "lat,lon" but the response contains
// an array with the order [lon,lat]
IsochroneResponse rsp = isochrone.getIsochrone("51.183728,14.42801", t, s, VehicleProfileId.CAR,
3, false, "fastest");
final IsochroneResponsePolygon isochrone0 = rsp.getPolygons().get(0);
List<List<BigDecimal>> exteriorRing = isochrone0.getGeometry().getCoordinates().get(0);
System.out.println(exteriorRing);
double lon0 = ((Number) exteriorRing.get(0).get(0)).doubleValue();
double lat0 = ((Number) exteriorRing.get(0).get(1)).doubleValue();
System.out.println("first coord " + lat0 + ", " + lon0);
} catch (Exception ex) {
throw new RuntimeException(ex);
}

感谢@Jamie,我通过修剪LineString中的坐标列表得到了插值点。您可以使用getPoints((.toLineString方法获取LineString,即bestPath.getPoints((.to LineString(false(.

是这样计算距离的

distance = (avgSpeedKMPerHr/ 3.6 ) * timeSec;

以下是使用的功能

//distacne in meters which was calucletd 
public Point interpolatePointAlogLine(LineString line,double distance) throws 
NoSuchAuthorityCodeException, FactoryException
{       
GeodeticCalculator calculator = new GeodeticCalculator(CRS.decode("EPSG:4326")); 
GeometryFactory geometryFactory = new GeometryFactory(new PrecisionModel(PrecisionModel.FLOATING), 4326);
List<Coordinate> coordinates = new ArrayList<Coordinate>();
Collections.addAll(coordinates, line.getCoordinates());
double accumulatedLength = 0;
if(distance >= line.getLength())
return geometryFactory.createPoint(line.getEndPoint());
for (int i = 0; i < coordinates.size(); i++)
{
Coordinate c1 = coordinates.get(i);
Coordinate c2 = coordinates.get(i + 1);
calculator.setStartingGeographicPoint(c1.x, c1.y);
calculator.setDestinationGeographicPoint(c2.x, c2.y);
double length = calculator.getOrthodromicDistance();
if (length + accumulatedLength >= distance) 
{
double offsetLength = distance - accumulatedLength;
double ratio = offsetLength / length;
double dx = c2.x - c1.x;
double dy = c2.y - c1.y;
Coordinate iPoint = new Coordinate(c1.x + (dx * ratio), 
c1.y + (dy * ratio));
return geometryFactory.createPoint(iPoint));
}
else {
accumulatedLength += length;
}
}
return null;
}

相关内容

  • 没有找到相关文章

最新更新