Coast, shore, and ports
Extracting distance features from and to points-of-interest using raster files.
The distance of a vessel from the nearest shore, coast, and port is a useful feature for vessel behavior analysis, environmental monitoring, and maritime safety assessments. AISdb offers functions to acquire these distances for specific vessel positions using raster files sourced from NASA and Global Fishing Watch. In this tutorial, we calculate the distance in kilometers from shore, coast, and the nearest port for a sample track, then show how to fetch a list of real-world ports for a bounding box.
First, we create a sample track:
import numpy as np
import aisdb
from datetime import datetime
from aisdb.gis import dt_2_epoch
y1, x1 = 44.57039426840729, -63.52931373766157
y2, x2 = 44.51304767533133, -63.494075674952555
y3, x3 = 44.458038982492134, -63.535634138077945
y4, x4 = 44.393941339104074, -63.53826396955358
y5, x5 = 44.14245580737021, -64.16608964280064
t1 = dt_2_epoch( datetime(2021, 1, 1, 1) )
t2 = dt_2_epoch( datetime(2021, 1, 1, 2) )
t3 = dt_2_epoch( datetime(2021, 1, 1, 3) )
t4 = dt_2_epoch( datetime(2021, 1, 1, 4) )
t5 = dt_2_epoch( datetime(2021, 1, 1, 7) )
# creating a sample track
tracks_short = [
dict(
mmsi=123456789,
lon=np.array([x1, x2, x3, x4, x5]),
lat=np.array([y1, y2, y3, y4, y5]),
time=np.array([t1, t2, t3, t4, t5]),
dynamic=set(['lon', 'lat', 'time']),
static=set(['mmsi'])
)
]Here is what the sample track looks like:

Distance from shore, coast, and port
ShoreDist, CoastDist, and PortDist all follow the same pattern, downloading the relevant raster from the AISdb data release on GitHub, merging it against the provided track list, and storing the result under a new dynamic key.
The class aisdb.webdata.shore_dist.ShoreDist is used to calculate the nearest distance to shore, using a raster file containing shore distance data. Currently, calling the get_distance function in ShoreDist will automatically download the shore distance raster file from the AISdb data release on GitHub. The function then merges the tracks in the provided track list, creates a new key, "km_from_shore", and stores the shore distance as the value for this key.
Similar to acquiring the distance from shore, CoastDist is implemented to obtain the distance between the given track positions and the coastline.
Like the distances from the coast and shore, the aisdb.webdata.shore_dist.PortDist class determines the distance between the track positions and the nearest ports.
Fetching a list of ports
ShoreDist, CoastDist, and PortDist all answer the same question from the vessel's side, how far is this point from a raster surface. Sometimes you need the opposite view, a list of the actual ports inside a region so you can label tracks, filter for cargo-capable harbors, or join port metadata onto your AIS data. AISdb exposes this through aisdb.ports.api.WorldPortIndexClient, which queries the World Port Index feature service maintained by the National Geospatial-Intelligence Agency and returns the result as a pandas DataFrame.
fetch_ports takes a bounding box as lat_min, lat_max, lon_min, lon_max, and returns every port whose coordinates fall inside it. Passing save=True along with out_path writes the result to a CSV file as well. The example below queries the Gulf of St. Lawrence, then narrows the result to ports with a usable cargo depth using filter_by_cargo_depth.
Each row in df_ports carries the port's LAT and LON columns along with the full set of World Port Index fields, such as PORT_NAME and HARBORSIZE. From here it is straightforward to combine the port list with the hexagon discretization tutorial, indexing each port with aisdb.discretize.h3.Discretizer so it shares the same spatial grid as your decoded AIS tracks, or joining km_from_port values back to the nearest named port for reporting.
Last updated