oslew docs
Console
Menu

Zone crossings and dwells

A day of AIS is hundreds of fixes for one ship. What is usually wanted is a handful of events — entered Mexican waters at 02:01, left them at 09:47 — and /v1/crossings is that reduction. It evaluates a vessel's track against zone polygons and answers every change of membership, as crossings, and every stay between them, as dwells.

curl
curl -G "https://api.oslew.com/v1/crossings" \
  -H "Authorization: Bearer $GEO_API_KEY" \
  --data-urlencode 'mmsi=366749490' \
  --data-urlencode 'zones=maritime'
Python
from oslew import Client

# The key is read from GEO_API_KEY.
client = Client(base_url="https://api.oslew.com")

answer = client.crossings(366749490, zones="maritime")
TypeScript
import { Client } from "@oslew/client";

// The key is read from GEO_API_KEY.
const client = new Client({ baseUrl: "https://api.oslew.com" });

const answer = await client.crossings(366749490, { zones: "maritime" });
Response
HTTP/1.1 200 OK
content-type: application/json
ratelimit-limit: 100
ratelimit-remaining: 291
ratelimit-reset: 1

{
  "mmsi": 366749490,
  "from": 1735603200,
  "to": 1735689600,
  "positions": 359,
  "layers": 1,
  "crossings": [
    {
      "zone": 61,
      "name": "México",
      "subtype": "country",
      "entered": true,
      "at_start": false,
      "time": 1735610513,
      "lat": 31.30892,
      "lon": -116.75186
    },
    {
      "zone": 61,
      "name": "México",
      "subtype": "country",
      "entered": false,
      "at_start": false,
      "time": 1735638472,
      "lat": 32.53099,
      "lon": -117.20863
    },
    {
      "zone": 75,
      "name": "United States",
      "subtype": "country",
      "entered": true,
      "at_start": false,
      "time": 1735638472,
      "lat": 32.53099,
      "lon": -117.20863
    }
  ],
  "dwells": [
    {
      "zone": 61,
      "name": "México",
      "subtype": "country",
      "from": 1735610513,
      "to": 1735638353,
      "positions": 114,
      "open": false
    },
    {
      "zone": 75,
      "name": "United States",
      "subtype": "country",
      "from": 1735638472,
      "to": 1735689548,
      "positions": 241,
      "open": true
    }
  ],
  "provenance": {
    "name": "noaa-ais",
    "release": "2024-12-31",
    "license": "public domain (US Government work)"
  },
  "took_us": 12453
}

A real response, captured from the service at commit b8f4bde-dirty. Timing fields such as took_us differ on every call.

The vessel above came north up the coast of Baja California on 31 December 2024: it entered Mexican waters at 02:01 UTC, left them for United States waters at 09:47, and ended the day berthed in San Diego.

Asking

ParameterDefaultIs
mmsirequiredThe vessel.
from, tothe layer's spanUnix seconds, both inclusive. Above, left out, they are the whole of 31 December 2024, the day the examples were captured from.
zonesmaritimemaritime, or all for every divisions layer loaded.

zones=maritime evaluates the maritime zones: each country's maritime area from the Overture divisions, which is its whole claim — land plus territorial waters, not a shape of the sea alone. So a ship at a berth is inside its country's zone, and leaving the zone means going out beyond the territorial sea, typically 12 nautical miles offshore. An exclusive economic zone reaches up to 200 nautical miles, far past these polygons; this layer does not answer when a ship entered one.

zones=all evaluates every divisions layer the deployment has loaded — countries, regions, localities and neighbourhoods on land, the maritime zones, time zones — and a ship is in several at once. Only the exact value all does this; anything else, a misspelling included, is maritime.

The answer says what it evaluated: positions is how many fixes the track had in the window — the same number /v1/track would return — and layers how many zone layers it tested them against. A vessel with no fixes in the window is a 200 with zero positions and nothing else.provenance is the AIS source's; the zones carry their own, in the divisions layer.

Crossings

FieldIs
zoneThe zone's position in its layer. A key for matching crossings to dwells within one answer — not an id to store: it can change when the data is rebuilt, and with zones=all two layers can use the same number.
name, subtypeThe zone's name as the divisions data spells it — México — and what kind of division it is.
enteredtrue for an entry, false for an exit.
at_starttrue only on an entry at the track's first fix: the vessel began inside, and nothing says when it arrived.
time, lat, lonThe first fix on the new side.

Crossings are in time order, and by zone within one instant. A crossing happened somewhere between the last fix on the old side and the first on the new, and time is the second of the two. Above, the ship's last fix in Mexican waters was at 09:45:53 — the end of its dwell there — and its first in United States waters at 09:47:52, so the boundary was crossed in those 119 seconds. Where fixes are minutes or hours apart, the uncertainty is that long; if you need the moment, interpolate between the two fixes against the boundary yourself.

Passing from one zone straight into a neighbour is two crossings at the same fix: an entry into one and an exit from the other, listed in zone order, which is why the exit from México (zone 61) comes before the entry into the United States (zone 75) above.

Dwells

FieldIs
zone, name, subtypeAs on a crossing.
from, toThe first and last fixes inside, unix seconds.
positionsHow many fixes were inside.
opentrue when the track ended inside: to is the last fix of the window, not an exit.

A dwell is bounded by fixes, and at either end of the window by the window itself. One that starts with anat_start entry began before the window or before the ship was first heard; one that isopen goes on past the last fix. Above, the ship spent 114 fixes in Mexican waters, a closed dwell, and 241 in United States waters, an open one: it was still there when the day ended. The other 4 of its 359 fixes that day came before its entry into Mexican waters, out beyond the territorial sea, where no dwell is recorded; that is why the first crossing is an entry with at_start: false.

Reading them honestly

  • Membership is only judged at fixes. A gap in reception hides whatever happened inside it: a ship that went into a zone and out again between two fixes shows neither crossing.
  • Nothing is smoothed. A ship running along a boundary, or swinging at anchor beside one, crosses at every fix that lands on the other side. For visits rather than flickers, join dwells in the same zone that are separated by a short gap.
  • Zones nest. With zones=all, arriving somewhere can be a country, a region, a locality and a time zone at once, each its own crossing.
Python
def visits(dwells, gap_s=1800):
    """Join stays in one zone separated by less than gap_s: visits, not flickers."""
    out = []
    for d in sorted(dwells, key=lambda d: (d["zone"], d["name"], d["from"])):
        last = out[-1] if out else None
        if last and (last["zone"], last["name"]) == (d["zone"], d["name"]) and d["from"] - last["to"] <= gap_s:
            last.update(to=d["to"], positions=last["positions"] + d["positions"], open=d["open"])
        else:
            out.append(dict(d))
    return sorted(out, key=lambda d: d["from"])
TypeScript
interface Dwell { zone: number; name: string; subtype: string; from: number; to: number; positions: number; open: boolean }

/** Join stays in one zone separated by less than gapS: visits, not flickers. */
function visits(dwells: Dwell[], gapS = 1800): Dwell[] {
  const out: Dwell[] = [];
  const sorted = [...dwells].sort((a, b) => a.zone - b.zone || a.name.localeCompare(b.name) || a.from - b.from);
  for (const d of sorted) {
    const last = out[out.length - 1];
    if (last && last.zone === d.zone && last.name === d.name && d.from - last.to <= gapS) {
      Object.assign(last, { to: d.to, positions: last.positions + d.positions, open: d.open });
    } else {
      out.push({ ...d });
    }
  }
  return out.sort((a, b) => a.from - b.from);
}

What it is for

Entry and exit reports. When a vessel entered or left a country's territorial waters, how long it stayed and how many times it came back — the default, and what the answer above shows.

Local time aboard. With zones=all, the time-zone dwells say which zone's clock applied when; time zones gives the offsets.

Port calls, which are stops rather than crossings. A maritime zone includes its country's land and ports, so arriving in port from that country's own waters crosses nothing. A port call is a run of fixes where the ship is not moving; find it in the track, then name the place withreverse geocoding:

Python
from oslew import Client

client = Client(base_url="https://api.oslew.com")

def stops(positions, min_s=3600, max_tenths=5):
    """Runs of fixes under 0.5 kn lasting at least min_s: anchorages and berths."""
    runs, run = [], []
    for p in positions:
        if p["sog"] < max_tenths:                 # 1023 and 65535 mean unknown, and are not slow
            run.append(p)
            continue
        if run and run[-1]["time"] - run[0]["time"] >= min_s:
            runs.append(run)
        run = []
    if run and run[-1]["time"] - run[0]["time"] >= min_s:
        runs.append(run)
    return runs

track = client.track(366749490)["positions"]
for run in stops(track):
    mid = run[len(run) // 2]
    near = client.reverse(f"{mid['lat']},{mid['lon']}", limit=1)["results"]
    print(run[0]["time"], run[-1]["time"], near[0]["name"] if near else "open water")
TypeScript
import { Client } from "@oslew/client";

const client = new Client({ baseUrl: "https://api.oslew.com" });
type Fix = { time: number; lat: number; lon: number; sog: number };

/** Runs of fixes under 0.5 kn lasting at least minS: anchorages and berths. */
function stops(positions: Fix[], minS = 3600, maxTenths = 5): Fix[][] {
  const runs: Fix[][] = [];
  let run: Fix[] = [];
  const close = () => {
    if (run.length && run[run.length - 1].time - run[0].time >= minS) runs.push(run);
    run = [];
  };
  for (const p of positions) {
    if (p.sog < maxTenths) run.push(p); // 1023 and 65535 mean unknown, and are not slow
    else close();
  }
  close();
  return runs;
}

const { positions } = await client.track(366749490);
for (const run of stops(positions)) {
  const mid = run[Math.floor(run.length / 2)];
  const { results } = await client.reverse(mid.lat + "," + mid.lon, { limit: 1 });
  console.log(run[0].time, run[run.length - 1].time, results[0]?.name ?? "open water");
}

An hour under half a knot catches berths and anchorages and ignores a ship slowing for a pilot; tune both numbers to the fleet. The status field — 1 at anchor, 5 moored — is a useful second opinion, and only that: crews do not always change it.

Endpoints

GET/v1/crossingsplatform

Zone entries, exits and dwells along a vessel's track, against the maritime zone layers.