Skip to main content

Reverse-geocode a coordinate

Use this procedure to find the places that contain a coordinate, such as the neighborhood, citylocalityThe city / town / settlement component of an address: a populated place sitting between region and neighborhood in the hierarchy., regionregionThe first-level administrative subdivision of a country — a US state, a French region, a province. The component between country and locality. and countrycountryThe top-level address component (an ISO country). Closed-vocabulary, so it is best handled by a deterministic matcher feeding a proposal rather than a retrained model head. of a GPS reading. Mailwoman's reverse geocoder returns a Who's On FirstWOF (Who's On First). An open-source gazetteer of places maintained by Mapzen/whosonfirst. Mailwoman builds a custom SQLite database from WOF GeoJSON repos, extended with postcode data, importance scores, and coincident-role relations. administrative hierarchy, from the smallest containing place up to the countrycountryThe top-level address component (an ISO country). Closed-vocabulary, so it is best handled by a deterministic matcher feeding a proposal rather than a retrained model head.. StreetstreetThe named linear feature along which house numbers are ordered. Decomposes into a name plus street affixes; one of the Tier 2 fine labels. addresses and house numbershouse numberThe numeric or alphanumeric identifier of a building on a street. Mailwoman's house_number component; its position relative to the street name flips between locales. come from forward geocodinggeocodingThe process of converting an address into geographic coordinates (latitude and longitude). Mailwoman geocodes in a multi-tier cascade: exact address-point match → street interpolation → locality centroid. Each tier is progressively coarser but more widely available..

Prerequisites​

  • Node.js ^24.22.3 || >=26.13.1
  • ECMAScript module project with "type": "module" in its package.json
  • Start by installing the required packages:

    sh
    npm install mailwoman @mailwoman/resolver-wof-sqlite

    The reverse geocoder reads an admin gazetteergazetteerA geographical index that maps place names and postcodes to real-world coordinates. Mailwoman uses a custom-built Who's On First (WOF) SQLite database as its gazetteer — the 'atlas' half of the grammar/atlas architecture.: a Who's On FirstWOF (Who's On First). An open-source gazetteer of places maintained by Mapzen/whosonfirst. Mailwoman builds a custom SQLite database from WOF GeoJSON repos, extended with postcode data, importance scores, and coincident-role relations. SQLite database with a place_bbox spatial indexR*TreeSQLite's spatial index of bounding boxes, enabling fast geographic range and nearest-neighbor queries in the resolver.. The 11.0.0 data bundlesevidence bundleThe pair of retrieval-augmented input channels (street-type + locality-surface) that feed lexicon membership as soft per-token evidence alongside the text. Shipped in 6.7.0; trained natively from step 0 in the from-scratch base line. ship without one, and the candidate.db file from mailwoman data pull candidate lacks that index. Build the gazetteergazetteerA geographical index that maps place names and postcodes to real-world coordinates. Mailwoman uses a custom-built Who's On First (WOF) SQLite database as its gazetteer — the 'atlas' half of the grammar/atlas architecture. from local clones of the Who's On FirstWOF (Who's On First). An open-source gazetteer of places maintained by Mapzen/whosonfirst. Mailwoman builds a custom SQLite database from WOF GeoJSON repos, extended with postcode data, importance scores, and coincident-role relations. repositories, then add its search and spatial indexesR*TreeSQLite's spatial index of bounding boxes, enabling fast geographic range and nearest-neighbor queries in the resolver.:

    sh
    npx mailwoman gazetteer build admin --data /path/to/whosonfirst-repos --out admin-global-priority.db
    npx mailwoman gazetteer build fts admin-global-priority.db

    The two commands above are checked against the published --help output only. The global gazetteergazetteerA geographical index that maps place names and postcodes to real-world coordinates. Mailwoman uses a custom-built Who's On First (WOF) SQLite database as its gazetteer — the 'atlas' half of the grammar/atlas architecture. used for the output below came from an earlier build on the same host and occupies 7,316,430,848 bytes.

    Point the reverse geocoder at the result:

    sh
    export MAILWOMAN_WOF_ADMIN_DB="$PWD/admin-global-priority.db"

    An optional polygon sidecar, read from $MAILWOMAN_WOF_POLYGONS_DB, holds place boundaries. With it, the geocoder tests whether the coordinate lies inside each place's boundary. Without it, the geocoder estimates containment from place centroids and bounding boxes. Step 2 shows the difference. mailwoman gazetteer polygons builds the sidecar. It reads the admin gazetteergazetteerA geographical index that maps place names and postcodes to real-world coordinates. Mailwoman uses a custom-built Who's On First (WOF) SQLite database as its gazetteer — the 'atlas' half of the grammar/atlas architecture. passed as --admin and the Who's On FirstWOF (Who's On First). An open-source gazetteer of places maintained by Mapzen/whosonfirst. Mailwoman builds a custom SQLite database from WOF GeoJSON repos, extended with postcode data, importance scores, and coincident-role relations. GeoJSON clones under --repos, and writes the database passed as --out. That command was read from the published 11.0.0 source and wasn't run for this guide.

    The output below was captured with locally built admin and polygon databases. Place IDs and names in your output come from your own Who's On FirstWOF (Who's On First). An open-source gazetteer of places maintained by Mapzen/whosonfirst. Mailwoman builds a custom SQLite database from WOF GeoJSON repos, extended with postcode data, importance scores, and coincident-role relations. build and can differ.

    1. Reverse-geocode one coordinate​

    sh
    npx mailwoman reverse 38.8977 -77.0365
    json
    {
    "lat": 38.8977,
    "lon": -77.0365,
    "containment": "approximate",
    "hierarchy": [
    {
    "id": 1108724059,
    "name": "White House Grounds",
    "placetype": "neighbourhood",
    "country": "US",
    "lat": 38.897087,
    "lon": -77.036539,
    "distanceKm": 0.0682459965296924
    },
    {
    "id": 1108723661,
    "name": "Northwest Washington",
    "placetype": "macrohood",
    "country": "US",
    "lat": 38.94299,
    "lon": -77.052654,
    "distanceKm": null
    },
    {
    "id": 85931779,
    "name": "Washington",
    "placetype": "locality",
    "country": "US",
    "lat": 38.904831,
    "lon": -77.016216,
    "distanceKm": null
    },
    {
    "id": 1377370667,
    "name": "District of Columbia",
    "placetype": "county",
    "country": "US",
    "lat": 38.912097,
    "lon": -77.014683,
    "distanceKm": null
    },
    {
    "id": 85688741,
    "name": "District of Columbia",
    "placetype": "region",
    "country": "US",
    "lat": 38.912097,
    "lon": -77.014683,
    "distanceKm": null
    },
    {
    "id": 85633793,
    "name": "United States",
    "placetype": "country",
    "country": "US",
    "lat": 39.71614,
    "lon": -96.999246,
    "distanceKm": null
    }
    ],
    "engine": {
    "name": "mailwoman",
    "version": "11.0.0",
    "license": "AGPL-3.0-only",
    "license_url": "https://mailwoman.post/license",
    "notice": "mailwoman is licensed AGPL-3.0-only: modified or network-served copies must offer their source. A commercial license waives that obligation."
    }
    }

    The output above is reformatted with one place per line. hierarchy runs from the smallest containing place to the countrycountryThe top-level address component (an ISO country). Closed-vocabulary, so it is best handled by a deterministic matcher feeding a proposal rather than a retrained model head.. Each entry's id is its Who's On FirstWOF (Who's On First). An open-source gazetteer of places maintained by Mapzen/whosonfirst. Mailwoman builds a custom SQLite database from WOF GeoJSON repos, extended with postcode data, importance scores, and coincident-role relations. ID, and its lat and lon are that place's own representative point. The lat and lon at the top of the result echo your coordinate.

    containment: "approximate" means no polygon sidecar was set. The finest place was chosen by its centroid, and distanceKm gives the distance from your coordinate to that centroid, 0.07 km here.

    2. Use place boundaries​

    Set the polygon sidecar and repeat the command:

    sh
    export MAILWOMAN_WOF_POLYGONS_DB="$PWD/wof-polygons.db"

    npx mailwoman reverse 38.8977 -77.0365

    The hierarchy is the same six places. The result now reads containment: "polygon", and every distanceKm is null, because each place was confirmed by its boundary rather than estimated from its centroid.

    --format text prints one place per line. For a coordinate in Arlington, Virginia, with and then without the sidecar:

    sh
    npx mailwoman reverse 38.8816 -77.0910 --format text
    text
    containment: polygon
    neighbourhood Lyon Park [wof:420546549]
    locality Arlington [wof:101729469]
    county Arlington [wof:102085953]
    region Virginia [wof:85688747]
    country United States [wof:85633793]
    text
    containment: approximate
    neighbourhood Lyon Park [wof:420546549] (~0.3 km from centroid)
    locality Arlington [wof:101729469]
    county Arlington [wof:102085953]
    region Virginia [wof:85688747]
    country United States [wof:85633793]

    Both runs returned the same places for this coordinate. The approximate method chooses the finest place by centroid distance, so close to a boundary it can choose the neighboring place. Neither tested coordinate showed that case. Use the sidecar when the finest place matters, and treat an approximate answer as an estimate.

    3. Handle coordinates outside every place​

    A point in open ocean returns an empty hierarchy:

    sh
    npx mailwoman reverse 35 -40 --format text
    text
    containment: approximate
    (no admin hierarchy — point may be in open ocean or outside the gazetteer coverage)

    In JSON output, hierarchy is an empty array. The command still exits 0, so check the hierarchy's length rather than the exit status. The result reads approximate even with the sidecar set, because no boundary contained the point.

    4. Reverse-geocode a list of points in a script​

    For a list of coordinates, such as a fleet's GPS readings, open the geocoder once and reuse it. Save this script as reverse-points.ts:

    ts
    import { WOFReverseGeocoder } from "@mailwoman/resolver-wof-sqlite"

    const geocoder = new WOFReverseGeocoder({
    adminDBPath: process.env.MAILWOMAN_WOF_ADMIN_DB,
    polygonDBPath: process.env.MAILWOMAN_WOF_POLYGONS_DB,
    })

    const points = [
    { id: "truck-1", lat: 38.8977, lon: -77.0365 },
    { id: "truck-2", lat: 38.8816, lon: -77.091 },
    { id: "truck-3", lat: 48.8584, lon: 2.2945 },
    { id: "ship-1", lat: 35, lon: -40 },
    ]

    try {
    for (const point of points) {
    const result = geocoder.reverseGeocodeSync(point.lat, point.lon)
    const locality = result.hierarchy.find((place) => place.placetype === "locality")
    const country = result.hierarchy.find((place) => place.placetype === "country")

    console.log(
    `${point.id}: ${locality?.name ?? "-"}, ${country?.name ?? "-"} (${result.containment}, ${result.hierarchy.length} places)`
    )
    }
    } finally {
    geocoder[Symbol.dispose]()
    }
    sh
    node reverse-points.ts
    text
    truck-1: Washington, United States (polygon, 6 places)
    truck-2: Arlington, United States (polygon, 5 places)
    truck-3: Paris, France (polygon, 7 places)
    ship-1: -, - (approximate, 0 places)

    reverseGeocodeSync answers each point from the open database. reverseGeocode is its promise-returning form. Close the geocoder when the batchbatch sizeHow many examples the model processes before each parameter update. Larger batches give smoother gradients but cost more memory; gradient accumulation simulates a big batch on a small GPU. is done so its database handles are released. A point with an empty hierarchy, like ship-1, prints - for its localitylocalityThe city / town / settlement component of an address: a populated place sitting between region and neighborhood in the hierarchy. and countrycountryThe top-level address component (an ISO country). Closed-vocabulary, so it is best handled by a deterministic matcher feeding a proposal rather than a retrained model head.. Record it as unplaced, with its own coordinate.

    The result schema describes the forward geocode result and its own hierarchy field. How geocoding chooses a coordinate explains reverse containment beside forward resolution.