[GIS] Sample code to plan a trip using OpenTripPlanner as a jar dependency

javaNetworkopentripplannerroadrouting

I would like to use OpenTripPlanner as a jar dependency in my project and do some routing with it. I have followed the steps at https://github.com/opentripplanner/OpenTripPlanner/blob/master/src/test/java/org/opentripplanner/GtfsTest.java and they do seem to work if the start and end points of a RoutingRequest are near a transit stop but don't work otherwise.
The following line returns me an empty list:

List<GraphPath> paths = new GraphPathFinder(router).getPaths(routingRequest);

I think its because the road network is missing from the graph. Specifically, the code in https://github.com/opentripplanner/OpenTripPlanner/blob/master/src/test/java/org/opentripplanner/GtfsTest.java only adds a transit network to the graph (the GtfsBundle). The road network is never added to the graph. So my question is could anyone please show me how to add the road network to the graph? I have the road network as an OSM PBF file. I seek a complete end-to-end example.

This link is saying at one point:

once the data have been loaded the geometries of the streets are
extra information purely for presentation purposes.

Is the OSM road network not used for routing?

Best Answer

After some searching I found this code to load road network.

Graph gg = new Graph();

        OpenStreetMapModule loader = new OpenStreetMapModule();
        loader.setDefaultWayPropertySetSource(new DefaultWayPropertySetSource());
        FileBasedOpenStreetMapProviderImpl provider = new FileBasedOpenStreetMapProviderImpl(); // this works with XML. For PBF use BinaryFileBasedOpenStreetMapProviderImpl

        File file = new File(URLDecoder.decode(getClass().getResource("map.osm.gz").getFile(), "UTF-8"));

        provider.setPath(file);
        loader.setProvider(provider);

        loader.buildGraph(gg, extra);

But even better was to do following:

put 1_gtfs.zip and seattle_washington.osm.pbf under c:\Users\user\otp and then run

java -Xmx2G -jar target/otp-1.1.0-SNAPSHOT-shaded.jar --build "c:\Users\user\otp" --basePath "c:\Users\user\otp" --verbose

It should generate Graph.obj in same directory. Now you can use following code to do the routing:

Graph graph = Graph.load(file, LoadLevel.DEBUG);
        Router router = new Router("TEST", graph);
        Leg[] legs = planTrip(home, work, graph, true, 1, router);

where the code for planTrip is below

public static Leg[] planTrip(Location start, Location end, Graph graph,
                                boolean preferLeastTransfers,
                                int legCount,
                                Router router)
    {
        long dateTime = System.currentTimeMillis() / 1000l - 24*60*60;
        RoutingRequest routingRequest = new RoutingRequest();
        routingRequest.setNumItineraries(1);
        routingRequest.setArriveBy(dateTime < 0);
        routingRequest.dateTime = Math.abs(dateTime);
        routingRequest.from = new GenericLocation(start.latitude, start.longitude);
        routingRequest.to = new GenericLocation(end.latitude, end.longitude);
        routingRequest.setRoutingContext(graph);
        routingRequest.setWheelchairAccessible(false);
        routingRequest.transferPenalty = (preferLeastTransfers ? 300 : 0);
        routingRequest.setModes(new TraverseModeSet(TraverseMode.WALK, TraverseMode.BUS));        
        routingRequest.setOtherThanPreferredRoutesPenalty(0);
        // The walk board cost is set low because it interferes with test 2c1.
        // As long as boarding has a very low cost, waiting should not be "better" than riding
        // since this makes interlining _worse_ than alighting and re-boarding the same line.
        // TODO rethink whether it makes sense to weight waiting to board _less_ than 1.
        routingRequest.setWaitReluctance(1);
        routingRequest.setWalkBoardCost(30);

        List<GraphPath> paths = new GraphPathFinder(router).getPaths(routingRequest);
        TripPlan tripPlan = GraphPathToTripPlanConverter.generatePlan(paths, routingRequest);
        // Stored in instance field for use in individual tests
        Itinerary itinerary = tripPlan.itinerary.get(0);

        assert itinerary.legs.size() > 0;

        return itinerary.legs.toArray(new Leg[legCount]);
    }