Tracking Oil on Water
Tracking Oil on Water
Here on Canada’s west coast, we worry about the shipping routes for the product of Canada’s tar sands. Oil from the tar sands is loaded onto tankers in our ports and shipped out to California or China -- passing through dangerous channels, and vital, sensitive habitats for whales and other sea mammals and fish.
ForestEthics envisioned a system for tracking ships through these key regions surrounding Vancouver, Victoria, and Puget Sound, place those oil tankers on a map in real time, and send notifications out via twitter when the ships pass through different areas. They came to Affinity Bridge with the challenge: can you build this?
A new kind of project
Affinity Bridge is known for designing and building effective websites in Drupal for our clients. But this project provided something different, and interesting, for the team to tackle. The Tanker Tracker Map Application on Tar Sands SOS was not something that could be built in Drupal.
It's a puzzle
The AIS tracking system, originally developed as a collision avoidance tool for commercial vessels, is at the core of the process. Commercial vessels are equipped with radio transmitters that emit their position, speed, identity and course, so that other vessels are aware of them and collisions can be avoided. This information is free and open to anyone who is willing to run their own radio receiver and share their feed through the AIS Hub. Basically, if you set up your own AIS radio receiver and send your data to the Hub, you are given access to all the data shared by other AIS Hub members worldwide. ForestEthics purchased a receiver in order to become part of the network -- and to gain access to that raw data.
Data from the AIS tracking system is transmitted in a binary format consisting of 25 or more different types of messages. We needed to pare down the data so we could work with what’s most important to this project: position information (emitted every 30 seconds to 2 minutes depending on whether or not the ship is moving), and static vessel and voyage data -- the name of the ship, the size, and type. What we needed to define was where the ship was in relation to prescribed zones, called geofences, and whether or not that ship was carrying tar sands oil.
Putting the pieces together
The application was built with ZeroMQ, Python, and Node.js. ZeroMQ is an API-based messaging library that allows any kind of communication between applications -- it acts as a kind of translator between applications and languages that can’t speak directly to each other. It manages this process while still maximizing performance, thus keeping a complicated system running quickly and smoothly.
With this strong communication and messaging system working as the backbone, the application was built using several independent Python scripts that all communicate with a message broker script.
The raw data comes from AISHub, and goes through GPSD -- a commonly used application developed to communicate with all kinds of GPS devices, including AIS receivers.
Next, a Python script we call the GPSD Bridge looks at messages coming through the GPSD and publishes the relevant information to the Message Broker. The Message Broker can then take that information and publish it using other Python scripts. One of these looks for the static vessel ship information messages and keeps track of every unique vessel, as well as their current positions, and publishes that via the message broker.
Another script, the notifier, monitors the messaging broker for ship updates that we want to emit notifications for -- in other words, it can send out a tweet or text message when a tanker goes into a sensitive region like an animal habitat.
We use Redis -- a high performance data storage system -- to store information from every relevant ship that a message has been received from, and to track all position information of ships that fall under a certain criteria within that list of ships. Specifically, ForestEthics wants to know which Tankers have visited Westridge Marine Terminal, and where they went next. Do they pass through a whale habitat? Are they traveling through environmentally sensitive areas?
And it all comes together on the map
We put all the pieces together using the combined strengths of node.js and Python. We have a node.js application subscribing to the ship updates from the broker, forwarding those updates via websockets to client browsers, and then plotting the locations on a public-facing map. Because of its event-based architecture, it reacts when something specific happens, triggering tasks or functions. This event-base architecture allows the map to update itself without being refreshed, so users can watch the map and see the tankers moving in near real-time.
The benefits to developing the system using standalone scripts and putting it all together through ZeroMQ are twofold: first, it means that if any one of these pieces doesn’t work, the rest of the application carries on doing what it is meant to do -- it is stable. Second, it makes it easy to add new components in the future.
What does this mean for Affinity Bridge? Well, we like to build websites, it’s true, but projects like the Tanker Tracker map are showing what’s possible when you start to think about new ways to use the tools that technology provides. It’s not always easy to make things work exactly the way it’s supposed to, but figuring out how to solve those complicated problems is a challenge that we like to take on. We’re looking forward to tackling more intriguing projects and discovering new problems to resolve.