.. _intro: Introduction ============ AISDB was created to provide a complete set of tools to aid in the collection and processing of AIS data. AISDB can be used with either livestreaming AIS or historical raw AIS data files. The core data model behind AISDB is an SQLite database, and AISDB provides a Python interface to interact with the database; from database creation, querying, data processing, visualization, and exportation of data in CSV format. AISDB also provides tools for integrating AIS with environmental data in raster file formats. For example, AISDB provides convenience functions to download ocean bathymetric chart grids which may be used to query seafloor depth beneath each surface vessel position, although any raster data using longitude/latitude coordinate grid cells may be appended. .. raw:: html .. _index: Index ----- 0. :ref:`Python Environment ` * Install with Pip * Install with Docker 1. :ref:`Database Creation ` * From MERIDIAN's data livestream * From historical AIS data files 2. :ref:`Querying the database ` * Connect to the database * Get vessel trajectories from the database * Query a bounding box encapsulating a collection of zone polygons 3. :ref:`Processing AIS messages ` * Voyage modelling * Data cleaning and MMSI deduplication * Interpolating, Geofencing, and filtering * Exporting as CSV 4. :ref:`Integration with external metadata ` * Retrieve detailed vessel metadata from marinetraffic.com * Bathymetric charts * Rasters 5. :ref:`Visualization ` * Display AIS data in the web interface 6. :ref:`Data collection and sharing ` * Setting up an AIS receiving antenna * Sharing data to external networks .. _python-env: 0. Python Environment --------------------- .. include:: readme.rst :start-after: _install-pip: :end-before: _readme-docs: AISDB may also be used with docker-compose to run as services, for more info see :ref:`AISDB Docker docs `. The Python code in the rest of this document can then be run in the new Python environment. When using an interpreter such as `Jupyter `__, ensure that jupyter is installed in the same environment as AISDB. .. _db-create: 1. Database Creation -------------------- Creating a database from live streaming data ++++++++++++++++++++++++++++++++++++++++++++ A typical workflow for using AISDB requires a database of recorded AIS messages. The following code snippet demonstrates how to create a new database from MERIDIAN's AIS data stream. with the argument ``stdout=True``, the raw message input will be copied to stdout before it is decoded and added to the database. Also see the receiver api docs: :func:`aisdb.receiver.start_receiver`. .. code-block:: python from aisdb.receiver import start_receiver start_receiver(connect_addr='aisdb.meridian.cs.dal.ca:9920', sqlite_dbpath='AIS.sqlitedb', stdout=True) Creating a database from historical data files ++++++++++++++++++++++++++++++++++++++++++++++ An SQLite database file will be created at the specified database path ``dbpath``. The ``source`` string will be stored in the database along with the decoded message data, making it easier to integrate data from multiple sources into the same database. A checksum of the first 1000 bytes from the input file will be stored to prevent processing the same data file twice. Checksum validation can be disabled by adding the argument ``skip_checksum=True``. Decoding speed can be improved by placing the raw data files on a seperate hard drive from the database. .. code-block:: python import aisdb aisdb.decode_msgs( filepaths=['aisdb/tests/test_data_20210701.csv', 'aisdb/tests/test_data_20211101.nm4'], dbpath='AIS.sqlitedb', dbconn=aisdb.DBConn(), source='TESTING', ) The decoder accepts raw AIS data in the ``.nm4`` format, as long as a timestamp is included in the message header. For example: .. code-block:: text \s:41925,c:1635731889,t:1635731965*66\!AIVDM,1,1,,,19NSRM@01v;inKaVqpGVUmN:00Rh,0*7C \s:41925,c:1635731889,t:1635731965*66\!AIVDM,1,1,,,15Benl0000`. The receiver server can then forward incoming filtered messages to a downstream UDP multicast channel: .. code-block:: python import aisdb # listen for incoming raw AIS messages on port 9921 and share with MERIDIAN network aisdb.start_receiver( udp_listen_addr='0.0.0.0:9921', multicast_rebroadcast_addr='aisdb.meridian.cs.dal.ca:9921', )