An open API service indexing awesome lists of open source software.

https://github.com/josericodata/dublinbusapp

A real-time Dublin Bus tracking app built with Streamlit, leveraging live API data to visualise bus routes, stops, and schedules.
https://github.com/josericodata/dublinbusapp

api bus bus-tracker-api bus-tracking-app dublin dublin-bus dublin-ireland gtfs gtfs-realtime ireland json json-schema linkedin-profile linux portfolio python rest-api streamlit transport-for-ireland ubuntu

Last synced: 2 months ago
JSON representation

A real-time Dublin Bus tracking app built with Streamlit, leveraging live API data to visualise bus routes, stops, and schedules.

Awesome Lists containing this project

README

        

# ๐ŸšŒ Dublin Bus App

**The Dublin Bus App has been built in an attempt to use real time data using the API's provided by the [National Transport Authority](https://developer.nationaltransport.ie/). Using this app you can see the incoming buses for a selected stop and the buses that currently operate a route based on a direction.**

---

## ๐Ÿงฌ **Project Structure**
```bash
DublinBusApp
โ”œโ”€โ”€ assets/
โ”‚ โ”œโ”€โ”€ data/
โ”‚ โ”‚ โ”œโ”€โ”€ Routes.txt
โ”‚ โ”‚ โ”œโ”€โ”€ StopMapLocation.txt
โ”‚ โ”‚ โ”œโ”€โ”€ StopTimesPerTrip.txt
โ”‚ โ”‚ โ””โ”€โ”€ Towards.csv
โ”‚ โ”œโ”€โ”€ dataCleaning/
โ”‚ โ”‚ โ”œโ”€โ”€ 00_EndpointExploration.ipynb
โ”‚ โ”‚ โ”œโ”€โ”€ 01_DataCleaning.ipynb
โ”‚ โ”‚ โ”œโ”€โ”€ 02_Flow_01_โŒš_StopTimes.ipynb
โ”‚ โ”‚ โ”œโ”€โ”€ 03_Flow_02_๐Ÿ“_BusLocator.ipynb
โ”‚ โ”‚ โ”œโ”€โ”€ routes.txt
โ”‚ โ”‚ โ”œโ”€โ”€ stop_times.txt
โ”‚ โ”‚ โ”œโ”€โ”€ stops.txt
โ”‚ โ”‚ โ””โ”€โ”€ trips.txt
โ”‚ โ”œโ”€โ”€ gifs/
โ”‚ โ”‚ โ”œโ”€โ”€ BusLocator.gif
โ”‚ โ”‚ โ””โ”€โ”€ StopTimes.gif
โ”‚ โ””โ”€โ”€ images/
โ”‚ โ”œโ”€โ”€ bus.png
โ”‚ โ”œโ”€โ”€ dublin_bus_favicon.png
โ”‚ โ”œโ”€โ”€ dublin_bus_logo.png
โ”‚ โ”œโ”€โ”€ pin_blue.png
โ”‚ โ”œโ”€โ”€ pin_green.png
โ”‚ โ”œโ”€โ”€ pin_red.png
โ”‚ โ””โ”€โ”€ tfi_logo.png
โ”œโ”€โ”€ streamlit_app/
โ”‚ โ”œโ”€โ”€ modules/
โ”‚ โ”‚ โ”œโ”€โ”€ __init__.py
โ”‚ โ”‚ โ”œโ”€โ”€ BLutils.py
โ”‚ โ”‚ โ”œโ”€โ”€ DataPrep.py
โ”‚ โ”‚ โ”œโ”€โ”€ STutils.py
โ”‚ โ”‚ โ””โ”€โ”€ Styles.py
โ”‚ โ”œโ”€โ”€ pages/
โ”‚ โ”‚ โ”œโ”€โ”€ 01_โŒš_StopTimes.py
โ”‚ โ”‚ โ””โ”€โ”€ 02_๐Ÿ“_BusLocator.py
โ”‚ โ””โ”€โ”€ 00_๐ŸšŒ_Info.py
โ”œโ”€โ”€ LICENSE
โ”œโ”€โ”€ README.md
โ””โ”€โ”€ requirements.txt
```

---

## ๐ŸŽฏ **Road Map**

Plan to develop the app:

- **Understand the data provided by the APIs**

- **Explore the static data provided by the NTA to complement data shown in the APIs**

- **Design the logic for `StopTimes` and `BusLocator` pages**

---

## ๐Ÿ—๏ธ **Deployment**

1. **Understand the data provided by the APIs**:

- First, sign up for the [National Transport Authority (NTA)](https://developer.nationaltransport.ie/) to access their APIs. Use the following link to sign up: [Sign up here](https://developer.nationaltransport.ie/signup).

- Next, understand and visualise the data provided by the following endpoints:

- [GTFSR Endpoint](https://api.nationaltransport.ie/gtfsr/v2/gtfsr?format=json)

- [Vehicles Endpoint](https://api.nationaltransport.ie/gtfsr/v2/Vehicles?format=json)

- To assist with exploring these endpoints, I have created a Jupyter Notebook (JN): [00_EndpointExploration.ipynb](https://github.com/josericodata/DublinBusApp/blob/main/assets/dataCleaning/00_EndpointExploration.ipynb). Replace `os.environ.get("API_KEY")` with the variable `YOUR_API_KEY`, and you're ready to proceed.

2. **Exploring and Preparing NTA Dublin Bus Static Data**:

- Please head over to the following link: [GTFS Dublin Bus Data](https://www.transportforireland.ie/transitData/Data/GTFS_Dublin_Bus.zip). This will download the file `GTFS_Dublin_Bus.zip`. After downloading, extract the files.

- We will need the following files from the extracted data:

1. `routes.txt`

2. `stops.txt`

3. `stop_times.txt`

4. `trips.txt`

- These files will be used to generate the following new files:

1. `Routes.txt`

2. `StopMapLocation.txt`

3. `StopTimesPerTrip.txt`

4. `Towards.txt`

- To visualise this step, I have created a JN: [01_DataCleaning.ipynb](https://github.com/josericodata/DublinBusApp/blob/main/assets/dataCleaning/01_DataCleaning.ipynb).

- **The files mentioned above were used in the early stages of this project. However, since the NTA updates `GTFS_Dublin_Bus.zip` almost every week, maintaining the app required frequent updates. To simplify this process, I created [DataPrep.py](https://github.com/josericodata/DublinBusApp/blob/main/streamlit_app/modules/DataPrep.py). With this script, every time the app runs, it fetches fresh data from [GTFS Dublin Bus Data](https://www.transportforireland.ie/transitData/Data/GTFS_Dublin_Bus.zip). I am keeping the old `.txt` files in case they help someone better understand this complex process.**

3. **What is GTFS?**

- Summarising point 1 and 2, the **General Transit Feed Specification (GTFS)** is a standardised format for public transportation data, allowing transit agencies to share schedules, routes, and real-time updates. It consists of structured text files that define agencies, stops, routes, trips, and schedules.

๐Ÿ”น **GTFS Data Types:**

| Type | Description |
|----------------|------------|
| **GTFS Realtime** | Delivers live updates on vehicle positions, delays, and service alerts. Uses `GTFSR` and `Vehicles` endpoints. |
| **GTFS Static** | Provides fixed schedules, routes, and stop locations. Uses `GTFS_Dublin_Bus.zip`. |

- For more details, visit the official GTFS website: [GTFS.org](https://gtfs.org/)

4. **Logic Design for `StopTimes` and `BusLocator` Pages**:

- After understanding the data provided by the endpoints and the static files, it is time to develop the logic for the pages.

- **`StopTimes` Page**:

- The user will first select a `route`, then be prompted to choose a `direction`, and finally select a `stop` to display the buses that are due to arrive.

- For a clear understanding of this flow, refer to the JN: [02_Flow_01_โŒš_StopTimes.ipynb](https://github.com/josericodata/DublinBusApp/blob/main/assets/dataCleaning/02_Flow_01_โŒš_StopTimes.ipynb).

- **`BusLocator` Page**:

- This page will show the operating buses for the selected `route` and `direction`.

- For more details on this logic, refer to the JN: [03_Flow_02_๐Ÿ“_BusLocator.ipynb](https://github.com/josericodata/DublinBusApp/blob/main/assets/dataCleaning/03_Flow_02_๐Ÿ“_BusLocator.ipynb).

---

5. **Streamlit App Creation**:

- After completing the steps above, the final app can be found at: [streamlit_app](https://github.com/josericodata/DublinBusApp/tree/main/streamlit_app).

## ๐Ÿš€ **Getting Started**

### **Local Installation**

1. Clone the repository:

```bash

git clone https://github.com/user/DublinBusApp.git

```

**Hint:** Replace `user` with `josericodata` in the URL above. I am deliberately asking you to pause here so you can support my work. If you appreciate it, please consider giving the repository a star or forking it. Your support means a lotโ€”thank you! ๐Ÿ˜Š

2. Navigate to the project directory:

```bash

cd DublinBusApp

```

3. Create a virtual environment:

```bash

python3 -m venv venvDublinBus

```

4. Activate the virtual environment:

```bash

source venvDublinBus/bin/activate

```

5. Install requirements:

```bash

pip install -r requirements.txt

```

6. Navigate to the modules directory:

```bash

cd streamlit_app/modules

```

7. Please locate in `BLutils.py` and `STutils.py` the following line code:

```bash

YOUR_API_KEY = st.secrets["API_KEY"]

```

It is crutial to replace `st.secrets["API_KEY"]` with the actual API Key provided by the [National Transport Authority](https://developer.nationaltransport.ie/) after [signing up](https://developer.nationaltransport.ie/signup) to acces their APIs. Please use the following link to sign up: https://developer.nationaltransport.ie/signup


8. Go back one directory and run the app:

```bash

cd ..

```

Then run

```bash

streamlit run 00_๐ŸšŒ_Info.py

```

The app will be live at `http://localhost:8501`.

---

## ๐ŸŽฌ **Demo**

### Page 1: Stop Times
![Stop Times Demo](assets/gifs/StopTimes.gif)

### Page 2: Bus Locator
![Bus Locator Demo](assets/gifs/BusLocator.gif)

---

### โ–ถ๏ธ Watch the YouTube Tutorial

[![Streamlit Bus Tracking App with GTFS, Folium & Python](https://img.youtube.com/vi/PaU5KaZCJ5E/maxresdefault.jpg)](https://www.youtube.com/watch?v=PaU5KaZCJ5E "Click to play")

Click the image above or [here](https://www.youtube.com/watch?v=PaU5KaZCJ5E) to watch the video on YouTube.

---

## ๐Ÿ”ฎ **Future Enhancements**

Planned improvements for the Dublin Bus App include:

- **Database Integration**: Implementing a relational database (e.g., PostgreSQL) to better manage and query data, enabling faster performance and enhanced data relationships between routes, stops, and trips.

- **Historical Data Storage**: Storing historical bus data to provide insights like delay trends, busiest stops, and more accurate arrival time predictions using machine learning.

- **Personalised Features**: Adding user-specific data storage, such as favorite routes or frequently accessed stops, for a more tailored experience.

- **Advanced Reporting and Analytics**: Generating detailed reports, such as route popularity or stop traffic trends, with visualisations like heatmaps or charts.

- **Enhanced Pages**: Introducing new pages and features, including route performance tracking, user feedback collection, and real-time bus location improvements.

- **Scalability**: Preparing the app for multi-user support by efficiently handling concurrent queries through the database.

---

## ๐ŸŽ“ **Motivation**

- The motivation behind developing this app is to gain hands-on experience in working with JSON-formatted data from real-time APIs. As a resident of Ireland, I found Dublin Bus data particularly convenient, as I am familiar with many of the routes, buses, and stops.

- Additionally, this project helps me stay up to date with my skills while serving as a valuable addition to my portfolio, showcasing my abilities in handling and processing data effectively.

---

## ๐Ÿ”ง **Environment Setup**

This app has been built and tested in the following environment:

- **Operating System**: Ubuntu 22.04.5 LTS (Jammy)

- **Python Version**: Python 3.10.12

---

## ๐Ÿค **Open Pull Requests**

If you find any bug, feel free to contact me by opening a pull request on GitHub or via email at **[email protected]**.

---

## โš ๏ธ **Disclaimer**

This app was developed for my portfolio and demonstration purposes only. The results are not guaranteed to be error-free and should not be used for critical decision-making.



Dublin Bus Logo



TFI Logo