1

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?

3
  • As it stands I think your question is too broad because you have only told us what you want to do and alluded to some things that you have tried. I think you should include a code snippet that illustrates precisely what you have tried and where you are stuck.
    – PolyGeo
    Commented Oct 7, 2016 at 2:27
  • @PolyGeo regarding the code snippet, I followed the exact steps at github.com/opentripplanner/OpenTripPlanner/blob/master/src/test/…. You can see that code is pretty long so not pasting it in the question. I have highlighted the line where I am stuck.
    – morpheus
    Commented Oct 7, 2016 at 2:39
  • 1
    Long code should not be posted in a question here. That is why we ask you to develop from your code a code snippet that illustrates precisely what you have tried and where you are stuck. There may be potential answerers here who will vote to re-open your question and be happy to follow links to find code to debug but I am not one of them.
    – PolyGeo
    Commented Oct 7, 2016 at 3:10

1 Answer 1

2

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]);
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.