Embedding with traj2vec
Discretize AIS trajectories onto an H3 grid and train a t2vec-style encoder-decoder in PyTorch that embeds vessel tracks as fixed-length vectors.
What you will learn
Prerequisites
pip install aisdb torch h3 geopandas cartopy matplotlib seaborn scikit-learn tqdm nest_asyncioStep 1. Query and clean the tracks
import os
import json
import h3
import aisdb
import numpy as np
import geopandas as gpd
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as cfeature
from aisdb.database.dbconn import PostgresDBConn
from aisdb.denoising_encoder import encode_greatcircledistance, InlandDenoising
from aisdb.track_gen import min_speed_filter
from aisdb.database import sqlfcn
from datetime import datetime, timedelta
from tqdm import tqdm
import nest_asyncio
nest_asyncio.apply()
dbconn = PostgresDBConn(hostaddr='127.0.0.1', port=5432, user='postgres',
password=os.environ.get('POSTGRES_PASSWORD'), dbname='postgres')
def process_interval(dbconn, start, end):
qry = aisdb.DBQuery(dbconn=dbconn, start=start, end=end,
xmin=xmin, ymin=ymin, xmax=xmax, ymax=ymax,
callback=aisdb.database.sqlfcn_callbacks.in_bbox_time_validmmsi)
# decimate=False keeps every reported point instead of curve-decimating them
rowgen = qry.gen_qry(fcn=sqlfcn.crawl_dynamic_static)
tracks = aisdb.track_gen.TrackGen(rowgen, decimate=False)
with InlandDenoising(data_dir='./data/tmp/') as remover:
cleaned_tracks = remover.filter_noisy_points(tracks)
# Split on time gaps, drop implausible segments, interpolate every minute.
track_segments = aisdb.track_gen.split_timedelta(cleaned_tracks, time_split)
tracks_encoded = encode_greatcircledistance(track_segments, distance_threshold=distance_split, speed_threshold=speed_split)
tracks_encoded = min_speed_filter(tracks_encoded, minspeed=1)
tracks_interpolated = aisdb.interp.interp_time(tracks_encoded, step=timedelta(minutes=1))
return list(tracks_interpolated)Step 2. Tokenize tracks onto the H3 grid
Step 3. Inspect and filter track lengths



Step 4. Build the H3 vocabulary
Step 5. Split and write the dataset

Step 6. Train and evaluate
Results
Takeaway
Last updated