Open source navigation with OpenTripPlanner

3 minute read

Published:


OpenTripPlanner-logo

OpenTripPlanner (OTP) is an open source multi-modal (i.e. walk/car/bicycle/transit/..) trip planner engine written in Java with an easy-to-use executable. It has amazing capabilities in terms of consuming the data from OpenStreetMap (OSM), General Transit Feed Specification (GTFS) as well as General Bikeshare Feed Specification (GBFS) feed. It comes with quite good documentation and a continously growing community.

Getting started

For me, the easiest way to start experimenting is with docker, thus, avoiding the headache to install/compile dependent libraries. I forked the a docker-otp image for OTP and added few more documentation to it and a newer version is available on my github which needs to build and is also adjusted for newer version of the OTP. After cloning the repo, one can quickly build the graphs as per the documentation. To build a graph, one needs the OSM data, which can be downloaded from geofabrik and for a small memory machine, it is a good idea to crop the region to the ROI (maybe a city). Instruction to do so can be found in a previous post. Additionally, one would need the GTFS feed, if you want to use the public transport data. For some big cities, this kind of data is opensourced and a zip file can be easily downloaded. Please note, if either on of data is not available then it will affect only that transport mode (OSM for bicycle/walk/car and GTFS for public transport), thereby, we can build the graph with the limited data only. Reiterating from the documentation, we need to place OSM and GTFS data to docker-otp/graphs/city_name, then to build the graph

docker run -v $PWD:/graphs -e JAVA_OPTIONS=-Xmx4G your_docker_id/otp --build /graphs

After building the graph, we can easily turn on the server:

docker run -p 8080:8080 -v $PWD:/var/otp/graphs -e JAVA_OPTIONS=-Xmx4G otp --server --autoScan --verbose

This will start a Java based navigation service and can be viewed with localhost:8080 in the browser. This service would also enable us to query the data via its REST API. E.g.

http://localhost:8080/otp/routers/default/plan?fromPlace=28.658420, 77.230757&toPlace=28.544442, 77.206334&mode=TRANSIT,WALK

However, the response would be xml. To have a json response, we need to pass the header, for e.g., in python, we can pass the following header in our request: headers={"Accept-type": "application/json"} With this basic setup, one can build a service or an application. Checkout this notebook for a ready-to-use solution available in rasta for OTP.

Taking a step

Adding bike rental

With the smart mobility, many cities are promoting bike rentals and scooters for the last mile connectivity. This could be integrated within the OTP quity easily. To use it without any coding in the source code, we need to have the access to the real-time GBFS feed (real-time because bike availability will be keep changing at all stations). A list of availavle GBFS feed: https://github.com/NABSA/gbfs/blob/master/systems.csv

After having access to the right GBFS feed for the city, you need to add this in the configuration file (named router-config.json) of that city (docker-otp/graphs/name_of_city folder) and it’s good idea to rebuild the graph. Following is a sample router-config.json file:

// router-config.json
{
    // Routing defaults are any public field or setter in the Java class
    // org.opentripplanner.routing.core.RoutingRequest
    "routingDefaults": {
        "numItineraries": 6,
        "walkSpeed": 2.0,
        "stairsReluctance": 4.0,
        "carDropoffTime": 240
    },

    {
        "requestLogFile": "/var/otp/graphs/request.log"
    }

    "updaters": [

        // Bike parking availability
        {
            "type": "bike-park"
        },

        {
             "type": "bike-rental",
             "frequencySec": 60,
             "sourceType": "gbfs",
             "url": "https://gbfs.nextbike.net/maps/gbfs/v1/nextbike_dd/de/"
        }

    ]
}

This file will overwrite the default settings. For a detailed list of the keywords, refer to the official document.

I will try to document a more deep dive by updating this post in future.