@mailwoman/nominatim 7.2.0 → 7.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/app.ts +74 -0
- package/cli.ts +385 -0
- package/dev-tools/capture-search-golden.run.ts +81 -0
- package/engine.ts +114 -0
- package/format.ts +150 -0
- package/index.ts +29 -0
- package/package.json +33 -19
- package/routes.ts +281 -0
- package/schema.ts +222 -0
package/format.ts
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright Sister Software
|
|
3
|
+
* @license AGPL-3.0
|
|
4
|
+
* @author Teffen Ellis, et al.
|
|
5
|
+
*
|
|
6
|
+
* The RESOLVED-address → Nominatim-schema formatter: rendering a {@link ResolvedAddress} into a
|
|
7
|
+
* {@link NominatimResult} (`toNominatimResult`), the `format=geojson` FeatureCollection envelope
|
|
8
|
+
* (`toFeatureCollection`), and the `format=jsonld` schema.org projection
|
|
9
|
+
* (`nominatimResultToSchemaOrg`). Wire types + the engine contract live in `engine.ts`.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { composeStreetAddress, type SchemaOrgPlace, toSchemaOrg } from "@mailwoman/annotations"
|
|
13
|
+
|
|
14
|
+
import type { NominatimAddressDetails, NominatimResult } from "./engine.ts"
|
|
15
|
+
|
|
16
|
+
/** A GeoJSON `FeatureCollection` — the `format=geojson` envelope. */
|
|
17
|
+
export interface NominatimFeatureCollection {
|
|
18
|
+
type: "FeatureCollection"
|
|
19
|
+
features: Array<{
|
|
20
|
+
type: "Feature"
|
|
21
|
+
properties: Record<string, unknown>
|
|
22
|
+
geometry: unknown
|
|
23
|
+
bbox?: [number, number, number, number]
|
|
24
|
+
}>
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Render results as a GeoJSON `FeatureCollection` (`format=geojson`). Nominatim moves the coordinate into `geometry` (a
|
|
29
|
+
* Point, or the place polygon when one is present), `boundingbox` ([south, north, west, east]) into a GeoJSON `bbox`
|
|
30
|
+
* ([west, south, east, north]), and the remaining result fields into `properties`. Rows without a coordinate are
|
|
31
|
+
* dropped — a Feature needs a geometry.
|
|
32
|
+
*/
|
|
33
|
+
export function toFeatureCollection(results: readonly NominatimResult[]): NominatimFeatureCollection {
|
|
34
|
+
const features: NominatimFeatureCollection["features"] = []
|
|
35
|
+
|
|
36
|
+
for (const r of results) {
|
|
37
|
+
if (r.lat == null || r.lon == null) continue
|
|
38
|
+
const { lat, lon, boundingbox, geojson, ...properties } = r
|
|
39
|
+
const feature: NominatimFeatureCollection["features"][number] = {
|
|
40
|
+
type: "Feature",
|
|
41
|
+
properties,
|
|
42
|
+
geometry: geojson ?? { type: "Point", coordinates: [Number(lon), Number(lat)] },
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (boundingbox?.length === 4) {
|
|
46
|
+
// boundingbox is [south, north, west, east]; GeoJSON bbox is [west, south, east, north].
|
|
47
|
+
feature.bbox = [Number(boundingbox[2]), Number(boundingbox[0]), Number(boundingbox[3]), Number(boundingbox[1])]
|
|
48
|
+
}
|
|
49
|
+
features.push(feature)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return { type: "FeatureCollection", features }
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* A resolved address in a neutral shape, the input to {@link toNominatimResult}. The engine maps its native
|
|
57
|
+
* geocode/reverse result into this; the formatter renders it as a Nominatim result. This is the #804 mapping seam, kept
|
|
58
|
+
* dependency-free (no `@mailwoman/*` import) so it stays unit-testable.
|
|
59
|
+
*/
|
|
60
|
+
export interface ResolvedAddress {
|
|
61
|
+
lat: number | null
|
|
62
|
+
lon: number | null
|
|
63
|
+
address: NominatimAddressDetails
|
|
64
|
+
/** Pre-rendered display name; falls back to the address values joined by ", ". */
|
|
65
|
+
displayName?: string
|
|
66
|
+
category?: string
|
|
67
|
+
type?: string
|
|
68
|
+
importance?: number
|
|
69
|
+
placeRank?: number
|
|
70
|
+
boundingbox?: [string, string, string, string]
|
|
71
|
+
/** A stable id from the resolver (WOF/GERS); a deterministic hash is used when absent. */
|
|
72
|
+
placeID?: string | number
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** The attribution string emitted as `licence` (the data sources Mailwoman resolves over). */
|
|
76
|
+
export const MAILWOMAN_LICENCE = "Data © Who's On First, Overture Maps, OpenAddresses, US Census TIGER"
|
|
77
|
+
|
|
78
|
+
function stableID(seed: string): number {
|
|
79
|
+
let h = 5381
|
|
80
|
+
|
|
81
|
+
for (let i = 0; i < seed.length; i++) {
|
|
82
|
+
h = (h * 33) ^ seed.charCodeAt(i)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return h >>> 0
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Render a {@link ResolvedAddress} as a Nominatim result. `addressdetails` gates the `address` block, matching
|
|
90
|
+
* Nominatim. The `annotations` block is attached by the caller (empty until the annotations layer lands).
|
|
91
|
+
*/
|
|
92
|
+
export function toNominatimResult(r: ResolvedAddress, opts: { addressdetails?: boolean } = {}): NominatimResult {
|
|
93
|
+
const displayName = r.displayName ?? Object.values(r.address).filter(Boolean).join(", ")
|
|
94
|
+
const lat = r.lat != null ? String(r.lat) : ""
|
|
95
|
+
const lon = r.lon != null ? String(r.lon) : ""
|
|
96
|
+
const result: NominatimResult = {
|
|
97
|
+
place_id: r.placeID ?? stableID(`${lat},${lon},${displayName}`),
|
|
98
|
+
licence: MAILWOMAN_LICENCE,
|
|
99
|
+
lat,
|
|
100
|
+
lon,
|
|
101
|
+
display_name: displayName,
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (r.category != null) {
|
|
105
|
+
result.class = r.category
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if (r.type != null) {
|
|
109
|
+
result.type = r.type
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (r.importance != null) {
|
|
113
|
+
result.importance = r.importance
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (r.placeRank != null) {
|
|
117
|
+
result.place_rank = r.placeRank
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
if (r.boundingbox) {
|
|
121
|
+
result.boundingbox = r.boundingbox
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if (opts.addressdetails) {
|
|
125
|
+
result.address = r.address
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
return result
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Project a Nominatim result into a schema.org `Place` JSON-LD object (`format=jsonld`, #1052) — the OUTPUT-format
|
|
133
|
+
* projection. Reads the result's `address` breakdown (populated because the router forces `addressdetails` for
|
|
134
|
+
* `jsonld`) plus the coordinate, re-serializing the SAME resolved place. `streetAddress` is the plain
|
|
135
|
+
* house-number-first join (house_number + road); `addressCountry` is ISO-3166 alpha-2 (uppercased).
|
|
136
|
+
*/
|
|
137
|
+
export function nominatimResultToSchemaOrg(r: NominatimResult): SchemaOrgPlace {
|
|
138
|
+
const a = r.address ?? {}
|
|
139
|
+
const streetAddress = composeStreetAddress({ houseNumber: a.house_number, street: a.road })
|
|
140
|
+
|
|
141
|
+
return toSchemaOrg({
|
|
142
|
+
lat: r.lat ? Number(r.lat) : null,
|
|
143
|
+
lon: r.lon ? Number(r.lon) : null,
|
|
144
|
+
streetAddress: streetAddress || undefined,
|
|
145
|
+
locality: a.city ?? a.town ?? a.village,
|
|
146
|
+
region: a.state,
|
|
147
|
+
postalCode: a.postcode,
|
|
148
|
+
countryCode: a.country_code,
|
|
149
|
+
})
|
|
150
|
+
}
|
package/index.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright Sister Software
|
|
3
|
+
* @license AGPL-3.0
|
|
4
|
+
* @author Teffen Ellis, et al.
|
|
5
|
+
*
|
|
6
|
+
* `@mailwoman/nominatim` — a Nominatim-compatible HTTP geocoding API over the Mailwoman engine.
|
|
7
|
+
*
|
|
8
|
+
* The package is intentionally engine-agnostic: {@link createNominatimApp} takes a
|
|
9
|
+
* {@link NominatimEngine} (the thing that actually parses + resolves) and exposes it under the
|
|
10
|
+
* endpoint shapes + response format a Nominatim client expects. The CLI (`./cli.ts`) wires the
|
|
11
|
+
* real Mailwoman engine; tests can inject a fake. This keeps the compat surface isolated from the
|
|
12
|
+
* resolver wiring.
|
|
13
|
+
*
|
|
14
|
+
* Implementation is staged across the epic (#801): #804 the result formatter, #802 `/search`, #803
|
|
15
|
+
* `/reverse`, #805 `/lookup` + `/status`. Routes whose engine method is absent answer `501`
|
|
16
|
+
* (`/status` is the one exception — see `routes.ts`).
|
|
17
|
+
*
|
|
18
|
+
* The Hono app (CORS + error envelope + the emitted OpenAPI document) lives in `app.ts`; route
|
|
19
|
+
* definitions + handlers (incl. `registerNominatimRoutes`, exported from the package root as the
|
|
20
|
+
* phase-4 `mailwoman serve` carry-forward) in `routes.ts`; wire types + the engine contract in
|
|
21
|
+
* `engine.ts`; the RESOLVED-address → Nominatim-schema formatter in `format.ts`; the zod wire
|
|
22
|
+
* schemas in `schema.ts`.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
export * from "./app.ts"
|
|
26
|
+
export * from "./engine.ts"
|
|
27
|
+
export * from "./format.ts"
|
|
28
|
+
export * from "./routes.ts"
|
|
29
|
+
export * from "./schema.ts"
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mailwoman/nominatim",
|
|
3
|
-
"version": "7.2.
|
|
3
|
+
"version": "7.2.1",
|
|
4
4
|
"description": "Nominatim drop-in — a Nominatim-compatible HTTP geocoding API (search / reverse / lookup / status) over the Mailwoman engine. Run it with `npx @mailwoman/nominatim serve`.",
|
|
5
5
|
"license": "AGPL-3.0-only OR LicenseRef-Commercial",
|
|
6
6
|
"repository": {
|
|
@@ -14,36 +14,50 @@
|
|
|
14
14
|
"out/**/*.js.map",
|
|
15
15
|
"out/**/*.d.ts",
|
|
16
16
|
"out/**/*.d.ts.map",
|
|
17
|
-
"README.md"
|
|
17
|
+
"README.md",
|
|
18
|
+
"*.ts",
|
|
19
|
+
"*.tsx",
|
|
20
|
+
"**/*.ts",
|
|
21
|
+
"**/*.tsx",
|
|
22
|
+
"!*.test.ts",
|
|
23
|
+
"!*.test.tsx",
|
|
24
|
+
"!**/*.test.ts",
|
|
25
|
+
"!**/*.test.tsx"
|
|
18
26
|
],
|
|
19
27
|
"type": "module",
|
|
20
28
|
"exports": {
|
|
21
29
|
"./package.json": "./package.json",
|
|
22
30
|
".": {
|
|
23
|
-
"
|
|
24
|
-
"default": "./out/index.js"
|
|
25
|
-
"types": "./out/index.d.ts"
|
|
31
|
+
"types": "./out/index.d.ts",
|
|
32
|
+
"default": "./out/index.js"
|
|
26
33
|
}
|
|
27
34
|
},
|
|
28
35
|
"publishConfig": {
|
|
29
|
-
"access": "public"
|
|
36
|
+
"access": "public",
|
|
37
|
+
"exports": {
|
|
38
|
+
"./package.json": "./package.json",
|
|
39
|
+
".": {
|
|
40
|
+
"types": "./out/index.d.ts",
|
|
41
|
+
"default": "./out/index.js"
|
|
42
|
+
}
|
|
43
|
+
}
|
|
30
44
|
},
|
|
31
45
|
"dependencies": {
|
|
32
46
|
"@hono/zod-openapi": "^1.4.0",
|
|
33
|
-
"@mailwoman/annotations": "7.2.
|
|
34
|
-
"@mailwoman/api-kit": "7.2.
|
|
35
|
-
"@mailwoman/ban": "7.2.
|
|
36
|
-
"@mailwoman/codex": "7.2.
|
|
37
|
-
"@mailwoman/neural": "7.2.
|
|
38
|
-
"@mailwoman/neural-weights-en-us": "7.2.
|
|
39
|
-
"@mailwoman/nuts-lookup": "7.2.
|
|
40
|
-
"@mailwoman/resolver": "7.2.
|
|
41
|
-
"@mailwoman/resolver-wof-sqlite": "7.2.
|
|
42
|
-
"@mailwoman/spatial": "7.2.
|
|
43
|
-
"@mailwoman/timezone-lookup": "7.2.
|
|
44
|
-
"@mailwoman/un-locode-lookup": "7.2.
|
|
47
|
+
"@mailwoman/annotations": "7.2.1",
|
|
48
|
+
"@mailwoman/api-kit": "7.2.1",
|
|
49
|
+
"@mailwoman/ban": "7.2.1",
|
|
50
|
+
"@mailwoman/codex": "7.2.1",
|
|
51
|
+
"@mailwoman/neural": "7.2.1",
|
|
52
|
+
"@mailwoman/neural-weights-en-us": "7.2.1",
|
|
53
|
+
"@mailwoman/nuts-lookup": "7.2.1",
|
|
54
|
+
"@mailwoman/resolver": "7.2.1",
|
|
55
|
+
"@mailwoman/resolver-wof-sqlite": "7.2.1",
|
|
56
|
+
"@mailwoman/spatial": "7.2.1",
|
|
57
|
+
"@mailwoman/timezone-lookup": "7.2.1",
|
|
58
|
+
"@mailwoman/un-locode-lookup": "7.2.1",
|
|
45
59
|
"hono": "^4.12.29",
|
|
46
|
-
"mailwoman": "7.2.
|
|
60
|
+
"mailwoman": "7.2.1",
|
|
47
61
|
"zod": "^4.4.3"
|
|
48
62
|
}
|
|
49
63
|
}
|
package/routes.ts
ADDED
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright Sister Software
|
|
3
|
+
* @license AGPL-3.0
|
|
4
|
+
* @author Teffen Ellis, et al.
|
|
5
|
+
*
|
|
6
|
+
* Route definitions + handlers for the Nominatim-compatible surface. The OpenAPI document is
|
|
7
|
+
* emitted from these definitions — no handwritten spec. Handlers parse params from the
|
|
8
|
+
* `legacyQuery` express-shaped view; the zod query schemas drive only the emitted document.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { createRoute, type OpenAPIHono, z } from "@hono/zod-openapi"
|
|
12
|
+
import type { Context } from "hono"
|
|
13
|
+
|
|
14
|
+
import type {
|
|
15
|
+
NominatimEngine,
|
|
16
|
+
NominatimFormat,
|
|
17
|
+
NominatimLookupParams,
|
|
18
|
+
NominatimReverseParams,
|
|
19
|
+
NominatimSearchParams,
|
|
20
|
+
NominatimStatus,
|
|
21
|
+
} from "./engine.ts"
|
|
22
|
+
import { nominatimResultToSchemaOrg, toFeatureCollection } from "./format.ts"
|
|
23
|
+
import {
|
|
24
|
+
ErrorSchema,
|
|
25
|
+
lookupQueryParams,
|
|
26
|
+
NominatimLookupResponseSchema,
|
|
27
|
+
NominatimReverseResponseSchema,
|
|
28
|
+
NominatimSearchResponseSchema,
|
|
29
|
+
NominatimStatusSchema,
|
|
30
|
+
reverseQueryParams,
|
|
31
|
+
searchQueryParams,
|
|
32
|
+
} from "./schema.ts"
|
|
33
|
+
|
|
34
|
+
const DEFAULT_LIMIT = 10
|
|
35
|
+
|
|
36
|
+
function parseFormat(raw: unknown): NominatimFormat {
|
|
37
|
+
return raw === "geojson" || raw === "json" || raw === "jsonld" ? raw : "jsonv2"
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function parseBool(raw: unknown): boolean {
|
|
41
|
+
return raw === "1" || raw === "true"
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function asString(raw: unknown): string | undefined {
|
|
45
|
+
return typeof raw === "string" && raw.length > 0 ? raw : undefined
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Express's `req.query` shape: `string` for a single value, `string[]` for repeats. The legacy parsing helpers
|
|
50
|
+
* (`asString`, `parseFormat`, `parseBool`, `Number(...)`) — and their observable degenerate behaviors (repeated `q` →
|
|
51
|
+
* `asString(array)` → undefined → silently treated as absent, never a 400) — key off exactly this shape, so the
|
|
52
|
+
* handlers consume it unchanged. Null-prototype, matching express-simple's req.query shape (a repeated `?__proto__=`
|
|
53
|
+
* param must create an own property, not reparent the object).
|
|
54
|
+
*/
|
|
55
|
+
function legacyQuery(c: Context): Record<string, string | string[]> {
|
|
56
|
+
const out: Record<string, string | string[]> = Object.create(null)
|
|
57
|
+
|
|
58
|
+
for (const [key, values] of Object.entries(c.req.queries())) {
|
|
59
|
+
out[key] = values.length === 1 ? values[0]! : values
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return out
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* A friendly HTML landing page for `GET /` (#1022). Nominatim itself has no root page (just `/status`), so there's no
|
|
67
|
+
* wire contract to match — this is pure courtesy: a browser visitor who pastes the bare host in gets a one-glance
|
|
68
|
+
* orientation with clickable example queries instead of Express's `Cannot GET /` 404, which reads as "the service is
|
|
69
|
+
* broken". Relative example URLs so they resolve against whatever host/port serves this.
|
|
70
|
+
*/
|
|
71
|
+
const ROOT_HTML = `<!doctype html>
|
|
72
|
+
<html lang="en">
|
|
73
|
+
<head>
|
|
74
|
+
<meta charset="utf-8">
|
|
75
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
76
|
+
<title>@mailwoman/nominatim</title>
|
|
77
|
+
<style>
|
|
78
|
+
:root { color-scheme: light dark }
|
|
79
|
+
body { font: 16px/1.6 system-ui, -apple-system, sans-serif; max-width: 42rem; margin: 3rem auto; padding: 0 1.25rem }
|
|
80
|
+
h1 { font-size: 1.3rem; margin: 0 0 .5rem }
|
|
81
|
+
code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace }
|
|
82
|
+
ul { padding-left: 1.2rem }
|
|
83
|
+
li { margin: .4rem 0 }
|
|
84
|
+
a { color: #2563eb }
|
|
85
|
+
.q { font-family: ui-monospace, SFMono-Regular, Menlo, monospace }
|
|
86
|
+
footer { margin-top: 2rem; font-size: .9rem; opacity: .8 }
|
|
87
|
+
</style>
|
|
88
|
+
</head>
|
|
89
|
+
<body>
|
|
90
|
+
<h1>@mailwoman/nominatim</h1>
|
|
91
|
+
<p>A Nominatim-compatible geocoding API — the same <code>/search</code>, <code>/reverse</code>, <code>/lookup</code>, and <code>/status</code> contract, served from a SQLite gazetteer instead of a PostgreSQL/PostGIS import.</p>
|
|
92
|
+
<p>Try a query:</p>
|
|
93
|
+
<ul>
|
|
94
|
+
<li><a class="q" href="/search?q=berlin&format=jsonv2&limit=3">/search?q=berlin&format=jsonv2&limit=3</a></li>
|
|
95
|
+
<li><a class="q" href="/search?q=1600+pennsylvania+ave+washington+dc&addressdetails=1">/search?q=1600+pennsylvania+ave+washington+dc&addressdetails=1</a></li>
|
|
96
|
+
<li><a class="q" href="/reverse?lat=52.52&lon=13.405">/reverse?lat=52.52&lon=13.405</a></li>
|
|
97
|
+
</ul>
|
|
98
|
+
<footer><a href="https://mailwoman.sister.software/docs/concepts/switching-from-nominatim">Switching from Nominatim</a> · <a href="https://mailwoman.sister.software/demo">Live demo</a></footer>
|
|
99
|
+
</body>
|
|
100
|
+
</html>
|
|
101
|
+
`
|
|
102
|
+
|
|
103
|
+
const errorContent = (description: string) => ({
|
|
104
|
+
description,
|
|
105
|
+
content: { "application/json": { schema: ErrorSchema } },
|
|
106
|
+
})
|
|
107
|
+
|
|
108
|
+
const searchResponses = {
|
|
109
|
+
200: {
|
|
110
|
+
description: "A jsonv2 result array (or a geojson FeatureCollection / jsonld Place[] per `format`).",
|
|
111
|
+
content: { "application/json": { schema: NominatimSearchResponseSchema } },
|
|
112
|
+
},
|
|
113
|
+
500: errorContent("An unexpected engine fault. A clean JSON error, never a stack trace."),
|
|
114
|
+
501: errorContent("The backing engine method is not wired for this deployment."),
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const reverseResponses = {
|
|
118
|
+
200: {
|
|
119
|
+
description: "A single jsonv2 result (or `null`; a geojson FeatureCollection / jsonld Place per `format`).",
|
|
120
|
+
content: { "application/json": { schema: NominatimReverseResponseSchema } },
|
|
121
|
+
},
|
|
122
|
+
400: errorContent("Missing or out-of-range `lat`/`lon`."),
|
|
123
|
+
500: errorContent("An unexpected engine fault. A clean JSON error, never a stack trace."),
|
|
124
|
+
501: errorContent("The backing engine method is not wired for this deployment."),
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const lookupResponses = {
|
|
128
|
+
200: {
|
|
129
|
+
description: "A jsonv2 result array (or a geojson FeatureCollection when format=geojson).",
|
|
130
|
+
content: { "application/json": { schema: NominatimLookupResponseSchema } },
|
|
131
|
+
},
|
|
132
|
+
500: errorContent("An unexpected engine fault. A clean JSON error, never a stack trace."),
|
|
133
|
+
501: errorContent("The backing engine method is not wired for this deployment."),
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const statusResponses = {
|
|
137
|
+
200: {
|
|
138
|
+
description: 'Engine health. Absent `engine.status` answers `{status: 0, message: "OK"}` — never 501.',
|
|
139
|
+
content: { "application/json": { schema: NominatimStatusSchema } },
|
|
140
|
+
},
|
|
141
|
+
500: errorContent("An unexpected engine fault. A clean JSON error, never a stack trace."),
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const rootRoute = createRoute({
|
|
145
|
+
method: "get",
|
|
146
|
+
path: "/",
|
|
147
|
+
operationId: "getRoot",
|
|
148
|
+
summary: "Landing page",
|
|
149
|
+
tags: ["meta"],
|
|
150
|
+
responses: { 200: { description: "HTML landing page.", content: { "text/html": { schema: z.string() } } } },
|
|
151
|
+
})
|
|
152
|
+
|
|
153
|
+
const searchRoute = createRoute({
|
|
154
|
+
method: "get",
|
|
155
|
+
path: "/search",
|
|
156
|
+
operationId: "search",
|
|
157
|
+
summary: "Forward geocoding (free-text or structured)",
|
|
158
|
+
tags: ["geocoding"],
|
|
159
|
+
request: { query: searchQueryParams },
|
|
160
|
+
responses: searchResponses,
|
|
161
|
+
})
|
|
162
|
+
|
|
163
|
+
const reverseRoute = createRoute({
|
|
164
|
+
method: "get",
|
|
165
|
+
path: "/reverse",
|
|
166
|
+
operationId: "reverse",
|
|
167
|
+
summary: "Reverse geocoding",
|
|
168
|
+
tags: ["geocoding"],
|
|
169
|
+
request: { query: reverseQueryParams },
|
|
170
|
+
responses: reverseResponses,
|
|
171
|
+
})
|
|
172
|
+
|
|
173
|
+
const lookupRoute = createRoute({
|
|
174
|
+
method: "get",
|
|
175
|
+
path: "/lookup",
|
|
176
|
+
operationId: "lookup",
|
|
177
|
+
summary: "Look up places by OSM id",
|
|
178
|
+
tags: ["geocoding"],
|
|
179
|
+
request: { query: lookupQueryParams },
|
|
180
|
+
responses: lookupResponses,
|
|
181
|
+
})
|
|
182
|
+
|
|
183
|
+
const statusRoute = createRoute({
|
|
184
|
+
method: "get",
|
|
185
|
+
path: "/status",
|
|
186
|
+
operationId: "status",
|
|
187
|
+
summary: "Engine health",
|
|
188
|
+
tags: ["meta"],
|
|
189
|
+
responses: statusResponses,
|
|
190
|
+
})
|
|
191
|
+
|
|
192
|
+
/** Register the Nominatim-compatible routes against an injected engine. */
|
|
193
|
+
export function registerNominatimRoutes(app: OpenAPIHono, engine: NominatimEngine): void {
|
|
194
|
+
app.openapi(rootRoute, (c) => c.html(ROOT_HTML))
|
|
195
|
+
|
|
196
|
+
app.openapi(searchRoute, async (c) => {
|
|
197
|
+
if (!engine.search) return c.json({ error: "search not implemented (see #802)" }, 501)
|
|
198
|
+
const q = legacyQuery(c)
|
|
199
|
+
const params: NominatimSearchParams = {
|
|
200
|
+
q: asString(q["q"]),
|
|
201
|
+
street: asString(q["street"]),
|
|
202
|
+
city: asString(q["city"]),
|
|
203
|
+
county: asString(q["county"]),
|
|
204
|
+
state: asString(q["state"]),
|
|
205
|
+
country: asString(q["country"]),
|
|
206
|
+
postalcode: asString(q["postalcode"]),
|
|
207
|
+
countrycodes: asString(q["countrycodes"])?.split(","),
|
|
208
|
+
limit: Number(q["limit"] ?? DEFAULT_LIMIT) || DEFAULT_LIMIT,
|
|
209
|
+
bounded: parseBool(q["bounded"]),
|
|
210
|
+
// #1052: jsonld projects the address breakdown into a PostalAddress, so it needs the details block.
|
|
211
|
+
addressdetails: parseBool(q["addressdetails"]) || q["format"] === "jsonld",
|
|
212
|
+
format: parseFormat(q["format"]),
|
|
213
|
+
acceptLanguage: asString(q["accept-language"]),
|
|
214
|
+
}
|
|
215
|
+
const results = await engine.search(params)
|
|
216
|
+
|
|
217
|
+
if (params.format === "geojson") {
|
|
218
|
+
return c.json(toFeatureCollection(results) as never, 200)
|
|
219
|
+
} else if (params.format === "jsonld") {
|
|
220
|
+
// #1052: re-serialize the SAME results as schema.org `Place[]`; jsonv2 stays the default.
|
|
221
|
+
return c.json(results.map(nominatimResultToSchemaOrg) as never, 200)
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
return c.json(results as never, 200)
|
|
225
|
+
})
|
|
226
|
+
|
|
227
|
+
app.openapi(reverseRoute, async (c) => {
|
|
228
|
+
if (!engine.reverse) return c.json({ error: "reverse not implemented (see #803)" }, 501)
|
|
229
|
+
const q = legacyQuery(c)
|
|
230
|
+
const lat = Number(q["lat"])
|
|
231
|
+
const lon = Number(q["lon"])
|
|
232
|
+
|
|
233
|
+
if (!Number.isFinite(lat) || !Number.isFinite(lon)) {
|
|
234
|
+
return c.json({ error: "lat and lon are required" }, 400)
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
if (lat < -90 || lat > 90 || lon < -180 || lon > 180) {
|
|
238
|
+
return c.json({ error: "lat must be in [-90, 90] and lon in [-180, 180]" }, 400)
|
|
239
|
+
}
|
|
240
|
+
const params: NominatimReverseParams = {
|
|
241
|
+
lat,
|
|
242
|
+
lon,
|
|
243
|
+
zoom: q["zoom"] != null ? Number(q["zoom"]) : undefined,
|
|
244
|
+
// #1052: jsonld projects the address breakdown into a PostalAddress, so it needs the details block.
|
|
245
|
+
addressdetails: parseBool(q["addressdetails"]) || q["format"] === "jsonld",
|
|
246
|
+
format: parseFormat(q["format"]),
|
|
247
|
+
acceptLanguage: asString(q["accept-language"]),
|
|
248
|
+
}
|
|
249
|
+
const result = await engine.reverse(params)
|
|
250
|
+
|
|
251
|
+
if (params.format === "geojson") {
|
|
252
|
+
return c.json(toFeatureCollection(result ? [result] : []) as never, 200)
|
|
253
|
+
} else if (params.format === "jsonld") {
|
|
254
|
+
// #1052: a single reverse hit → one schema.org `Place` (or null when unresolved).
|
|
255
|
+
return c.json((result ? nominatimResultToSchemaOrg(result) : null) as never, 200)
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
return c.json(result as never, 200)
|
|
259
|
+
})
|
|
260
|
+
|
|
261
|
+
app.openapi(lookupRoute, async (c) => {
|
|
262
|
+
if (!engine.lookup) return c.json({ error: "lookup not implemented (see #805)" }, 501)
|
|
263
|
+
const q = legacyQuery(c)
|
|
264
|
+
const params: NominatimLookupParams = {
|
|
265
|
+
osmIds: asString(q["osm_ids"])?.split(",") ?? [],
|
|
266
|
+
addressdetails: parseBool(q["addressdetails"]),
|
|
267
|
+
format: parseFormat(q["format"]),
|
|
268
|
+
}
|
|
269
|
+
const results = await engine.lookup(params)
|
|
270
|
+
|
|
271
|
+
// NOTE: no jsonld branch here — a legacy quirk of the express handler, preserved verbatim. `format=jsonld`
|
|
272
|
+
// on `/lookup` falls through to the raw jsonv2 results, unlike `/search` and `/reverse`.
|
|
273
|
+
return c.json((params.format === "geojson" ? toFeatureCollection(results) : results) as never, 200)
|
|
274
|
+
})
|
|
275
|
+
|
|
276
|
+
app.openapi(statusRoute, async (c) => {
|
|
277
|
+
if (!engine.status) return c.json({ status: 0, message: "OK" } satisfies NominatimStatus, 200)
|
|
278
|
+
|
|
279
|
+
return c.json(await engine.status(), 200)
|
|
280
|
+
})
|
|
281
|
+
}
|