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

Database Loading

Load AIS data into a SQLite or PostgreSQL database with AISdb, from installation through a full-year Spire ingestion example.

This tutorial will guide you in using the AISdb package to load AIS data into a database and perform queries. We will begin with AISdb installation and environment setup, then proceed to examples of querying the loaded data and creating simple visualizations.

Install Requirements

Preparing a Python virtual environment for AISdb is a good practice. It allows you to manage dependencies and prevent conflicts with other projects, ensuring a clean and isolated setup for your work with AISdb. Run these commands in your terminal based on the operating system you are using:

Linux
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/
Windows
python -m venv .venv         # create a virtual environment
./AISdb/Scripts/activate     # activate the virtual environment
pip install aisdb            # install the AISdb package using pip

Now you can check your installation by running:

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

If you're using AISdb in a Jupyter Notebook, please include the following commands in your notebook cells:

# install nest-asyncio for enabling asyncio.run() in Jupyter Notebook
%pip install nest-asyncio

# Some of the systems may show the following error when running the user interface:
# urllib3 v2.0 only supports OpenSSL 1.1.1+; currently, the 'SSL' module is compiled with 'LibreSSL 2.8.3'.
# install urllib3 v1.26.6 to avoid this error
%pip install urllib3==1.26.6

Then, import the required packages:

Load AIS data into a database

This section will show you how to efficiently load AIS data into a database.

AISdb includes two database connection approaches:

  1. SQLite database connection; and,

  2. PostgreSQL database connection.

We work with the SQLite database in most usage scenarios. Here is an example of loading data using the sample data included in the AISdb package:

The code above decodes the AIS messages from the CSV file specified in filepaths and inserts them into the SQLite database connected via dbconn.

The following is a quick example of a query and visualization of the data we just loaded with AISdb:

Visualization of vessel tracks queried from SQLite database created from test data

In addition to SQLite, AISdb supports PostgreSQL, which handles concurrent access and data sharing better than SQLite and scales more comfortably to larger, collaborative deployments. psycopg (the PostgreSQL driver AISdb uses under the hood) ships as a core dependency, so pip install aisdb already gives you everything you need, no separate driver install required.

To connect to a PostgreSQL database, AISdb uses the PostgresDBConn class:

After you open a connection to PostgreSQL and point aisdb.decode_msgs at your data files, it runs through file parsing, table creation, data insertion, and index rebuilding, in that order.

Please pay close attention to the arguments of aisdb.decode_msgs, since they control how files are parsed, how fast the load runs, and which table layout gets used. The full signature is decode_msgs(filepaths, dbconn, source, vacuum=False, skip_checksum=True, workers=4, type_preference="all", raw_insertion=True, verbose=True, timescaledb=False). The parameters worth understanding before a large load are:

  • source (str, required) A free-form label identifying where the data came from, stored alongside each decoded message (for example "Spire", "NOAA", or "TESTING"). There is no default; you must always pass one. If the string contains "noaa" (case-insensitive), AISdb switches to parsing NOAA's BaseDateTime CSV column instead of the generic Time column.

  • workers (int, optional)

    • Number of parallel worker processes used to decode files.

    • Default: 4.

  • type_preference (str, optional)

    • Which AIS message types to keep during decoding ("all", "static", or "dynamic").

    • Default: "all".

  • raw_insertion (bool, optional)

    • If True, rows are inserted without maintaining indexes as you go, which is significantly faster for bulk loads. Indexes get (re)built afterward. Set to False only for small, incremental inserts into an already-indexed table.

    • Default: True.

  • skip_checksum (bool, optional)

    • If True, skips the MD5 checksum lookup AISdb otherwise uses to avoid re-ingesting a file it has already processed.

    • Default: True.

  • vacuum (bool, optional)

    • If True, runs a VACUUM on the database after insertion to reclaim space and update planner statistics.

    • Default: False.

  • timescaledb (bool, optional)

    • Set to True only if using the TimescaleDB extension in your PostgreSQL database, which structures dynamic tables as hypertables instead of the original per-month B-Tree indexed tables.

    • Default: False. Refer to the TimescaleDB documentation for proper setup and usage.

Example: Processing a Full Year of Spire Data (2024)

The following example demonstrates how to process and load Spire data for the entire year 2024 into an aisdb database with the TimescaleDB extension installed:

Example of performing queries and visualizations with PostgreSQL database:

Visualization of tracks queried from PostgreSQL database

If you want to load your own AIS data instead of the bundled test files, see our guide on data processing and database creation, Using Your AIS Data.

Last updated