@mailwoman/ogc 0.0.0-stage → 11.3.0

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.
Files changed (82) hide show
  1. package/README.md +288 -2
  2. package/cli/geocoder.ts +50 -0
  3. package/cli/main.ts +407 -0
  4. package/lib/app.ts +107 -0
  5. package/lib/bbox.ts +69 -0
  6. package/lib/collections.ts +213 -0
  7. package/lib/engine.ts +143 -0
  8. package/lib/format.ts +46 -0
  9. package/lib/html.ts +298 -0
  10. package/lib/index.ts +24 -0
  11. package/lib/items.ts +169 -0
  12. package/lib/projection.ts +226 -0
  13. package/lib/provenance.ts +86 -0
  14. package/lib/refusal.ts +34 -0
  15. package/lib/routes.ts +852 -0
  16. package/lib/schema.ts +457 -0
  17. package/out/app.d.ts +41 -0
  18. package/out/app.d.ts.map +1 -0
  19. package/out/app.js +67 -0
  20. package/out/app.js.map +1 -0
  21. package/out/bbox.d.ts +23 -0
  22. package/out/bbox.d.ts.map +1 -0
  23. package/out/bbox.js +54 -0
  24. package/out/bbox.js.map +1 -0
  25. package/out/cli/geocoder.d.ts +24 -0
  26. package/out/cli/geocoder.d.ts.map +1 -0
  27. package/out/cli/geocoder.js +28 -0
  28. package/out/cli/geocoder.js.map +1 -0
  29. package/out/cli/main.d.ts +3 -0
  30. package/out/cli/main.d.ts.map +1 -0
  31. package/out/cli/main.js +297 -0
  32. package/out/cli/main.js.map +1 -0
  33. package/out/collections.d.ts +99 -0
  34. package/out/collections.d.ts.map +1 -0
  35. package/out/collections.js +132 -0
  36. package/out/collections.js.map +1 -0
  37. package/out/engine.d.ts +133 -0
  38. package/out/engine.d.ts.map +1 -0
  39. package/out/engine.js +4 -0
  40. package/out/engine.js.map +1 -0
  41. package/out/format.d.ts +18 -0
  42. package/out/format.d.ts.map +1 -0
  43. package/out/format.js +31 -0
  44. package/out/format.js.map +1 -0
  45. package/out/html.d.ts +52 -0
  46. package/out/html.d.ts.map +1 -0
  47. package/out/html.js +200 -0
  48. package/out/html.js.map +1 -0
  49. package/out/index.d.ts +21 -0
  50. package/out/index.d.ts.map +1 -0
  51. package/out/index.js +23 -0
  52. package/out/index.js.map +1 -0
  53. package/out/items.d.ts +38 -0
  54. package/out/items.d.ts.map +1 -0
  55. package/out/items.js +136 -0
  56. package/out/items.js.map +1 -0
  57. package/out/projection.d.ts +40 -0
  58. package/out/projection.d.ts.map +1 -0
  59. package/out/projection.js +181 -0
  60. package/out/projection.js.map +1 -0
  61. package/out/provenance.d.ts +41 -0
  62. package/out/provenance.d.ts.map +1 -0
  63. package/out/provenance.js +60 -0
  64. package/out/provenance.js.map +1 -0
  65. package/out/refusal.d.ts +18 -0
  66. package/out/refusal.d.ts.map +1 -0
  67. package/out/refusal.js +30 -0
  68. package/out/refusal.js.map +1 -0
  69. package/out/routes.d.ts +95 -0
  70. package/out/routes.d.ts.map +1 -0
  71. package/out/routes.js +624 -0
  72. package/out/routes.js.map +1 -0
  73. package/out/schema.d.ts +422 -0
  74. package/out/schema.d.ts.map +1 -0
  75. package/out/schema.js +378 -0
  76. package/out/schema.js.map +1 -0
  77. package/out/tools/ets.run.d.ts +2 -0
  78. package/out/tools/ets.run.d.ts.map +1 -0
  79. package/out/tools/ets.run.js +187 -0
  80. package/out/tools/ets.run.js.map +1 -0
  81. package/package.json +120 -3
  82. package/tools/ets.run.ts +258 -0
package/README.md CHANGED
@@ -1,3 +1,289 @@
1
- # Temporary Holding Version
1
+ # @mailwoman/ogc
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ An [OGC API](https://ogcapi.ogc.org) adapter for the [Mailwoman](https://mailwoman.post) engine. OGC API clients
4
+ such as QGIS, GDAL/OGR and OWSLib start from a service's landing page and its conformance declaration. This
5
+ package serves both, the API definition they link to, as described in
6
+ [OGC API Common Part 1](https://docs.ogc.org/is/19-072/19-072.html), and the collections of
7
+ [OGC API Common Part 2](https://www.opengis.net/doc/is/ogcapi-common-2/1.0). With `--serve-items`, the `places`
8
+ collection serves the gazetteer's places as items, as described in
9
+ [OGC API Features Part 1](https://docs.ogc.org/is/17-069r4/17-069r4.html), so QGIS, GDAL/OGR and other Features
10
+ clients can browse them. Forward geocoding is served at `/search`, in the form of the
11
+ [PDOK Location API](https://api.pdok.nl/kadaster/location-api/v1/).
12
+
13
+ ```sh
14
+ npx @mailwoman/ogc serve --port 8090
15
+ curl "http://localhost:8090/collections"
16
+ curl "http://localhost:8090/search?q=Dam%201%2C%201012JS%20Amsterdam&limit=2"
17
+
18
+ # Bulk browsing is opt-in.
19
+ npx @mailwoman/ogc serve --port 8090 --serve-items
20
+ curl "http://localhost:8090/collections/places/items?bbox=4.7,52.2,5.1,52.5&limit=5"
21
+ ```
22
+
23
+ `serve` loads the same engine as `@mailwoman/photon` and `@mailwoman/nominatim`: the en-US classifier, the
24
+ gazetteer, and the address-point, interpolation and street-centroid layers under `$MAILWOMAN_DATA_ROOT`. It
25
+ exits with a message naming the missing data when no gazetteer is found. Pass `--candidate-db <path>` to choose
26
+ a candidate gazetteer.
27
+
28
+ ## Endpoints
29
+
30
+ | Endpoint | What it returns |
31
+ | ----------------------------------------------- | ---------------------------------------------------------------------------------- |
32
+ | `/` | The landing page: a title, a description and links to every resource below |
33
+ | `/conformance` | `{ "conformsTo": [...] }`, the conformance classes the routes implement |
34
+ | `/api` | The API definition as OpenAPI 3.0 (`application/vnd.oai.openapi+json;version=3.0`) |
35
+ | `/collections` | `{ "links": [...], "collections": [...] }`, the collections the engine serves |
36
+ | `/collections/{collectionId}` | One collection, as `/collections` lists it, or a 404 for an unknown id |
37
+ | `/collections/{collectionId}/items` | One page of a collection's places, as a GeoJSON FeatureCollection |
38
+ | `/collections/{collectionId}/items/{featureId}` | One place, as a GeoJSON Feature |
39
+ | `/search` | Forward geocoding, as a GeoJSON FeatureCollection (`application/geo+json`) |
40
+ | `/openapi.json` | The same routes as OpenAPI 3.1, the document every Mailwoman HTTP surface serves |
41
+
42
+ Every resource accepts `f=json` or `f=html`. Without `f`, the `Accept` header decides, and JSON answers a
43
+ request that names neither. Any other `f` value, a query parameter the API definition doesn't name, or an
44
+ invalid value returns a 400 with the native error body, `{ "error": "invalid query parameter", "detail": "..." }`.
45
+
46
+ ## Collections
47
+
48
+ A collection groups the answers one kind of layer produces. The geocoder reports which tier placed each
49
+ coordinate, and the tier decides the collection:
50
+
51
+ | Collection | Tier | Served when |
52
+ | ------------ | ------------------------------------------------------------ | ------------------------------------------------------------- |
53
+ | `addresses` | `address_point`, `interpolated` | a region has an address-point or interpolation layer |
54
+ | `streets` | `street` | a region has a street-centroid layer |
55
+ | `postcodes` | `postcode`, and postcode places | the gazetteer holds postcode places |
56
+ | `places` | `admin`, and the other gazetteer places the resolver weighed | always |
57
+ | `plus-codes` | `plus_code` | always, because the engine decodes plus codes without a layer |
58
+
59
+ At startup the server asks each layer provider for every region it serves and lists `addresses` and `streets`
60
+ only when some region has a layer. Their descriptions name those regions, such as `FR` or `US-TX`, and the
61
+ startup banner prints them. A region absent from the list has no layer the engine opened. Its addresses may
62
+ still exist, and the list makes no claim about them.
63
+
64
+ Each collection reports `version: 1` and `geometryDimension: 0`, since every feature is a point. A collection
65
+ that serves items adds `itemType: "feature"`, an `items` link for GeoJSON, an `items` link for HTML and an `extent`.
66
+ The other collections omit all three item fields, and their items routes answer 404.
67
+
68
+ Every collection also links where its data comes from, read from the `layer_manifest` of each database behind it:
69
+
70
+ - A `license` link ([RFC 4946](https://www.rfc-editor.org/rfc/rfc4946)) for each identifier in the recorded
71
+ license expression whose text has a known URL: SPDX's page for an SPDX identifier, and Who's On First's own
72
+ license text for `LicenseRef-WhosOnFirst-Mixed`, because that text requires the link back.
73
+ - A `via` link ([RFC 4287](https://www.rfc-editor.org/rfc/rfc4287), "the source of the information") identifies each
74
+ source the manifest records, to the page where a user obtains it. Each source's location belongs to the package
75
+ that builds its layers: `GAZETTEER_SOURCE_LOCATIONS` in `@mailwoman/resolver-wof-sqlite/build/sources` for the
76
+ gazetteer, `BAN_SOURCE_LOCATIONS` in `@mailwoman/ban/source`, and `OSM_SOURCE_LOCATIONS` in
77
+ `@mailwoman/osm/source`. Each build writes its source names from the same table. The Who's On First entry points to
78
+ the [`mailwoman` organization](https://github.com/mailwoman). Its `whosonfirst-data-admin-<cc>` repositories are
79
+ the forks the build clones from `whosonfirst-data`. The OpenStreetMap entry points to the `@mailwoman/osm` build
80
+ documentation, because the project publishes the builder and a user builds the database.
81
+ - When a recorded license requires credit, the collection includes the `attribution` link that OGC API Common Part 2 defines.
82
+
83
+ A source that matches another opened layer, as the candidate gazetteer's manifest records the admin gazetteer,
84
+ uses that layer's license links, sources and credit. The address and street collections read the manifest
85
+ of every region file their providers report, and list each license and source once. If a manifest records no
86
+ license or source, a source has no known location, or a license identifier has no published text, the description
87
+ states that condition instead of linking to it. A region file without a readable `layer_manifest` adds no link,
88
+ and the description counts those regions. `plus-codes` has no layer.
89
+
90
+ ## Where the data comes from
91
+
92
+ The adapter serves only the operator's own data root, `$MAILWOMAN_DATA_ROOT`. `/search` answers one
93
+ query at a time. Bulk browsing through `items` is opt-in: `serve` serves no items unless it's started with
94
+ `--serve-items`.
95
+
96
+ A user who wants the data obtains it from its sources:
97
+
98
+ - The gazetteer: `mailwoman data pull candidate` installs the candidate gazetteer, as the
99
+ [data and locales reference](https://mailwoman.post/docs/reference/data-and-locales) describes. To build the admin
100
+ gazetteer from Who's On First yourself, clone the `whosonfirst-data-admin-<cc>` repositories from the
101
+ [`mailwoman` organization](https://github.com/mailwoman) or from `whosonfirst-data`, then run
102
+ `mailwoman gazetteer build admin`, as the
103
+ [reverse geocoding guide](https://mailwoman.post/docs/guides/reverse-geocode) shows.
104
+ - Address points, interpolation and street layers: `mailwoman data pull` installs the published bundles. The
105
+ data and locales reference lists them.
106
+ - Each source's own terms: the [data and licensing page](https://mailwoman.post/product/data-and-licensing) links
107
+ the publishers' pages. Each collection's `license` and `via` links identify what the opened databases record.
108
+ - Your own data root: `mailwoman data inventory` lists every database under it with its license, the
109
+ obligations imposed by that license, its sources and where to obtain each one, and the databases whose
110
+ `layer_manifest` it couldn't read. The collections' links come from the same function, `resolveLayerTerms` in
111
+ `@mailwoman/core/layers/provenance`, so the two agree.
112
+
113
+ ## Items
114
+
115
+ Items are served only when `serve` is started with `--serve-items`, or `createOGCApp` is built with
116
+ `serveItems: true`. While items are off, the collection descriptions link none, both items routes answer 404
117
+ with `{ "error": "collection serves no items" }`, `/search` features omit `href`, and `/conformance` lists
118
+ only the OGC API Common classes. The startup banner states whether items are served and gives the reason when they're not.
119
+
120
+ Two collections serve items:
121
+
122
+ - `places` reads the admin gazetteer's current places, the `admin-global-priority.db` the server resolves under
123
+ `$MAILWOMAN_DATA_ROOT/db/wof`, through its primary key and its `place_bbox` R*Tree.
124
+ - `postcodes` reads the candidate gazetteer's postcode places through its `candidate_postcode_place` index. The index
125
+ holds each postcode's id, point and name in id order. A candidate gazetteer built without that index serves no
126
+ postcode items, and the banner reports that absence. A server that resolves against WOF databases rather than a candidate
127
+ gazetteer serves no postcode items either. The candidate gazetteer's recorded license decides an obligation
128
+ refusal, as the admin gazetteer's does for `places`.
129
+
130
+ The other collections serve no items:
131
+
132
+ - `plus-codes`: the engine decodes plus codes and stores none.
133
+ - `addresses` and `streets`: out of scope for now.
134
+
135
+ | Parameter | Meaning |
136
+ | ---------- | ----------------------------------------------------------------------------------------------------------- |
137
+ | `limit` | The most features to return. The default is 10. A value above 1000 returns 1000 rather than an error. |
138
+ | `bbox` | `minLon,minLat,maxLon,maxLat`, or six numbers with a minimum and maximum height after each latitude. |
139
+ | `datetime` | An RFC 3339 date-time, or an interval of two separated by `/` with `..` or an empty string for an open end. |
140
+ | `after` | The cursor encoded by a `next` link. A client follows the link rather than building it. |
141
+ | `f` | `json` (GeoJSON) or `html`. |
142
+
143
+ A page lists places in ascending WOF id order. Its `next` link continues after the page's last id, so a deep page
144
+ costs what the first page costs, and following the links visits every place in the box once. A `postcodes` box
145
+ page walks the index in id order and keeps the postcodes whose point lies inside the box. A box with few postcodes
146
+ therefore reads more of the index before its page fills. On the 2026-11-21 candidate gazetteer, a box with no
147
+ postcodes read the whole index of 4,098,848 rows in 246 ms. The last page has no
148
+ `next` link. A response includes `timeStamp`, `numberReturned`, `self`, `alternate` and `collection` links, and
149
+ omits the optional `numberMatched`, because counting a large box's places costs a scan.
150
+
151
+ `bbox` keeps the places whose point lies inside the box, and a box whose minimum longitude exceeds its maximum
152
+ crosses the antimeridian. The places have no height, so a six-number box selects by its longitudes and latitudes.
153
+ The places have no temporal geometry, so every valid `datetime` selects every place, as Requirement 26 C of the
154
+ standard requires for features without a temporal geometry. A malformed value of either returns 400.
155
+
156
+ A feature's `id` is the gazetteer place ID `/search` reports as `place_id`, such as `wof:101751893`, and
157
+ `/collections/places/items/wof:101751893` returns that place. Its properties are `name`, `placetype`,
158
+ `country_code` and `display_name`, the label `/search` gives the same place. An id the collection doesn't hold
159
+ returns 404.
160
+
161
+ The items routes serve the gazetteer in bulk, so with `--serve-items` the server reads the database's recorded
162
+ license at startup and prints it. When `MAILWOMAN_REFUSE_OBLIGATIONS` refuses a class listed by that license, `places`
163
+ serves no items and the banner states the refusal. The database records `LicenseRef-WhosOnFirst-Mixed`
164
+ (attribution) beside `ODbL-1.0` (attribution and share-alike) and `CC-BY-4.0`. An operator who sets
165
+ `MAILWOMAN_REFUSE_OBLIGATIONS=share-alike` therefore serves no items, and one who sets `unresolved` serves them.
166
+
167
+ ## Search
168
+
169
+ | Parameter | Meaning |
170
+ | --------------------------- | -------------------------------------------------------------------------------------------------- |
171
+ | `q` | The address or place to search for. Required. |
172
+ | `<collectionId>[version]=1` | Restrict the search to the collections identified by the parameter. Repeat it for each collection. |
173
+ | `<collectionId>[relevance]` | A number from 0 to 1. It also selects the collection, and doesn't change the ranking. |
174
+ | `limit` | The most features to return, from 1 to 50. The default is 10. |
175
+ | `bbox` | `minLon,minLat,maxLon,maxLat`. Only features whose point lies inside the box are kept. |
176
+ | `f` | `json` (GeoJSON) or `html`. |
177
+
178
+ The response includes `timeStamp`, `links` (`self` and `alternate`) and `numberReturned` beside `features`.
179
+ Each feature's properties are:
180
+
181
+ - `display_name`: a one-line label, in the country's address order for an address or a street.
182
+ - `score`: the engine's ranking score for the match, larger ranking higher. Scores compare candidates for
183
+ one part of the input and have no unit across collections. A match the engine measured no score for, such as
184
+ an address point or a place without a recorded population, has `score: null`, and `score_unmeasured` names the
185
+ reason.
186
+ - `collection_id`: the collection the feature belongs to.
187
+ - `place_id` and `placetype`: the gazetteer place ID, such as `wof:85975673`, and placetype, such as `locality`
188
+ or `localadmin`, of a place or postcode. Two places that share a label, such as a locality and the
189
+ local-government area of the same name, differ here. Both are absent for an address, a street and a plus code,
190
+ and `place_id` is absent for a place the gazetteer gave no ID.
191
+ - `href`: the place's URL under `/collections/places/items`, as on PDOK. It's present only when the collection
192
+ serves items and holds the place, so the link never answers 404.
193
+
194
+ A place's `display_name` is its name, then the ancestors the gazetteer records between it and its country, most
195
+ specific first, then the country, such as `Camden, Ouachita, Arkansas, United States`. An ancestor that repeats
196
+ the place's name appears once. A place without a WOF place ID, or without a recorded ancestry, keeps its name and
197
+ country.
198
+
199
+ The first feature is the engine's answer. The places the resolver weighed for the same part of the input
200
+ follow. A box whose minimum longitude exceeds its maximum crosses the antimeridian. `f=html` without `q`
201
+ serves a search form. The landing page links that form as `search`.
202
+
203
+ ### Differences from PDOK
204
+
205
+ - A `q` without a collection parameter searches every collection and returns 200. PDOK answers 400, because
206
+ its index needs the collection. The engine has no such need.
207
+ - `<collectionId>[relevance]` is accepted so a PDOK client works unchanged, but it doesn't reweight the
208
+ ranking, because the engine's scores don't compare across collections.
209
+ - PDOK defines no reverse operation, so reverse geocoding stays on `@mailwoman/photon` and `@mailwoman/nominatim`.
210
+
211
+ ## Conformance
212
+
213
+ The declaration lists five OGC API Common Part 1 classes, each under
214
+ `http://www.opengis.net/spec/ogcapi-common-1/1.0/conf/`, and one Part 2 class:
215
+
216
+ - `core`: HTTP 1.1, and a 400 for an unknown query parameter or an invalid value.
217
+ - `landing-page`: the landing page links the API definition (`service-desc` and `service-doc`) and the
218
+ conformance declaration, and each link resolves.
219
+ - `json`: the landing page, conformance declaration and collections as `application/json`.
220
+ - `html`: the same resources as HTML5, each link an `<a>` element.
221
+ - `oas30`: an OpenAPI 3.0 definition as `service-desc` and its HTML form as `service-doc`.
222
+ - `http://www.opengis.net/spec/ogcapi-common-2/1.0/conf/collections`: the landing page links `/collections`
223
+ as `data`, and `/collections` and each `/collections/{collectionId}` include `self` and `alternate` links with
224
+ a `type`.
225
+
226
+ When a collection serves items, it adds four OGC API Features Part 1 classes, each under
227
+ `http://www.opengis.net/spec/ogcapi-features-1/1.0/conf/`:
228
+
229
+ - `core`: the two items routes with `limit`, `bbox` and `datetime`, `next` links, and the error codes above.
230
+ - `geojson`: both items routes as `application/geo+json`.
231
+ - `html`: both items routes as HTML5, each link an `<a>` element.
232
+ - `oas30`: the OpenAPI 3.0 definition declares both items routes and their parameters in the form the standard gives.
233
+
234
+ A server whose collections serve no items declares none of the four and registers no items routes.
235
+ `/search` is PDOK's convention. OGC publishes no conformance class for it, so the declaration lists none.
236
+
237
+ ## The OGC test suite
238
+
239
+ `packages/ogc/tools/ets.run.ts` runs OGC's executable test suite for Features Part 1,
240
+ [`ets-ogcapi-features10`](https://github.com/opengeospatial/ets-ogcapi-features10) revision 1.9, against a server on
241
+ this machine. It needs Podman and the data root's gazetteer. From the repository root:
242
+
243
+ ```sh
244
+ node packages/ogc/tools/ets.run.ts --out "$MAILWOMAN_DATA_ROOT/audits/ogc-ets/latest"
245
+ ```
246
+
247
+ The tool starts `mailwoman-ogc serve --serve-items` on 127.0.0.1 (port 8195 unless `--port` specifies another port),
248
+ because the suite needs a feature collection to test. It runs the suite
249
+ from OGC's published image `docker.io/ogccite/ets-ogcapi-features10:1.9-teamengine-6.0.0-RC2` with the host's
250
+ network, and stops the server it started. The container opens no port. The TestNG and EARL reports land under
251
+ `--out`, `summary.json` beside them lists every failed and skipped test, and the tool exits 1 when a test failed.
252
+ `--iut <url>` tests a running server instead, and the tool starts no server of its own.
253
+
254
+ ## Link hrefs behind a proxy
255
+
256
+ Link hrefs are absolute and resolve against the request's own origin. Behind a reverse proxy that rewrites
257
+ the host or mounts the service under a path, pass the public URL:
258
+
259
+ ```sh
260
+ npx @mailwoman/ogc serve --base-url https://example.org/ogc/
261
+ ```
262
+
263
+ ## Library use
264
+
265
+ `createOGCApp` takes an `OGCEngine`: the collections it serves and a `search(q)` that returns its matches.
266
+ `geocodeSearchHits` projects a `geocodeAddress` result to those matches. A collection serves items when it defines
267
+ an `items` source, such as `placeItemSource(new WOFPlaceBrowser({ databasePath }), ancestorsOf)`, with
268
+ `WOFPlaceBrowser` from `@mailwoman/resolver-wof-sqlite/lookup/browse`, and the app is built with `serveItems: true`.
269
+ A collection's `layers` lists the manifest terms its license and `via` links come from.
270
+
271
+ ```ts
272
+ import { serveNode } from "@mailwoman/api-kit"
273
+ import { createOGCApp, geocodeSearchHits, OGCCollectionID } from "@mailwoman/ogc"
274
+
275
+ const app = createOGCApp(
276
+ {
277
+ collections: [{ id: OGCCollectionID.Places, regions: null }],
278
+ search: async (q) => geocodeSearchHits(await geocode(q)),
279
+ },
280
+ { baseURL: "https://example.org/ogc/" }
281
+ )
282
+
283
+ serveNode({ fetch: app.fetch, port: 8090, hostname: "0.0.0.0" })
284
+ ```
285
+
286
+ ## CORS
287
+
288
+ The server sends `Access-Control-Allow-Origin: *` and answers preflight `OPTIONS` by default. Turn it off
289
+ with `--no-cors`, or `createOGCApp(engine, { cors: false })`, where a reverse proxy already sets the headers.
@@ -0,0 +1,50 @@
1
+ // SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
2
+ // SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
3
+
4
+ /**
5
+ * The forward geocoder behind `mailwoman-ogc serve`: the data layers the server composes,
6
+ * and the geocode of one query through its session.
7
+ *
8
+ * The module is separate from `main.ts` because that file runs the command line when it loads.
9
+ */
10
+
11
+ import type { GeocodeResult } from "@mailwoman/core/geocode"
12
+ import { type DropInGeocoder, openDropInGeocoder } from "mailwoman/cli/kit/dropin"
13
+ import type { LayerContribution } from "mailwoman/geocode/layers/contributions"
14
+ import { banLayer, forkEntityProbe, observationRoutes, osmLayer, regionDatabases } from "mailwoman/services/entry/node"
15
+
16
+ /**
17
+ * The data layers the OGC server composes.
18
+ *
19
+ * The server consults the national open register (BAN) and then the OpenStreetMap tier,
20
+ * as the Photon and Nominatim servers do.
21
+ * It leaves national Overture out, so `/search` reads every collection it lists.
22
+ */
23
+ const OGC_LAYERS: readonly LayerContribution[] = [
24
+ banLayer,
25
+ osmLayer,
26
+ regionDatabases,
27
+ forkEntityProbe,
28
+ observationRoutes,
29
+ ]
30
+
31
+ /**
32
+ * What `mailwoman-ogc serve` opens to answer `/search` and to list its collections.
33
+ */
34
+ export interface OGCGeocoder extends DropInGeocoder {
35
+ /**
36
+ * Geocodes one query.
37
+ */
38
+ geocode(query: string): Promise<GeocodeResult>
39
+ }
40
+
41
+ /**
42
+ * Opens the gazetteer and the server's session, and ends the process when either is unavailable.
43
+ *
44
+ * @param candidateDBFlag The `--candidate-db` value, or `null` to read the conventional path.
45
+ */
46
+ export async function openOGCGeocoder(candidateDBFlag: string | null): Promise<OGCGeocoder> {
47
+ const opened = await openDropInGeocoder(candidateDBFlag, OGC_LAYERS)
48
+
49
+ return { ...opened, geocode: async (query) => (await opened.session.geocode(query)).result }
50
+ }