Page cover
For the complete documentation index, see llms.txt. This page is also available as Markdown.

Quick Start

A hands-on quick start guide for using AISdb.

If you are new to AIS topics, click here to learn about the Automatic Identification System (AIS).

If you are starting from scratch, download the example ".db" file from our AISdb Tutorial GitHub repository so you can follow this guide with real data.

Python Environment and Installation

To work with the AISdb Python package, please ensure you have Python version 3.10 or higher. If you plan to use SQLite, no additional installation is required, as it is included with Python by default. Those who prefer using a PostgreSQL server must install it separately and can optionally enable the TimescaleDB extension for better performance on large time-series ingests.

User Installation

The AISdb Python package can be conveniently installed using pip. It's highly recommended that a virtual Python environment be created and the package installed within it.

install.sh
python -m venv .venv   # create a python virtual environment
source ./.venv/bin/activate  # activate the virtual environment
pip install aisdb  # from https://pypi.org/project/aisdb/

PyPI carries the latest stable line of AISdb. The 1.8.0-alpha release that this documentation targets is published on GitHub and installs from source. If you need the features introduced in 1.8.0-alpha, such as weather integration, NOAA CSV ingestion, and TimescaleDB support, follow the Compile AISdb guide or run pip install git+https://github.com/MAPS-Lab/AISdb.git with a Rust toolchain installed. The core workflow shown in this guide works the same on both.

You can test your installation by running the following commands:

python
>>> import aisdb
>>> aisdb.__version__  # '1.8.0a0' when built from source, or the latest PyPI release

If you are running Jupyter, ensure it is installed in the same environment as AISdb:

source ./.venv/bin/activate
pip install jupyter
jupyter notebook

The Python code in the rest of this document can be run in the Python environment you created.

Development Installation

To use nightly builds (not mandatory), you can install AISdb from source:

Alternatively, you can use nightly builds (not mandatory) on Google Colab as follows:

Database Handling

AISdb supports SQLite and PostgreSQL databases. When using PostgreSQL, TimescaleDB is an optional extension that AISdb can take advantage of for its automatic partitioning and compression of time-series data. It is not required (PostgreSQL alone works fine), but for large ingests it is worth setting up. decode_msgs() exposes a timescaledb argument (default False) that switches on the TimescaleDB-aware table schema when inserting data. If you want to enable it, follow these steps first.

1

Install TimescaleDB (PostgreSQL Extension)

2

Enable the Extension in PostgreSQL

3

Verify the Installation

4

Restart PostgreSQL

Connecting to a PostgreSQL database

This uses psycopg, which installs automatically with pip install aisdb, so there's nothing extra to add for interfacing with PostgreSQL databases. PostgreSQL accepts these keyword arguments. Alternatively, a connection string may be used. Information on connection strings and PostgreSQL URI format can be found here.

Attaching a SQLite database to AISdb

Querying SQLite is as easy as providing the name of a ".db" file with the same entity-relationship model as the databases supported by AISdb, which are detailed in the SQL Database section. We prepared an example SQLite database example_data.db based on AIS data from a small region near Maine, United States, in January 2022 from Marine Cadastre, which is available in the AISdb Tutorial GitHub repository.

If you want to create your database using your data, we have a tutorial with examples that show you how to create an SQLite database from open-source data.

Querying the Database

Parameters for the database query can be defined using aisdb.database.dbqry.DBQuery. Iterate over rows returned from the database for each vessel with aisdb.database.dbqry.DBQuery.gen_qry(). Convert the results into a generator yielding dictionaries with NumPy arrays describing position vectors, e.g., lon, lat, and time, using aisdb.track_gen.TrackGen().

The following query will return vessel trajectories from a given 1-hour time window:

A specific region can be queried for AIS data using aisdb.gis.Domain or one of its sub-classes to define a collection of shapely polygon features. For this example, the domain contains a single bounding box polygon derived from a longitude/latitude coordinate pair and radial distance specified in meters. If multiple features are included in the domain object, the domain boundaries will encompass the convex hull of all features.

Additional query callbacks for filtering by region, timeframe, identifier, etc., can be found in aisdb.database.sql_query_strings and aisdb.database.sqlfcn_callbacks.

Processing

Voyage Modelling

The above generator can be input into a processing function, yielding modified results. For example, when modeling the activity of vessels on a per-voyage or per-transit basis, each voyage is defined as a continuous vector of positions where the time between observed timestamps never exceeds 24 hours.

Data cleaning and MMSI deduplication

A common problem with AIS data is noise, where multiple vessels might broadcast using the same identifier (sometimes simultaneously). AISdb denoises this data in three steps.

First, denoising with the encoder. The aisdb.denoising_encoder.encode_greatcircledistance() function checks the approximate distance between each vessel's position. It separates vectors where a vessel couldn't reasonably travel using the most direct path, such as when the implied speed exceeds 50 knots.

Second, distance and speed thresholds. These thresholds limit the maximum distance or time between messages that can be considered continuous.

Third, scoring and segment concatenation. A score is computed for each position delta, with sequential messages nearby at shorter intervals given a higher score. This score is calculated by dividing the Haversine distance by elapsed time. Any deltas with a score not reaching the minimum threshold are considered the start of a new segment. New segments are compared to the end of existing segments with the same vessel identifier, and if the score exceeds the minimum, they are concatenated. If multiple segments meet the minimum score, the new segment is concatenated to the existing segment with the highest score.

Notice that processing functions may be executed in sequence as a chain or pipeline, so after segmenting the individual voyages as shown above, results can be input into the encoder to remove noise and correct for vessels with duplicate identifiers.

Interpolating, geofencing, and filtering

Building on the above processing pipeline, the resulting cleaned trajectories can be geofenced and filtered for results contained by at least one domain polygon and interpolated for uniformity.

Additional processing functions can be found in the aisdb.track_gen module.

Exporting as CSV

The resulting processed voyage data can be exported in CSV format instead of being printed:

Integration with external metadata

AISdb supports integrating external data sources such as bathymetric charts and other raster grids.

Bathymetric charts

To determine the approximate ocean depth at each vessel position, the aisdb.webdata.bathymetry module can be used.

Once the data has been downloaded, the Gebco() class may be used to append bathymetric data to tracks in the context of a TrackGen() processing pipeline like the processing functions described above.

Also, see aisdb.webdata.shore_dist.ShoreDist for determining the approximate nearest distance to shore from vessel positions.

Rasters

Similarly, arbitrary coordinate-gridded raster data may be appended to vessel tracks.

Visualization

AIS data from the database may be overlaid on a map such as the one shown below using the aisdb.web_interface.visualize() function. This function accepts a generator of track dictionaries such as those output by aisdb.track_gen.TrackGen().

Visualization of vessel tracks within a defined time range

For a complete plug-and-play solution, you may clone our Google Colab Notebook.

Where to go next

This quick start covers the basics of loading, querying, and visualizing AIS data. The Tutorials section goes deeper into each step, starting with database loading, then data querying, and data cleaning for handling noisy or duplicate position reports.

Last updated