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