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 safe 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-mvenvAISdb# create a python virtual environmentsource./AISdb/bin/activate# activate the virtual environmentpipinstallaisdb# from https://pypi.org/project/aisdb/
Windows
python-mvenvAISdb# create a virtual environment./AISdb/Scripts/activate# activate the virtual environmentpipinstallaisdb# install the AISdb package using pip
Now you can check your installation by running:
$python>>> importaisdb>>> aisdb.__version__# should return '1.7.0' or newer
If you're using AISdb in Jupyter Notebook, please include the following commands in your notebook cells:
# install nest-asyncio for enabling asyncio.run() in Jupyter Notebook%pipinstallnest-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%pipinstallurllib3==1.26.6
Then, import the required packages:
from datetime import datetime, timedeltaimport osimport aisdbimport nest_asyncionest_asyncio.apply()
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:
SQLite database connection; and,
PostgreSQL database connection.
SQLite database connection
We are working with the SQLite database in most of the 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.
Following is a quick example of a query and visualization of the data we just loaded with AISdb:
In addition to SQLite database connection, PostgreSQL is used in AISdb for its superior concurrency handling and data-sharing capabilities, making it suitable for collaborative environments and handling larger datasets efficiently. The structure and interactions with PostgreSQL are designed to provide robust and scalable solutions for AIS data storage and querying. For PostgreSQL, you need the psycopg2 library:
pipinstallpsycopg2
To connect to a PostgreSQL database, AISdb uses the PostgresDBConn class:
from aisdb.database.dbconn import PostgresDBConn# Option 1: Using keyword argumentsdbconn =PostgresDBConn( hostaddr='127.0.0.1', # Replace with the PostgreSQL address port=5432, # Replace with the PostgreSQL running port user='USERNAME', # Replace with the PostgreSQL username password='PASSWORD', # Replace with your password dbname='aisviz'# Replace with your database name)# Option 2: Using a connection stringdbconn =PostgresDBConn('postgresql://USERNAME:PASSWORD@HOST:PORT/DATABASE')
Example of performing queries and visualizations with PostgreSQL database:
from aisdb.gis import DomainFromPointsfrom aisdb.database.dbqry import DBQueryfrom datetime import datetime# Define a spatial domain centered around the point (-63.6, 44.6) with a radial distance of 50000 meters.domain =DomainFromPoints(points=[(-63.6, 44.6)], radial_distances=[50000])# Create a query object to fetch AIS data within the specified time range and spatial domain.qry =DBQuery( dbconn=dbconn, start=datetime(2023, 1, 1), end=datetime(2023, 2, 1), xmin=domain.boundary['xmin'], xmax=domain.boundary['xmax'], ymin=domain.boundary['ymin'], ymax=domain.boundary['ymax'], callback=aisdb.database.sqlfcn_callbacks.in_time_bbox_validmmsi)# Generate rows from the queryrowgen = qry.gen_qry()# Convert the generated rows into trackstracks = aisdb.track_gen.TrackGen(rowgen, decimate=False)# Visualize the tracks on a mapaisdb.web_interface.visualize( tracks, # The tracks (trajectories) to visualize. domain=domain, # The spatial domain to use for the visualization. visualearth=True, # If True, use Visual Earth for the map background. open_browser=True# If True, automatically open the visualization in a web browser.)
Moreover, if you wish to use your own AIS data to create and process a database with AISdb, please check out our instructional guide on data processing and database creation: Using Your AIS Data.
# List the test data files included in the packageprint(os.listdir(os.path.join(aisdb.sqlpath, '..', 'tests', 'testdata')))# You will see the print result: # ['test_data_20210701.csv', 'test_data_20211101.nm4', 'test_data_20211101.nm4.gz']# Set the path for the SQLite database file to be usedtest_database.db'# Use test_data_20210701.csv as the test datafilepaths = [os.path.join(aisdb.sqlpath, '..', 'tests', 'testdata', 'test_data_20210701.csv')]with aisdb.DBConn(dbpath = dbpath)as dbconn: aisdb.decode_msgs(filepaths=filepaths, dbconn=dbconn, source='TESTING')