derhuerst / berlin-gtfs-rt-server

Expose Berlin & Brandenburg transit data as a GTFS-RT feed.
https://v0.berlin-gtfs-rt.transport.rest/
Other
13 stars 1 forks source link
berlin gtfs gtfs-realtime gtfs-rt public-transport transit

berlin-gtfs-rt-server

Poll the VBB HAFAS endpoint to provide a GTFS Realtime (GTFS-RT) feed for Berlin & Brandenburg.

Prosperity/Apache license support me via GitHub Sponsors chat with me on Twitter

This project uses hafas-client & hafas-gtfs-rt-feed to fetch live data about all vehicles in the Berlin & Brandenburg area and build a live GTFS Realtime (GTFS-RT) feed from them.

Installing & running

Note: hafas-gtfs-rt-feed, the library used by this project for building the GTFS-RT feed, has more extensive docs. For brevity and to avoid duplication (with e.g. hamburg-gtfs-rt-server), the following instructions just cover the basics.

Prerequisites

berlin-gtfs-rt-server needs access to a Redis server, you can configure a custom host/port by setting the REDIS_URL environment variable.

It also needs access to a PostgreSQL 14+ server with PostGIS installed; Pass custom PG* environment variables if you run PostgreSQL in an unusual configuration.

It also needs access to a NATS Streaming server (just follow its installation guide); Set the NATS_STREAMING_URL environment variable if you run it in an unusual configuration.

It also needs the sponge command from the moreutils package to be installed.

The start.sh script requires at least Bash 5.0 to run (because it uses 5.0); macOS currently bundles Bash 3.2, so use brew install bash to install an up-to-date version.

Optional: dynamic local addresses from a range

As an optional feature, when polling the HAFAS API, berlin-gtfs-rt-server uses IP addresses from a pool defined in $RANDOM_LOCAL_ADDRESSES_PREFIX as local addresses. localaddress-agent is used for this, which currently only supports Linux.

localaddress-agent is defined as an optional dependency, so its installation will be attempted, but a failure won't cause the whole npm install command to fail. By setting the $RANDOM_LOCAL_ADDRESSES_PREFIX environment variable, you make its installation mandatory.

Refer to localaddress-agent's installation instructions for its required dependencies.

Cloning

git clone https://github.com/derhuerst/berlin-gtfs-rt-server.git
cd berlin-gtfs-rt-server
npm install

Building the matching index

npm run build

The build script will download the latest VBB GTFS Static data and import it into PostgreSQL. Then, it will add additional lookup tables to match realtime data with GTFS Static data. psql will need to have access to your database.

Running

Specify the bounding box to be observed as JSON:

export BBOX='{"north": 52.52, "west": 13.36, "south": 52.5, "east": 13.39}'

berlin-gtfs-rt-server uses hafas-gtfs-rt-feed underneath, which is split into three parts: polling the HAFAS endpoint (monitor-hafas CLI), matching realtime data (match-with-gtf CLI), and serving a GTFS-RT feed (serve-as-gtfs-rt CLI). You can run all three at once using the start.sh wrapper script:

./start.sh

In production, run all three using a tool that restarts them when they crash, e.g. systemctl, forever or Kubernetes.

via Docker

A Docker image is available as derhuerst/berlin-gtfs-rt-server.

Note: The Docker image does not contain Redis, PostgreSQL & NATS. You need to configure access to them using the environment variables documented above (e.g. NATS_STREAMING_URL).

export BBOX='{"north": 52.52, "west": 13.36, "south": 52.5, "east": 13.39}'
# build the matching index
docker run -e BBOX -i -t --rm derhuerst/berlin-gtfs-rt-server ./build.sh
# run
docker run -e BBOX -i -t --rm derhuerst/berlin-gtfs-rt-server

via docker-compose

The example docker-compose.yml starts up a complete set of containers (berlin-gtfs-rt-server, Redis, PostGIS/PostgreSQL, NATS Streaming) to generate a GTFS-RT feed

Be sure to set POSTGRES_PASSWORD, either via a .env file or an environment variable.

The environment may be started via

POSTGRES_PASSWORD=mySecretPassword docker-compose up -d

After starting, the GTFS-RT feed should be available via http://localhost:3000/.

inspecting the feed

Check out hafas-gtfs-rt-feed's inspecting the feed section.

metrics

Check out hafas-gtfs-rt-feed's metrics section.

License

This project is dual-licensed: My contributions are licensed under the Prosperity Public License, contributions of other people are licensed as Apache 2.0.

This license allows you to use and share this software for noncommercial purposes for free and to try this software for commercial purposes for thirty days.

Personal use for research, experiment, and testing for the benefit of public knowledge, personal study, private entertainment, hobby projects, amateur pursuits, or religious observance, without any anticipated commercial application, doesn’t count as use for a commercial purpose.

Buy a commercial license or read more about why I sell private licenses for my projects.