AWS Public Sector Blog
Using Amazon Timestream and Amazon Location Service to detect transportation route deviations and anomalies
Transit authorities have to maintain the location and schedule of large numbers of vehicle fleets on a daily basis. Most commonly, GPS coordinates are used to track vehicle location and transportation route. GPS coordinates often have anomalies that can contaminate location reporting. Additionally, if a vehicle takes a detour, it will offset public transportation schedules. Both cases impact the riders negatively. Keeping track and getting notified is a challenge.
In this post, we look into an anomaly detection mechanism for public transportation using Amazon Web Services (AWS) offerings. Transit authorities can use this approach to improve their customer experience by notifying riders of bus detours or delays and providing an accurate bus schedule.
Solution overview
In the following architecture, shown in Figure 1, we have three parts: Log ingestion and processing, route or geofence ingestion, and processing and route monitoring.
- Log ingestion and processing – The General Transit Feed Specification (GTFS) data is read from the Amazon Timestream database by an AWS Lambda function for processing and sent to an Amazon Location Service Tracker. The processed data is also stored as a .csv file in Amazon Simple Storage Service (Amazon S3) for future use.
- Route or geofence ingestion and processing –The event driven architecture triggers a Lambda function that curates a shape and trip text file from a GTFS repository once it arrives in an Amazon S3 bucket. After processing is done, it loads the processed route into a geofence collection in Amazon Location.
- Route monitoring – Finally, an Amazon EventBridge rule is created with Amazon CloudWatch as a target, which will publish the entry and exit events from a geofence collection to a target CloudWatch log group.
Log ingestion and processing
The log data from the bus is stored in a Timestream database. Amazon Timestream is a fast, scalable, fully managed, purpose-built time series database that makes it easy to store and analyze trillions of time series data points per day.
Lambda is used to ingest log bus data for processing. Lambda allows us to run code without provisioning or managing servers. The processing involves reading the data from a timestream table and then sending it to the Amazon Location tracker resource.
A tracker resource is an Amazon Location Service resource that receives location updates from devices. The tracker resource provides support for location queries, such as current and historic device location. The functionality will be useful to know which bus goes outside its assigned route.
The function outputs information such as DeviceId
, Position
(in latitude and longitude), and Accuracy
, which is a measure of precision of the position data provided by the device.
Lastly, data from Timestream is unloaded to Amazon S3 as a .csv file, which provides flexibility to store and analyze your time series data for historical purposes. Using the UNLOAD statement, exported query results are stored in a selected Amazon S3 bucket.
Route or geofence ingestion and processing
To create routes, you can use Amazon Location to create a geofence. Geofences are polygon boundaries that you can use to be notified when devices or positions move in or out of the areas. A geofence collection allows you to store and manage geofences.
The Lambda function reads the route files stored in Amazon S3 and extract the coordinates needed to create a geofence.
To transform the straight-line coordinates into a polygon, we use the buffer method in the Shapely Python package. This method creates a buffer area around the line coordinates, which generates a polygon.
After the transformation is done, the Lambda function transmits the polygon coordinates to a geofence collection using the Amazon Location geofences API.
Route monitoring
To begin monitoring routes, we evaluate bus locations against a geofence collection, and we are notified when a tracker crosses the boundaries of any geofences. When this occurs, the geofence collection resource emits events such as ENTER and EXIT on EventBridge, a serverless event bus that connects applications using data from services like Amazon Location. EventBridge receives these geofence events and routes them to targets like CloudWatch, allowing you to set up routing rules to build applications that react to these events.
Conclusion
In conclusion, maintaining accurate vehicle locations and schedules is essential for an efficient public transportation system. This post introduced an architecture for detecting route deviations and anomalies that transit authorities can use to address GPS issues. Ensuring riders receive timely notifications about delays or changes improves the reliability and rider experience of public transportation systems.
To learn more about Amazon Location and best security practices, explore these resources: