@mailwoman/ogc 0.0.0-stage → 11.2.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.
- package/README.md +149 -2
- package/cli/main.ts +153 -0
- package/lib/app.ts +99 -0
- package/lib/collections.ts +159 -0
- package/lib/engine.ts +71 -0
- package/lib/format.ts +46 -0
- package/lib/html.ts +236 -0
- package/lib/index.ts +20 -0
- package/lib/projection.ts +206 -0
- package/lib/routes.ts +587 -0
- package/lib/schema.ts +296 -0
- package/out/app.d.ts +33 -0
- package/out/app.d.ts.map +1 -0
- package/out/app.js +67 -0
- package/out/app.js.map +1 -0
- package/out/cli/main.d.ts +3 -0
- package/out/cli/main.d.ts.map +1 -0
- package/out/cli/main.js +118 -0
- package/out/cli/main.js.map +1 -0
- package/out/collections.d.ts +72 -0
- package/out/collections.d.ts.map +1 -0
- package/out/collections.js +103 -0
- package/out/collections.js.map +1 -0
- package/out/engine.d.ts +65 -0
- package/out/engine.d.ts.map +1 -0
- package/out/engine.js +4 -0
- package/out/engine.js.map +1 -0
- package/out/format.d.ts +18 -0
- package/out/format.d.ts.map +1 -0
- package/out/format.js +31 -0
- package/out/format.js.map +1 -0
- package/out/html.d.ts +42 -0
- package/out/html.d.ts.map +1 -0
- package/out/html.js +153 -0
- package/out/html.js.map +1 -0
- package/out/index.d.ts +17 -0
- package/out/index.d.ts.map +1 -0
- package/out/index.js +19 -0
- package/out/index.js.map +1 -0
- package/out/projection.d.ts +26 -0
- package/out/projection.d.ts.map +1 -0
- package/out/projection.js +168 -0
- package/out/projection.js.map +1 -0
- package/out/routes.d.ts +93 -0
- package/out/routes.d.ts.map +1 -0
- package/out/routes.js +429 -0
- package/out/routes.js.map +1 -0
- package/out/schema.d.ts +267 -0
- package/out/schema.d.ts.map +1 -0
- package/out/schema.js +241 -0
- package/out/schema.js.map +1 -0
- package/package.json +98 -3
package/lib/routes.ts
ADDED
|
@@ -0,0 +1,587 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
|
|
2
|
+
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Route definitions and handlers for the OGC API resources.
|
|
6
|
+
*
|
|
7
|
+
* The API definition is emitted from these definitions, so no handwritten spec exists.
|
|
8
|
+
* Every resource validates its query against a strict schema, and the app's
|
|
9
|
+
* validation hook answers a failure with a 400.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { createRoute, type OpenAPIHono, z } from "@hono/zod-openapi"
|
|
13
|
+
import { APIErrorSchema, emitOpenAPIDocuments, errorResponse, type OpenAPIDocInfo } from "@mailwoman/api-kit"
|
|
14
|
+
import { MAX_ADDRESS_LENGTH } from "@mailwoman/api/input-limits"
|
|
15
|
+
import { stringifyJSON } from "@mailwoman/core/json"
|
|
16
|
+
import { isoSeconds } from "@mailwoman/core/utils"
|
|
17
|
+
import { isValidLatitude, isValidLongitude } from "@mailwoman/spatial/coordinate/bounds"
|
|
18
|
+
import type { Context } from "hono"
|
|
19
|
+
|
|
20
|
+
import { OGC_COLLECTION_CATALOG } from "#collections"
|
|
21
|
+
import type { OGCEngine, OGCSearchHit, OGCServedCollection } from "#engine"
|
|
22
|
+
import { negotiateFormat } from "#format"
|
|
23
|
+
import {
|
|
24
|
+
apiDefinitionHTML,
|
|
25
|
+
collectionHTML,
|
|
26
|
+
collectionsHTML,
|
|
27
|
+
conformanceHTML,
|
|
28
|
+
landingPageHTML,
|
|
29
|
+
type OpenAPI30Document,
|
|
30
|
+
searchHTML,
|
|
31
|
+
} from "#html"
|
|
32
|
+
import {
|
|
33
|
+
collectionRelevanceParameter,
|
|
34
|
+
collectionVersionParameter,
|
|
35
|
+
type OGCCollection,
|
|
36
|
+
OGCCollectionPathSchema,
|
|
37
|
+
type OGCCollections,
|
|
38
|
+
OGCCollectionSchema,
|
|
39
|
+
OGCCollectionsSchema,
|
|
40
|
+
OGC_COLLECTION_VERSION,
|
|
41
|
+
type OGCConformance,
|
|
42
|
+
OGCConformanceClass,
|
|
43
|
+
OGCConformanceSchema,
|
|
44
|
+
OGCFormat,
|
|
45
|
+
OGCFormatQuerySchema,
|
|
46
|
+
type OGCLandingPage,
|
|
47
|
+
OGCLandingPageSchema,
|
|
48
|
+
type OGCLink,
|
|
49
|
+
OGCLinkRelation,
|
|
50
|
+
OGCMediaType,
|
|
51
|
+
type OGCSearchFeature,
|
|
52
|
+
type OGCSearchResponse,
|
|
53
|
+
OGCSearchResponseSchema,
|
|
54
|
+
OGC_SEARCH_DEFAULT_LIMIT,
|
|
55
|
+
OGC_SEARCH_MAX_LIMIT,
|
|
56
|
+
searchQuerySchema,
|
|
57
|
+
} from "#schema"
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* What the routes need to know about the deployment.
|
|
61
|
+
*/
|
|
62
|
+
export interface OGCRouteOptions {
|
|
63
|
+
/**
|
|
64
|
+
* The service's public base URL, such as `https://example.org/ogc/`.
|
|
65
|
+
*
|
|
66
|
+
* Link hrefs resolve against it.
|
|
67
|
+
* Without it they resolve against the request's own origin.
|
|
68
|
+
*
|
|
69
|
+
* A reverse proxy that rewrites the host or mounts the service under a path needs it set.
|
|
70
|
+
*/
|
|
71
|
+
baseURL?: string
|
|
72
|
+
/**
|
|
73
|
+
* The landing page's title and description, and the API definition's `info` block.
|
|
74
|
+
*/
|
|
75
|
+
docInfo: OpenAPIDocInfo
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* The conformance classes the routes below implement.
|
|
80
|
+
*/
|
|
81
|
+
export const OGC_CONFORMANCE: OGCConformance = {
|
|
82
|
+
conformsTo: Object.values(OGCConformanceClass),
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const errorContent = { [OGCMediaType.JSON]: { schema: APIErrorSchema } }
|
|
86
|
+
|
|
87
|
+
const errorResponses = {
|
|
88
|
+
400: {
|
|
89
|
+
description: "An unknown query parameter, or an invalid parameter value.",
|
|
90
|
+
content: errorContent,
|
|
91
|
+
},
|
|
92
|
+
500: {
|
|
93
|
+
description: "An unexpected server fault.",
|
|
94
|
+
content: errorContent,
|
|
95
|
+
},
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const htmlContent = { [OGCMediaType.HTML]: { schema: z.string() } }
|
|
99
|
+
|
|
100
|
+
const landingRoute = createRoute({
|
|
101
|
+
method: "get",
|
|
102
|
+
path: "/",
|
|
103
|
+
operationId: "getLandingPage",
|
|
104
|
+
summary: "Landing page",
|
|
105
|
+
description: "Links to the API definition, the conformance declaration, the collections and the search.",
|
|
106
|
+
tags: ["capabilities"],
|
|
107
|
+
request: { query: OGCFormatQuerySchema },
|
|
108
|
+
responses: {
|
|
109
|
+
200: {
|
|
110
|
+
description: "The landing page.",
|
|
111
|
+
content: { [OGCMediaType.JSON]: { schema: OGCLandingPageSchema }, ...htmlContent },
|
|
112
|
+
},
|
|
113
|
+
...errorResponses,
|
|
114
|
+
},
|
|
115
|
+
})
|
|
116
|
+
|
|
117
|
+
const conformanceRoute = createRoute({
|
|
118
|
+
method: "get",
|
|
119
|
+
path: "/conformance",
|
|
120
|
+
operationId: "getConformanceDeclaration",
|
|
121
|
+
summary: "Conformance declaration",
|
|
122
|
+
description: "The OGC API conformance classes this service implements.",
|
|
123
|
+
tags: ["capabilities"],
|
|
124
|
+
request: { query: OGCFormatQuerySchema },
|
|
125
|
+
responses: {
|
|
126
|
+
200: {
|
|
127
|
+
description: "The conformance declaration.",
|
|
128
|
+
content: { [OGCMediaType.JSON]: { schema: OGCConformanceSchema }, ...htmlContent },
|
|
129
|
+
},
|
|
130
|
+
...errorResponses,
|
|
131
|
+
},
|
|
132
|
+
})
|
|
133
|
+
|
|
134
|
+
const apiRoute = createRoute({
|
|
135
|
+
method: "get",
|
|
136
|
+
path: "/api",
|
|
137
|
+
operationId: "getAPIDefinition",
|
|
138
|
+
summary: "API definition",
|
|
139
|
+
description: "This document, as OpenAPI 3.0 JSON or as HTML.",
|
|
140
|
+
tags: ["capabilities"],
|
|
141
|
+
request: { query: OGCFormatQuerySchema },
|
|
142
|
+
responses: {
|
|
143
|
+
200: {
|
|
144
|
+
description: "The API definition.",
|
|
145
|
+
content: { [OGCMediaType.OpenAPI30]: { schema: z.object({}).loose() }, ...htmlContent },
|
|
146
|
+
},
|
|
147
|
+
...errorResponses,
|
|
148
|
+
},
|
|
149
|
+
})
|
|
150
|
+
|
|
151
|
+
const collectionsRoute = createRoute({
|
|
152
|
+
method: "get",
|
|
153
|
+
path: "/collections",
|
|
154
|
+
operationId: "getCollections",
|
|
155
|
+
summary: "Collections",
|
|
156
|
+
description: "The collections the running engine serves, one for each kind of layer it opened.",
|
|
157
|
+
tags: ["data"],
|
|
158
|
+
request: { query: OGCFormatQuerySchema },
|
|
159
|
+
responses: {
|
|
160
|
+
200: {
|
|
161
|
+
description: "The collections.",
|
|
162
|
+
content: { [OGCMediaType.JSON]: { schema: OGCCollectionsSchema }, ...htmlContent },
|
|
163
|
+
},
|
|
164
|
+
...errorResponses,
|
|
165
|
+
},
|
|
166
|
+
})
|
|
167
|
+
|
|
168
|
+
const collectionRoute = createRoute({
|
|
169
|
+
method: "get",
|
|
170
|
+
path: "/collections/{collectionId}",
|
|
171
|
+
operationId: "describeCollection",
|
|
172
|
+
summary: "Collection description",
|
|
173
|
+
description: "One collection, as `/collections` lists it.",
|
|
174
|
+
tags: ["data"],
|
|
175
|
+
request: { params: OGCCollectionPathSchema, query: OGCFormatQuerySchema },
|
|
176
|
+
responses: {
|
|
177
|
+
200: {
|
|
178
|
+
description: "The collection description.",
|
|
179
|
+
content: { [OGCMediaType.JSON]: { schema: OGCCollectionSchema }, ...htmlContent },
|
|
180
|
+
},
|
|
181
|
+
404: { description: "No collection has this id.", content: errorContent },
|
|
182
|
+
...errorResponses,
|
|
183
|
+
},
|
|
184
|
+
})
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Resolve a service path against the deployment's base URL.
|
|
188
|
+
*
|
|
189
|
+
* @param path A path relative to the service root, without a leading slash.
|
|
190
|
+
*/
|
|
191
|
+
function hrefOf(base: string, path: string, format: OGCFormat, query: Iterable<[string, string]> = []): string {
|
|
192
|
+
const url = new URL(path, base.endsWith("/") ? base : `${base}/`)
|
|
193
|
+
|
|
194
|
+
for (const [key, value] of query) {
|
|
195
|
+
url.searchParams.append(key, value)
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
url.searchParams.set("f", format)
|
|
199
|
+
|
|
200
|
+
return url.href
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
function otherFormat(format: OGCFormat): OGCFormat {
|
|
204
|
+
return format === OGCFormat.HTML ? OGCFormat.JSON : OGCFormat.HTML
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
function mediaTypeOf(format: OGCFormat, json: OGCMediaType = OGCMediaType.JSON): OGCMediaType {
|
|
208
|
+
return format === OGCFormat.HTML ? OGCMediaType.HTML : json
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* The `self` and `alternate` links of a resource served in `format`.
|
|
213
|
+
*
|
|
214
|
+
* @param json The media type the resource's JSON form is served as.
|
|
215
|
+
*/
|
|
216
|
+
function selfLinks(
|
|
217
|
+
base: string,
|
|
218
|
+
path: string,
|
|
219
|
+
format: OGCFormat,
|
|
220
|
+
title: string,
|
|
221
|
+
options: { query?: ReadonlyArray<[string, string]>; json?: OGCMediaType } = {}
|
|
222
|
+
): OGCLink[] {
|
|
223
|
+
const alternate = otherFormat(format)
|
|
224
|
+
|
|
225
|
+
return [
|
|
226
|
+
{
|
|
227
|
+
href: hrefOf(base, path, format, options.query),
|
|
228
|
+
rel: OGCLinkRelation.Self,
|
|
229
|
+
type: mediaTypeOf(format, options.json),
|
|
230
|
+
title,
|
|
231
|
+
},
|
|
232
|
+
{
|
|
233
|
+
href: hrefOf(base, path, alternate, options.query),
|
|
234
|
+
rel: OGCLinkRelation.Alternate,
|
|
235
|
+
type: mediaTypeOf(alternate, options.json),
|
|
236
|
+
title: `${title} as ${alternate.toUpperCase()}`,
|
|
237
|
+
},
|
|
238
|
+
]
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* The landing page for a request served in `format`.
|
|
243
|
+
*/
|
|
244
|
+
export function buildLandingPage(base: string, docInfo: OpenAPIDocInfo, format: OGCFormat): OGCLandingPage {
|
|
245
|
+
const conformanceHref = hrefOf(base, "conformance", OGCFormat.JSON)
|
|
246
|
+
const conformanceTitle = "Conformance declaration"
|
|
247
|
+
const collectionsHref = hrefOf(base, "collections", OGCFormat.JSON)
|
|
248
|
+
const collectionsTitle = "Collections"
|
|
249
|
+
|
|
250
|
+
return {
|
|
251
|
+
title: docInfo.title,
|
|
252
|
+
description: docInfo.description ?? docInfo.title,
|
|
253
|
+
links: [
|
|
254
|
+
...selfLinks(base, "", format, "This document"),
|
|
255
|
+
{
|
|
256
|
+
href: hrefOf(base, "api", OGCFormat.JSON),
|
|
257
|
+
rel: OGCLinkRelation.ServiceDesc,
|
|
258
|
+
type: OGCMediaType.OpenAPI30,
|
|
259
|
+
title: "API definition (OpenAPI 3.0)",
|
|
260
|
+
},
|
|
261
|
+
{
|
|
262
|
+
href: hrefOf(base, "api", OGCFormat.HTML),
|
|
263
|
+
rel: OGCLinkRelation.ServiceDoc,
|
|
264
|
+
type: OGCMediaType.HTML,
|
|
265
|
+
title: "API documentation",
|
|
266
|
+
},
|
|
267
|
+
{ href: conformanceHref, rel: OGCLinkRelation.Conformance, type: OGCMediaType.JSON, title: conformanceTitle },
|
|
268
|
+
{ href: conformanceHref, rel: OGCLinkRelation.OGCConformance, type: OGCMediaType.JSON, title: conformanceTitle },
|
|
269
|
+
{ href: collectionsHref, rel: OGCLinkRelation.Data, type: OGCMediaType.JSON, title: collectionsTitle },
|
|
270
|
+
{ href: collectionsHref, rel: OGCLinkRelation.OGCData, type: OGCMediaType.JSON, title: collectionsTitle },
|
|
271
|
+
{
|
|
272
|
+
href: hrefOf(base, "search", OGCFormat.HTML),
|
|
273
|
+
rel: OGCLinkRelation.Search,
|
|
274
|
+
type: OGCMediaType.HTML,
|
|
275
|
+
title: "Search form (forward geocoding)",
|
|
276
|
+
},
|
|
277
|
+
],
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* The description of one collection the engine serves, in the form `/collections`
|
|
283
|
+
* and `/collections/{collectionId}` both serve, so the two can't disagree.
|
|
284
|
+
*/
|
|
285
|
+
export function buildCollection(base: string, served: OGCServedCollection, format: OGCFormat): OGCCollection {
|
|
286
|
+
const { title, description } = OGC_COLLECTION_CATALOG[served.id]
|
|
287
|
+
const coverage = served.regions ? ` The engine opened its layers for ${served.regions.join(", ")}.` : ""
|
|
288
|
+
|
|
289
|
+
return {
|
|
290
|
+
id: served.id,
|
|
291
|
+
title,
|
|
292
|
+
description: `${description}${coverage}`,
|
|
293
|
+
links: selfLinks(base, `collections/${encodeURIComponent(served.id)}`, format, title),
|
|
294
|
+
geometryDimension: 0,
|
|
295
|
+
version: OGC_COLLECTION_VERSION,
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* The collections list for a request served in `format`.
|
|
301
|
+
*/
|
|
302
|
+
export function buildCollections(
|
|
303
|
+
base: string,
|
|
304
|
+
collections: readonly OGCServedCollection[],
|
|
305
|
+
format: OGCFormat
|
|
306
|
+
): OGCCollections {
|
|
307
|
+
return {
|
|
308
|
+
links: selfLinks(base, "collections", format, "Collections"),
|
|
309
|
+
collections: collections.map((served) => buildCollection(base, served, format)),
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
* A `[minLon, minLat, maxLon, maxLat]` box in WGS 84 degrees.
|
|
315
|
+
*/
|
|
316
|
+
export type OGCBBox = readonly [number, number, number, number]
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* The count of numbers in a two-dimensional `bbox`: two longitudes and two latitudes.
|
|
320
|
+
*/
|
|
321
|
+
const BBOX_CORNERS = 4
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* Parse a `bbox` value.
|
|
325
|
+
*
|
|
326
|
+
* @returns The box, or a message that states the problem with the value.
|
|
327
|
+
*/
|
|
328
|
+
export function parseBBox(raw: string): OGCBBox | string {
|
|
329
|
+
const parts = raw.split(",").map((part) => part.trim())
|
|
330
|
+
const numbers = parts.map((part) => (part === "" ? Number.NaN : Number(part)))
|
|
331
|
+
|
|
332
|
+
if (numbers.length !== BBOX_CORNERS || !numbers.every(Number.isFinite)) {
|
|
333
|
+
return "bbox: four comma-separated numbers are required, minLon,minLat,maxLon,maxLat"
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
const [minLon, minLat, maxLon, maxLat] = numbers as [number, number, number, number]
|
|
337
|
+
|
|
338
|
+
if (!isValidLongitude(minLon) || !isValidLongitude(maxLon)) return "bbox: longitudes must lie in [-180, 180]"
|
|
339
|
+
|
|
340
|
+
if (!isValidLatitude(minLat) || !isValidLatitude(maxLat)) return "bbox: latitudes must lie in [-90, 90]"
|
|
341
|
+
|
|
342
|
+
if (minLat > maxLat) return "bbox: the minimum latitude exceeds the maximum"
|
|
343
|
+
|
|
344
|
+
return [minLon, minLat, maxLon, maxLat]
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
/**
|
|
348
|
+
* Whether a point lies inside the box, edges included.
|
|
349
|
+
*
|
|
350
|
+
* A box whose minimum longitude exceeds its maximum crosses the antimeridian, as OGC API boxes may.
|
|
351
|
+
*/
|
|
352
|
+
export function searchBoxContains([minLon, minLat, maxLon, maxLat]: OGCBBox, lon: number, lat: number): boolean {
|
|
353
|
+
if (lat < minLat || lat > maxLat) return false
|
|
354
|
+
|
|
355
|
+
return minLon <= maxLon ? lon >= minLon && lon <= maxLon : lon >= minLon || lon <= maxLon
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
/**
|
|
359
|
+
* A validated `/search` request.
|
|
360
|
+
*/
|
|
361
|
+
export interface OGCSearchRequest {
|
|
362
|
+
q: string
|
|
363
|
+
limit: number
|
|
364
|
+
bbox: OGCBBox | null
|
|
365
|
+
/**
|
|
366
|
+
* The collections the request names, or null when it names none and every collection is searched.
|
|
367
|
+
*/
|
|
368
|
+
collections: ReadonlySet<string> | null
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
/**
|
|
372
|
+
* Validate a `/search` query the schema admitted.
|
|
373
|
+
*
|
|
374
|
+
* @returns The request, or a message that identifies the invalid parameter.
|
|
375
|
+
*/
|
|
376
|
+
export function parseSearchQuery(
|
|
377
|
+
query: Readonly<Record<string, string | undefined>>,
|
|
378
|
+
collections: readonly OGCServedCollection[]
|
|
379
|
+
): OGCSearchRequest | string {
|
|
380
|
+
const q = query["q"]?.trim()
|
|
381
|
+
|
|
382
|
+
if (!q) return "q: a non-empty query is required"
|
|
383
|
+
|
|
384
|
+
if (q.length > MAX_ADDRESS_LENGTH) return `q: the query exceeds ${MAX_ADDRESS_LENGTH} characters`
|
|
385
|
+
|
|
386
|
+
let limit = OGC_SEARCH_DEFAULT_LIMIT
|
|
387
|
+
const rawLimit = query["limit"]
|
|
388
|
+
|
|
389
|
+
if (rawLimit != null) {
|
|
390
|
+
limit = /^\d+$/.test(rawLimit) ? Number(rawLimit) : Number.NaN
|
|
391
|
+
|
|
392
|
+
if (!(limit >= 1 && limit <= OGC_SEARCH_MAX_LIMIT)) {
|
|
393
|
+
return `limit: an integer from 1 to ${OGC_SEARCH_MAX_LIMIT} is required`
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
let bbox: OGCBBox | null = null
|
|
398
|
+
const rawBBox = query["bbox"]
|
|
399
|
+
|
|
400
|
+
if (rawBBox != null) {
|
|
401
|
+
const parsed = parseBBox(rawBBox)
|
|
402
|
+
|
|
403
|
+
if (typeof parsed === "string") return parsed
|
|
404
|
+
|
|
405
|
+
bbox = parsed
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
const named = new Set<string>()
|
|
409
|
+
|
|
410
|
+
for (const { id } of collections) {
|
|
411
|
+
const version = query[collectionVersionParameter(id)]
|
|
412
|
+
const relevance = query[collectionRelevanceParameter(id)]
|
|
413
|
+
|
|
414
|
+
if (version != null) {
|
|
415
|
+
if (version !== String(OGC_COLLECTION_VERSION)) {
|
|
416
|
+
return `${collectionVersionParameter(id)}: the collection's version is ${OGC_COLLECTION_VERSION}`
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
named.add(id)
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
if (relevance != null) {
|
|
423
|
+
const weight = relevance.trim() === "" ? Number.NaN : Number(relevance)
|
|
424
|
+
|
|
425
|
+
if (!(weight >= 0 && weight <= 1)) {
|
|
426
|
+
return `${collectionRelevanceParameter(id)}: a number from 0 to 1 is required`
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
named.add(id)
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
return { q, limit, bbox, collections: named.size ? named : null }
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
/**
|
|
437
|
+
* Keep the hits the request's collections and box admit, up to its limit.
|
|
438
|
+
*/
|
|
439
|
+
export function selectHits(hits: readonly OGCSearchHit[], request: OGCSearchRequest): OGCSearchHit[] {
|
|
440
|
+
return hits
|
|
441
|
+
.filter((hit) => !request.collections || request.collections.has(hit.collectionID))
|
|
442
|
+
.filter((hit) => !request.bbox || searchBoxContains(request.bbox, hit.lon, hit.lat))
|
|
443
|
+
.slice(0, request.limit)
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
/**
|
|
447
|
+
* One hit as a GeoJSON feature.
|
|
448
|
+
*/
|
|
449
|
+
export function searchFeature(hit: OGCSearchHit): OGCSearchFeature {
|
|
450
|
+
return {
|
|
451
|
+
type: "Feature",
|
|
452
|
+
geometry: { type: "Point", coordinates: [hit.lon, hit.lat] },
|
|
453
|
+
properties: {
|
|
454
|
+
display_name: hit.displayName,
|
|
455
|
+
score: hit.score,
|
|
456
|
+
...(hit.score == null && hit.scoreUnmeasured ? { score_unmeasured: hit.scoreUnmeasured } : {}),
|
|
457
|
+
collection_id: hit.collectionID,
|
|
458
|
+
...(hit.placeID ? { place_id: hit.placeID } : {}),
|
|
459
|
+
...(hit.placetype ? { placetype: hit.placetype } : {}),
|
|
460
|
+
},
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
/**
|
|
465
|
+
* The query pairs of a request except `f`, which each link sets for itself.
|
|
466
|
+
*/
|
|
467
|
+
function queryPairsOf(c: Context): Array<[string, string]> {
|
|
468
|
+
return [...new URL(c.req.url).searchParams].filter(([key]) => key !== "f")
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
/**
|
|
472
|
+
* Register the OGC API routes against an engine.
|
|
473
|
+
*/
|
|
474
|
+
export function registerOGCRoutes(app: OpenAPIHono, engine: OGCEngine, { baseURL, docInfo }: OGCRouteOptions): void {
|
|
475
|
+
const baseOf = (url: string) => baseURL ?? new URL(url).origin
|
|
476
|
+
const collections = engine.collections
|
|
477
|
+
// The route table is complete once the app is built, so the document is
|
|
478
|
+
// emitted once, at the first request.
|
|
479
|
+
let document: OpenAPI30Document | undefined
|
|
480
|
+
|
|
481
|
+
const searchRoute = createRoute({
|
|
482
|
+
method: "get",
|
|
483
|
+
path: "/search",
|
|
484
|
+
operationId: "search",
|
|
485
|
+
summary: "Search",
|
|
486
|
+
description:
|
|
487
|
+
"Forward geocoding in the form of PDOK's Location API. A query without a collection parameter searches every collection.",
|
|
488
|
+
tags: ["data"],
|
|
489
|
+
request: { query: searchQuerySchema(collections.map(({ id }) => id)) },
|
|
490
|
+
responses: {
|
|
491
|
+
200: {
|
|
492
|
+
description: "The matches, best first, as a GeoJSON FeatureCollection.",
|
|
493
|
+
content: { [OGCMediaType.GeoJSON]: { schema: OGCSearchResponseSchema }, ...htmlContent },
|
|
494
|
+
},
|
|
495
|
+
...errorResponses,
|
|
496
|
+
},
|
|
497
|
+
})
|
|
498
|
+
|
|
499
|
+
app.openapi(landingRoute, (c) => {
|
|
500
|
+
const format = negotiateFormat(c, c.req.valid("query").f)
|
|
501
|
+
const landing = buildLandingPage(baseOf(c.req.url), docInfo, format)
|
|
502
|
+
|
|
503
|
+
return format === OGCFormat.HTML ? c.html(landingPageHTML(landing), 200) : c.json(landing, 200)
|
|
504
|
+
})
|
|
505
|
+
|
|
506
|
+
app.openapi(conformanceRoute, (c) => {
|
|
507
|
+
const format = negotiateFormat(c, c.req.valid("query").f)
|
|
508
|
+
const links = selfLinks(baseOf(c.req.url), "conformance", format, "This document")
|
|
509
|
+
|
|
510
|
+
return format === OGCFormat.HTML
|
|
511
|
+
? c.html(conformanceHTML(OGC_CONFORMANCE, links), 200)
|
|
512
|
+
: c.json({ ...OGC_CONFORMANCE, links }, 200)
|
|
513
|
+
})
|
|
514
|
+
|
|
515
|
+
app.openapi(apiRoute, (c) => {
|
|
516
|
+
const format = negotiateFormat(c, c.req.valid("query").f)
|
|
517
|
+
|
|
518
|
+
document ??= emitOpenAPIDocuments(app, docInfo).v30 as OpenAPI30Document
|
|
519
|
+
|
|
520
|
+
if (format === OGCFormat.HTML) {
|
|
521
|
+
const base = baseOf(c.req.url)
|
|
522
|
+
|
|
523
|
+
const links: OGCLink[] = [
|
|
524
|
+
{ href: hrefOf(base, "api", OGCFormat.HTML), rel: OGCLinkRelation.Self, type: OGCMediaType.HTML },
|
|
525
|
+
{ href: hrefOf(base, "api", OGCFormat.JSON), rel: OGCLinkRelation.ServiceDesc, type: OGCMediaType.OpenAPI30 },
|
|
526
|
+
{ href: hrefOf(base, "", OGCFormat.HTML), rel: "start", type: OGCMediaType.HTML, title: "Landing page" },
|
|
527
|
+
]
|
|
528
|
+
|
|
529
|
+
return c.html(apiDefinitionHTML(document, links), 200)
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
return c.body(stringifyJSON(document), 200, { "Content-Type": OGCMediaType.OpenAPI30 })
|
|
533
|
+
})
|
|
534
|
+
|
|
535
|
+
app.openapi(collectionsRoute, (c) => {
|
|
536
|
+
const format = negotiateFormat(c, c.req.valid("query").f)
|
|
537
|
+
const body = buildCollections(baseOf(c.req.url), collections, format)
|
|
538
|
+
|
|
539
|
+
return format === OGCFormat.HTML ? c.html(collectionsHTML(body), 200) : c.json(body, 200)
|
|
540
|
+
})
|
|
541
|
+
|
|
542
|
+
app.openapi(collectionRoute, (c) => {
|
|
543
|
+
const format = negotiateFormat(c, c.req.valid("query").f)
|
|
544
|
+
const { collectionId } = c.req.valid("param")
|
|
545
|
+
const served = collections.find(({ id }) => id === collectionId)
|
|
546
|
+
|
|
547
|
+
if (!served) {
|
|
548
|
+
return errorResponse(c, 404, "collection not found", `no collection has the id "${collectionId}"`)
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
const body = buildCollection(baseOf(c.req.url), served, format)
|
|
552
|
+
|
|
553
|
+
return format === OGCFormat.HTML ? c.html(collectionHTML(body), 200) : c.json(body, 200)
|
|
554
|
+
})
|
|
555
|
+
|
|
556
|
+
app.openapi(searchRoute, async (c) => {
|
|
557
|
+
const query = c.req.valid("query") as Record<string, string | undefined>
|
|
558
|
+
const format = negotiateFormat(c, query["f"] as OGCFormat | undefined)
|
|
559
|
+
const base = baseOf(c.req.url)
|
|
560
|
+
|
|
561
|
+
// Without a query the HTML form is the page a browser following the landing page's `search` link expects.
|
|
562
|
+
if (format === OGCFormat.HTML && !query["q"]?.trim()) {
|
|
563
|
+
return c.html(searchHTML(null, selfLinks(base, "search", format, "Search"), collections), 200)
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
const request = parseSearchQuery(query, collections)
|
|
567
|
+
|
|
568
|
+
if (typeof request === "string") return errorResponse(c, 400, "invalid query parameter", request)
|
|
569
|
+
|
|
570
|
+
const features = selectHits(await engine.search(request.q), request).map(searchFeature)
|
|
571
|
+
|
|
572
|
+
const body: OGCSearchResponse = {
|
|
573
|
+
type: "FeatureCollection",
|
|
574
|
+
timeStamp: isoSeconds(),
|
|
575
|
+
links: selfLinks(base, "search", format, "Search results", {
|
|
576
|
+
query: queryPairsOf(c),
|
|
577
|
+
json: OGCMediaType.GeoJSON,
|
|
578
|
+
}),
|
|
579
|
+
numberReturned: features.length,
|
|
580
|
+
features,
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
if (format === OGCFormat.HTML) return c.html(searchHTML(body, body.links, collections, request.q), 200)
|
|
584
|
+
|
|
585
|
+
return c.body(stringifyJSON(body), 200, { "Content-Type": OGCMediaType.GeoJSON })
|
|
586
|
+
})
|
|
587
|
+
}
|