Use NaPTAN data for the UK stop finder

Import active NaPTAN stops into a local SQLite spatial index, including conversion of British National Grid coordinates. Add local searches, shareable stop links, source details and transport mode colours while retaining OSM route lookup by ATCO code.

Closes #1
This commit is contained in:
Edward Betts 2026-09-18 18:04:55 +01:00
parent 8ed7b8d112
commit 29afb4f115
12 changed files with 503 additions and 55 deletions

View file

@ -109,33 +109,50 @@ when the server is running.
### UK Bus Stop Finder
A mobile-friendly map for finding the `naptan:AtcoCode` recorded on UK bus
stops in OpenStreetMap. Search by postcode, street, place, exact ATCO code, or
browser location. Postcode, street, and place searches show up to 20
UK-only Nominatim matches for the user to choose from. Searches are preserved in shareable `?q=...` or
`?lat=...&lon=...` URLs, and `latitude, longitude` can be entered directly in
the search box. Selecting a stop shows its tags and stop-area-aware bus route
relations. Stop results identify their transport mode (such as bus, rail or
tram), and moving the map at zoom level 15 or closer loads stops in the visible
area. Wider map views deliberately do not query Overpass.
Postcode searches with exactly one Nominatim match skip the choice screen.
The current map bounds bias Nominatim's ranking without excluding UK matches
outside the visible area.
A mobile-friendly map for finding ATCO codes using the Department for Transport's
[NaPTAN dataset](https://beta-naptan.dft.gov.uk/download). It covers active public
transport stops in England, Scotland and Wales, including bus, rail, tram,
ferry and airport access points; Northern Ireland is not included.
Selecting a stop replaces the search parameters with a shareable stop URL such
as `?node=485403163` (or `?way=...` / `?relation=...` for other OSM object
types). Refreshing that URL fetches and reopens the same stop.
Search by postcode, street, place, exact ATCO code, coordinates or browser
location. Place searches use Nominatim, with the visible map as a ranking bias.
The map loads stops at zoom level 15 or closer. Selecting a stop shows its
NaPTAN fields and looks up bus routes in OpenStreetMap by matching its ATCO code.
OSM route coverage may be incomplete; NaPTAN itself does not contain routes or
live departures. An Overpass outage does not prevent NaPTAN stop searches.
UK stop queries use the Britain-and-Ireland Overpass instance at
`overpass.atownsend.org.uk`, with `overpass.private.coffee` as an automatic
fallback when the primary service is unavailable.
Searches have shareable `?q=...` and `?lat=...&lon=...` URLs; a selected NaPTAN
stop uses `?naptan=010000056`. Existing `?node=...`, `?way=...` and
`?relation=...` OSM links still work.
Run it locally with:
Download the national CSV from NaPTAN, then import it and start the app:
```sh
python3 -m venv .venv
.venv/bin/pip install -e ".[web]"
.venv/bin/uk-bus-stops-import-naptan Stops.csv
.venv/bin/flask --app uk_bus_stops.app run
```
pip install -e ".[web]"
flask --app uk_bus_stops.app run
```
The importer uses SQLite's built-in R-tree spatial index; no database server or
SpatiaLite extension is required. It preserves leading zeros in ATCO codes,
converts British National Grid coordinates when latitude/longitude are absent,
and reports imported, inactive/pending, invalid and converted row counts.
Only active records with usable positions are included. The original CSV fields
remain available in the detail panel. XML is not needed.
The default database is `data/naptan.sqlite3` in the project directory. Set
`NAPTAN_DATABASE=/absolute/path/naptan.sqlite3` for both import and serving, or
use `--database PATH` for the importer. The serving process needs read access.
To refresh, download a new CSV and rerun the same command. It builds a temporary
database alongside the destination and replaces the old snapshot atomically;
a failed import leaves the old snapshot intact. Downloads and databases are
excluded from git. The app returns a JSON 503 with import instructions if its
database is missing or unreadable.
NaPTAN is the default stop source. Set `UK_BUS_STOPS_SOURCE=osm` to retain
Overpass stop searches instead. OSM route queries use `overpass.atownsend.org.uk`,
with `overpass.private.coffee` as a fallback.
The production URL is `https://openstreetmap.tools/uk-bus-stops/`.
The app's `/about` page documents shareable URL parameters, map behaviour,