Connected vehicle data meets python: building a mapping insights application with Streamlit, Pandas, and Pydeck

Connected vehicle data meets python: building a mapping insights application with Streamlit, Pandas, and Pydeck

Connected vehicles generate vast amounts of data, from location and speed to driving behavior and vehicle performance. This data presents opportunities to gain insights into traffic patterns, optimize route planning, and improve road safety. However, without the right tools and technologies, it can be difficult to process and analyze this big data effectively. 

  

Intending to demonstrate the potential of this data, mobway presents the development of an application with open-source tools for interactive visualization of geolocation data. Imagine being able to identify a huge urban center's parking usage pattern and strategically plan infrastructure improvements. Or, set targeted billboard advertising based on the stratification of the driver's profile and its location. The tool we will be developing in this post utilizing data from the mobway’s platform will have components that, when utilized specifically, may offer these mapping insights. 


In this article, we'll be using several popular Python open-source libraries for various data-related tasks:

  • Pandas: a library for data manipulation and analysis, particularly for tabular data. We'll use Pandas to read our connected vehicle data from a CSV file and perform some initial processing. 
  • Pydeck: a WebGL-powered visualization library that allows us to create interactive maps and other 3D visualizations. We'll use Pydeck to render our connected vehicle data on a map. 
  • Streamlit: a library for building web applications quickly and easily. We'll use Streamlit to create an interface for our connected vehicle data that users can interact with.

 

pip install pandas, pydeck, streamlit         


The first step in our project is to import our connected vehicle data into Python. We'll be using a CSV file that contains location and speed data for a fleet of vehicles. To import this data, we'll use Pandas' read_csv function. This will create a Pandas DataFrame object containing our connected vehicle data, which we can then manipulate and analyze as needed. 


 ./main.py

import pandas as pd  

def get_data(): 

    data = pd.read_csv('mobway_connected_vehicle_map.csv') 

    return data          


As previously mentioned, the vehicles generate vast amounts of data (more than 200 parameters) that are continuously monitored, and, for this example, we will gather four important anchors from mobway’s dataset: the position, including latitude and longitude, the timestamp for this record and the speed as you can see below. It's worth noting that the dataset provided by mobway has already been curated and follows a unique data standard, ensuring that the most important parameters are captured accurately. 


 ./mobway_connected_vehicle_data.csv

timestamp, lat, lon, speed 
2022-09-07 14:21:41.116, -19.650, -43.941, 40.30          


Also, mobway’s API allows custom selection of various parameters that may be referenced to these anchor columns, including battery status, seat belt and doors states, and many others. With this clean and organized data, we can easily import it into Python using Pandas and start analyzing it to gain valuable insights. 


example 

filtered_data = data[data['SPEED'] >= 100]         


Reach out to us at mobway.cloud to receive a sample connected vehicle dataset.


With our data imported, we can now use Pydeck to render our data on a map. We'll create a Pydeck Layer object that will display our vehicle data as points on a map, with the color of each point indicating the vehicle's speed: 


 ./main.py 

def render_map(data):

    layer = pdk.Layer(  
        'ScatterplotLayer',  
        data=data,  
        get_position=['lon', 'lat'],  
        get_fill_color='[speed, 0, 100-speed]',  
        get_radius=50000,  
        pickable=True,  
        auto_highlight=True  
    )  
  
    view_state = pdk.ViewState(  
        latitude=data['lat'].mean(),  
        longitude=data['lon'].mean(),  
        zoom=10,  
        bearing=0,  
        pitch=0  
    )  

    mobway_connected_vehicle_map = pdk.Deck(  
        layers=[layer],  
        initial_view_state=view_state  
    )  

    return mobway_connected_vehicle_map          


This will create an interactive map with our connected vehicle data. Users can zoom in and out, pan around, and click on individual points to see more information about each vehicle speed. 


Now that we have loaded our data and created a visualization, it is time to build an interface that allows users to interact with the data. We will use Streamlit, a Python library for building web applications, to create an interactive dashboard. 

  

First, let's create a sidebar with filters that allow users to select which data they want to see on the map. We can create a sidebar using the streamlit.sidebar function and add widgets to it using functions such as slider, selectbox, and multiselect. 


./main.py 

import streamlit as st

# Sidebar Filters 

st.sidebar.markdown("# Filter Data")  

min_speed = st.sidebar.slider("Minimum Speed", 0, 100, 0) 

max_speed = st.sidebar.slider("Maximum Speed", 0, 100, 100)          


In this example, we have created sliders for minimum and maximum speeds. We can use these filters to select a subset of the data to display on the map. Next, we can create the main section of the Streamlit app, which will display the map using the st.pydeck_chart function. We can pass our Pydeck visualization to this function to display the map in the app. 


./main.py 

# Main Section – Map

def main(): 
 
    data = get_data() 
    data = data[(data['speed'] >= min_speed) & (data['speed'] <= max_speed)] 

    mobway_connected_vehicle_map = render_map(data) 
    st.pydeck_chart(mobway_connected_vehicle_map) 

if __name__ == "__main__": 
    main()          


In this code, we are using the pdk.Deck function to create a Pydeck map and passing it to the st.pydeck_chart function to display it in the app. We are also using our filters to select a subset of the data to display on the map. 

 

Once this all has been done, you can launch your app by running the following command in your terminal or command prompt: 


cmd

streamlit run main.py         


This will launch your Streamlit app, and you will be able to view it in your web browser at the specified local address (usually http://localhost:8501/). As you make changes to your code and save the file, the app will automatically update in your browser, allowing you to see the effects of your changes in real-time.

 

Não foi fornecido texto alternativo para esta imagem
mobway connected vehicle map


Now that we have built our Streamlit app, we can deploy it using Streamlit Cloud, a platform for deploying and sharing Streamlit apps. To deploy our app to Streamlit Cloud, we will first need to create an account on the Streamlit Cloud website. 

  

Once we have an account, we can create a new app by clicking the "New App" button and following the instructions. We will need to connect our app to a GitHub repository that contains our code, and set up some configuration options for the app. After our app is set up, we can deploy it to Streamlit Cloud by clicking the "Deploy" button. Our app will then be available to access through a public URL. 


In summary, the power of connected vehicle data cannot be overstated when it comes to unlocking insights into the mobility sector. By leveraging Python and advanced libraries such as Pandas, Pydeck, and Streamlit, we can extract valuable insights from the vast amounts of data generated by vehicles. 

  

By creating an interactive dashboard using these tools, we can easily explore and comprehend connected vehicle data, leading to tangible improvements in transportation systems. This includes optimizing routes, reducing carbon emissions, and improving safety for drivers and passengers alike. 

  

With mobway, your business can request your customer's access to their vehicle data to improve their experience with more connected features. Our API offers standard, enriched and GDPR compliant vehicle data without aftermarket telematics. Learn more at mobway.cloud on how to get started! 

To view or add a comment, sign in

More articles by mobway

Insights from the community

Others also viewed

Explore topics