Search code examples
javagraphhopper

Find point on the route using GraphHopper


I am using GraphHopper for finding a route between points. I want to predict the location of the vehicle at the given time t if it is moving at an average speed of x. GraphHopper has a module for finding isochrone but I can't figure out how I can run it on a single route. Below is the code which I am currently using

    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();

Solution

  • Thanks to @Jamie i got the interpolated point by lopping the coordinate list in LineString. You can get the LineString by using getPoints().toLineString method i.e. bestPath.getPoints().toLineString(false).

    The distance was calculated like this by

       distance = (avgSpeedKMPerHr/ 3.6 ) * timeSec;
    

    Below is the function which is used

      //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;
    }