@mailwoman/ogc 11.2.0 → 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.
- package/README.md +167 -28
- package/cli/geocoder.ts +50 -0
- package/cli/main.ts +303 -49
- package/lib/app.ts +9 -1
- package/lib/bbox.ts +69 -0
- package/lib/collections.ts +63 -9
- package/lib/engine.ts +72 -0
- package/lib/html.ts +64 -2
- package/lib/index.ts +4 -0
- package/lib/items.ts +169 -0
- package/lib/projection.ts +28 -8
- package/lib/provenance.ts +86 -0
- package/lib/refusal.ts +34 -0
- package/lib/routes.ts +326 -61
- package/lib/schema.ts +164 -3
- package/out/app.d.ts +8 -0
- package/out/app.d.ts.map +1 -1
- package/out/app.js +1 -1
- package/out/app.js.map +1 -1
- package/out/bbox.d.ts +23 -0
- package/out/bbox.d.ts.map +1 -0
- package/out/bbox.js +54 -0
- package/out/bbox.js.map +1 -0
- package/out/cli/geocoder.d.ts +24 -0
- package/out/cli/geocoder.d.ts.map +1 -0
- package/out/cli/geocoder.js +28 -0
- package/out/cli/geocoder.js.map +1 -0
- package/out/cli/main.js +225 -46
- package/out/cli/main.js.map +1 -1
- package/out/collections.d.ts +31 -4
- package/out/collections.d.ts.map +1 -1
- package/out/collections.js +32 -3
- package/out/collections.js.map +1 -1
- package/out/engine.d.ts +68 -0
- package/out/engine.d.ts.map +1 -1
- package/out/html.d.ts +11 -1
- package/out/html.d.ts.map +1 -1
- package/out/html.js +49 -2
- package/out/html.js.map +1 -1
- package/out/index.d.ts +4 -0
- package/out/index.d.ts.map +1 -1
- package/out/index.js +4 -0
- package/out/index.js.map +1 -1
- package/out/items.d.ts +38 -0
- package/out/items.d.ts.map +1 -0
- package/out/items.js +136 -0
- package/out/items.js.map +1 -0
- package/out/projection.d.ts +15 -1
- package/out/projection.d.ts.map +1 -1
- package/out/projection.js +21 -8
- package/out/projection.js.map +1 -1
- package/out/provenance.d.ts +41 -0
- package/out/provenance.d.ts.map +1 -0
- package/out/provenance.js +60 -0
- package/out/provenance.js.map +1 -0
- package/out/refusal.d.ts +18 -0
- package/out/refusal.d.ts.map +1 -0
- package/out/refusal.js +30 -0
- package/out/refusal.js.map +1 -0
- package/out/routes.d.ts +24 -22
- package/out/routes.d.ts.map +1 -1
- package/out/routes.js +246 -51
- package/out/routes.js.map +1 -1
- package/out/schema.d.ts +157 -2
- package/out/schema.d.ts.map +1 -1
- package/out/schema.js +140 -3
- package/out/schema.js.map +1 -1
- package/out/tools/ets.run.d.ts +2 -0
- package/out/tools/ets.run.d.ts.map +1 -0
- package/out/tools/ets.run.js +187 -0
- package/out/tools/ets.run.js.map +1 -0
- package/package.json +32 -10
- package/tools/ets.run.ts +258 -0
package/lib/routes.ts
CHANGED
|
@@ -12,24 +12,38 @@
|
|
|
12
12
|
import { createRoute, type OpenAPIHono, z } from "@hono/zod-openapi"
|
|
13
13
|
import { APIErrorSchema, emitOpenAPIDocuments, errorResponse, type OpenAPIDocInfo } from "@mailwoman/api-kit"
|
|
14
14
|
import { MAX_ADDRESS_LENGTH } from "@mailwoman/api/input-limits"
|
|
15
|
-
import {
|
|
15
|
+
import type { LayerTerms } from "@mailwoman/core/layers/provenance"
|
|
16
|
+
import { wofIDFromPlaceID } from "@mailwoman/core/resolver"
|
|
16
17
|
import { isoSeconds } from "@mailwoman/core/utils"
|
|
17
|
-
import {
|
|
18
|
+
import { stringifyJSON } from "@mailwoman/core/values/json"
|
|
18
19
|
import type { Context } from "hono"
|
|
19
20
|
|
|
21
|
+
import { type OGCBBox, parseBBox, searchBoxContains } from "#bbox"
|
|
20
22
|
import { OGC_COLLECTION_CATALOG } from "#collections"
|
|
21
|
-
import type { OGCEngine, OGCSearchHit, OGCServedCollection } from "#engine"
|
|
23
|
+
import type { OGCEngine, OGCItem, OGCSearchHit, OGCServedCollection } from "#engine"
|
|
22
24
|
import { negotiateFormat } from "#format"
|
|
23
25
|
import {
|
|
24
26
|
apiDefinitionHTML,
|
|
25
27
|
collectionHTML,
|
|
26
28
|
collectionsHTML,
|
|
27
29
|
conformanceHTML,
|
|
30
|
+
itemHTML,
|
|
31
|
+
itemsHTML,
|
|
28
32
|
landingPageHTML,
|
|
29
33
|
type OpenAPI30Document,
|
|
30
34
|
searchHTML,
|
|
31
35
|
} from "#html"
|
|
36
|
+
import { itemFeature, itemPlaceID, parseItemsQuery } from "#items"
|
|
37
|
+
import { collectionTerms } from "#provenance"
|
|
32
38
|
import {
|
|
39
|
+
type OGCExtent,
|
|
40
|
+
type OGCItemFeature,
|
|
41
|
+
OGCItemFeatureSchema,
|
|
42
|
+
OGCItemPathSchema,
|
|
43
|
+
OGCItemsQuerySchema,
|
|
44
|
+
type OGCItemsResponse,
|
|
45
|
+
OGCItemsResponseSchema,
|
|
46
|
+
OGC_CRS84,
|
|
33
47
|
collectionRelevanceParameter,
|
|
34
48
|
collectionVersionParameter,
|
|
35
49
|
type OGCCollection,
|
|
@@ -73,15 +87,50 @@ export interface OGCRouteOptions {
|
|
|
73
87
|
* The landing page's title and description, and the API definition's `info` block.
|
|
74
88
|
*/
|
|
75
89
|
docInfo: OpenAPIDocInfo
|
|
90
|
+
/**
|
|
91
|
+
* Serve `/collections/{collectionId}/items` for the collections whose engine entry has an item source.
|
|
92
|
+
*
|
|
93
|
+
* While it's off, the descriptions link items for none of the collections,
|
|
94
|
+
* and both items routes answer 404.
|
|
95
|
+
* `/search` features then link no item, and the conformance declaration lists only the Common classes.
|
|
96
|
+
*
|
|
97
|
+
* @defaultValue `false`
|
|
98
|
+
*/
|
|
99
|
+
serveItems?: boolean
|
|
76
100
|
}
|
|
77
101
|
|
|
78
102
|
/**
|
|
79
|
-
* The
|
|
103
|
+
* The OGC API Features Part 1 classes, declared only when a collection serves items.
|
|
80
104
|
*/
|
|
81
|
-
|
|
82
|
-
|
|
105
|
+
const FEATURES_CLASSES: ReadonlySet<string> = new Set([
|
|
106
|
+
OGCConformanceClass.FeaturesCore,
|
|
107
|
+
OGCConformanceClass.FeaturesGeoJSON,
|
|
108
|
+
OGCConformanceClass.FeaturesHTML,
|
|
109
|
+
OGCConformanceClass.FeaturesOAS30,
|
|
110
|
+
])
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* The conformance classes the routes implement for a service whose collections are `collections`.
|
|
114
|
+
*
|
|
115
|
+
* The OGC API Features Part 1 classes need a collection with items, so a service with none omits them.
|
|
116
|
+
*/
|
|
117
|
+
export function conformanceOf(collections: readonly OGCServedCollection[]): OGCConformance {
|
|
118
|
+
const servesItems = collections.some((collection) => collection.items)
|
|
119
|
+
|
|
120
|
+
return {
|
|
121
|
+
conformsTo: Object.values(OGCConformanceClass).filter((uri) => servesItems || !FEATURES_CLASSES.has(uri)),
|
|
122
|
+
}
|
|
83
123
|
}
|
|
84
124
|
|
|
125
|
+
/**
|
|
126
|
+
* The spatial extent of every collection that serves items.
|
|
127
|
+
*
|
|
128
|
+
* The box is the whole CRS84 range, so it contains every item.
|
|
129
|
+
* A tighter box would need a scan of the gazetteer at startup, and OGC API Features
|
|
130
|
+
* Part 1 asks only that the box contain the items.
|
|
131
|
+
*/
|
|
132
|
+
const WORLD_EXTENT: OGCExtent = { spatial: { bbox: [[-180, -90, 180, 90]], crs: OGC_CRS84 } }
|
|
133
|
+
|
|
85
134
|
const errorContent = { [OGCMediaType.JSON]: { schema: APIErrorSchema } }
|
|
86
135
|
|
|
87
136
|
const errorResponses = {
|
|
@@ -282,77 +331,80 @@ export function buildLandingPage(base: string, docInfo: OpenAPIDocInfo, format:
|
|
|
282
331
|
* The description of one collection the engine serves, in the form `/collections`
|
|
283
332
|
* and `/collections/{collectionId}` both serve, so the two can't disagree.
|
|
284
333
|
*/
|
|
285
|
-
export function buildCollection(
|
|
334
|
+
export function buildCollection(
|
|
335
|
+
base: string,
|
|
336
|
+
served: OGCServedCollection,
|
|
337
|
+
format: OGCFormat,
|
|
338
|
+
knownLayers: readonly LayerTerms[] = served.layers ?? []
|
|
339
|
+
): OGCCollection {
|
|
286
340
|
const { title, description } = OGC_COLLECTION_CATALOG[served.id]
|
|
287
341
|
const coverage = served.regions ? ` The engine opened its layers for ${served.regions.join(", ")}.` : ""
|
|
342
|
+
const path = collectionPath(served.id)
|
|
343
|
+
const terms = collectionTerms(served, knownLayers)
|
|
344
|
+
const links = [...selfLinks(base, path, format, title), ...terms.links]
|
|
345
|
+
|
|
346
|
+
if (served.items) {
|
|
347
|
+
links.push(
|
|
348
|
+
{
|
|
349
|
+
href: hrefOf(base, `${path}/items`, OGCFormat.JSON),
|
|
350
|
+
rel: OGCLinkRelation.Items,
|
|
351
|
+
type: OGCMediaType.GeoJSON,
|
|
352
|
+
title: `${title} items`,
|
|
353
|
+
},
|
|
354
|
+
{
|
|
355
|
+
href: hrefOf(base, `${path}/items`, OGCFormat.HTML),
|
|
356
|
+
rel: OGCLinkRelation.Items,
|
|
357
|
+
type: OGCMediaType.HTML,
|
|
358
|
+
title: `${title} items as HTML`,
|
|
359
|
+
}
|
|
360
|
+
)
|
|
361
|
+
}
|
|
288
362
|
|
|
289
363
|
return {
|
|
290
364
|
id: served.id,
|
|
291
365
|
title,
|
|
292
|
-
description: `${description}${coverage}`,
|
|
293
|
-
links
|
|
366
|
+
description: [`${description}${coverage}`, ...terms.notes].join(" "),
|
|
367
|
+
links,
|
|
368
|
+
...(terms.attribution ? { attribution: terms.attribution } : {}),
|
|
369
|
+
...(served.items ? { itemType: "feature" as const, extent: WORLD_EXTENT } : {}),
|
|
294
370
|
geometryDimension: 0,
|
|
295
371
|
version: OGC_COLLECTION_VERSION,
|
|
296
372
|
}
|
|
297
373
|
}
|
|
298
374
|
|
|
299
375
|
/**
|
|
300
|
-
*
|
|
376
|
+
* Every layer the collections' manifests describe, so one collection's source can resolve to another layer.
|
|
301
377
|
*/
|
|
302
|
-
|
|
303
|
-
|
|
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
|
-
}
|
|
378
|
+
function knownLayersOf(collections: readonly OGCServedCollection[]): LayerTerms[] {
|
|
379
|
+
return collections.flatMap((collection) => collection.layers ?? [])
|
|
311
380
|
}
|
|
312
381
|
|
|
313
382
|
/**
|
|
314
|
-
* A
|
|
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.
|
|
383
|
+
* A collection's path relative to the service root.
|
|
320
384
|
*/
|
|
321
|
-
|
|
385
|
+
function collectionPath(collectionID: string): string {
|
|
386
|
+
return `collections/${encodeURIComponent(collectionID)}`
|
|
387
|
+
}
|
|
322
388
|
|
|
323
389
|
/**
|
|
324
|
-
*
|
|
325
|
-
*
|
|
326
|
-
* @returns The box, or a message that states the problem with the value.
|
|
390
|
+
* The path of one item relative to the service root.
|
|
327
391
|
*/
|
|
328
|
-
export function
|
|
329
|
-
|
|
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]
|
|
392
|
+
export function itemPath(collectionID: string, item: Pick<OGCItem, "wofID">): string {
|
|
393
|
+
return `${collectionPath(collectionID)}/items/${encodeURIComponent(itemPlaceID(item))}`
|
|
345
394
|
}
|
|
346
395
|
|
|
347
396
|
/**
|
|
348
|
-
*
|
|
349
|
-
*
|
|
350
|
-
* A box whose minimum longitude exceeds its maximum crosses the antimeridian, as OGC API boxes may.
|
|
397
|
+
* The collections list for a request served in `format`.
|
|
351
398
|
*/
|
|
352
|
-
export function
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
399
|
+
export function buildCollections(
|
|
400
|
+
base: string,
|
|
401
|
+
collections: readonly OGCServedCollection[],
|
|
402
|
+
format: OGCFormat
|
|
403
|
+
): OGCCollections {
|
|
404
|
+
return {
|
|
405
|
+
links: selfLinks(base, "collections", format, "Collections"),
|
|
406
|
+
collections: collections.map((served) => buildCollection(base, served, format, knownLayersOf(collections))),
|
|
407
|
+
}
|
|
356
408
|
}
|
|
357
409
|
|
|
358
410
|
/**
|
|
@@ -446,7 +498,7 @@ export function selectHits(hits: readonly OGCSearchHit[], request: OGCSearchRequ
|
|
|
446
498
|
/**
|
|
447
499
|
* One hit as a GeoJSON feature.
|
|
448
500
|
*/
|
|
449
|
-
export function searchFeature(hit: OGCSearchHit): OGCSearchFeature {
|
|
501
|
+
export function searchFeature(hit: OGCSearchHit, href?: string): OGCSearchFeature {
|
|
450
502
|
return {
|
|
451
503
|
type: "Feature",
|
|
452
504
|
geometry: { type: "Point", coordinates: [hit.lon, hit.lat] },
|
|
@@ -457,10 +509,31 @@ export function searchFeature(hit: OGCSearchHit): OGCSearchFeature {
|
|
|
457
509
|
collection_id: hit.collectionID,
|
|
458
510
|
...(hit.placeID ? { place_id: hit.placeID } : {}),
|
|
459
511
|
...(hit.placetype ? { placetype: hit.placetype } : {}),
|
|
512
|
+
...(href ? { href } : {}),
|
|
460
513
|
},
|
|
461
514
|
}
|
|
462
515
|
}
|
|
463
516
|
|
|
517
|
+
/**
|
|
518
|
+
* The item URL of a `/search` hit, when its collection serves items and holds the hit's place.
|
|
519
|
+
*
|
|
520
|
+
* The collection is asked for the place, so a link never points at an item that answers 404.
|
|
521
|
+
*/
|
|
522
|
+
async function searchHitHref(
|
|
523
|
+
base: string,
|
|
524
|
+
hit: OGCSearchHit,
|
|
525
|
+
collections: readonly OGCServedCollection[]
|
|
526
|
+
): Promise<string | undefined> {
|
|
527
|
+
const items = collections.find(({ id }) => id === hit.collectionID)?.items
|
|
528
|
+
const wofID = wofIDFromPlaceID(hit.placeID ?? null)
|
|
529
|
+
|
|
530
|
+
if (!items || wofID == null) return undefined
|
|
531
|
+
|
|
532
|
+
const item = await items.item(wofID)
|
|
533
|
+
|
|
534
|
+
return item ? hrefOf(base, itemPath(hit.collectionID, item), OGCFormat.JSON) : undefined
|
|
535
|
+
}
|
|
536
|
+
|
|
464
537
|
/**
|
|
465
538
|
* The query pairs of a request except `f`, which each link sets for itself.
|
|
466
539
|
*/
|
|
@@ -471,13 +544,32 @@ function queryPairsOf(c: Context): Array<[string, string]> {
|
|
|
471
544
|
/**
|
|
472
545
|
* Register the OGC API routes against an engine.
|
|
473
546
|
*/
|
|
474
|
-
export function registerOGCRoutes(
|
|
547
|
+
export function registerOGCRoutes(
|
|
548
|
+
app: OpenAPIHono,
|
|
549
|
+
engine: OGCEngine,
|
|
550
|
+
{ baseURL, docInfo, serveItems = false }: OGCRouteOptions
|
|
551
|
+
): void {
|
|
475
552
|
const baseOf = (url: string) => baseURL ?? new URL(url).origin
|
|
476
|
-
|
|
553
|
+
|
|
554
|
+
// With items off, every collection is served as if its engine entry had no item source.
|
|
555
|
+
const collections: readonly OGCServedCollection[] = serveItems
|
|
556
|
+
? engine.collections
|
|
557
|
+
: engine.collections.map(({ items: _, ...served }) => served)
|
|
558
|
+
|
|
559
|
+
const conformance = conformanceOf(collections)
|
|
477
560
|
// The route table is complete once the app is built, so the document is
|
|
478
561
|
// emitted once, at the first request.
|
|
479
562
|
let document: OpenAPI30Document | undefined
|
|
480
563
|
|
|
564
|
+
// A service whose collections serve no items keeps the items routes out of its API
|
|
565
|
+
// definition, and its conformance declaration lists no Features class.
|
|
566
|
+
// The two paths still answer 404 in the native form.
|
|
567
|
+
if (collections.some((collection) => collection.items)) {
|
|
568
|
+
registerItemRoutes(app, collections, baseOf)
|
|
569
|
+
} else {
|
|
570
|
+
registerItemRoutesNotFound(app, collections)
|
|
571
|
+
}
|
|
572
|
+
|
|
481
573
|
const searchRoute = createRoute({
|
|
482
574
|
method: "get",
|
|
483
575
|
path: "/search",
|
|
@@ -508,8 +600,8 @@ export function registerOGCRoutes(app: OpenAPIHono, engine: OGCEngine, { baseURL
|
|
|
508
600
|
const links = selfLinks(baseOf(c.req.url), "conformance", format, "This document")
|
|
509
601
|
|
|
510
602
|
return format === OGCFormat.HTML
|
|
511
|
-
? c.html(conformanceHTML(
|
|
512
|
-
: c.json({ ...
|
|
603
|
+
? c.html(conformanceHTML(conformance, links), 200)
|
|
604
|
+
: c.json({ ...conformance, links }, 200)
|
|
513
605
|
})
|
|
514
606
|
|
|
515
607
|
app.openapi(apiRoute, (c) => {
|
|
@@ -529,7 +621,11 @@ export function registerOGCRoutes(app: OpenAPIHono, engine: OGCEngine, { baseURL
|
|
|
529
621
|
return c.html(apiDefinitionHTML(document, links), 200)
|
|
530
622
|
}
|
|
531
623
|
|
|
532
|
-
|
|
624
|
+
// The served definition lists the origin the request reached, the base its link hrefs use,
|
|
625
|
+
// so a client resolves each path against a URL rather than the `{host}:{port}` template.
|
|
626
|
+
const served = { ...document, servers: [{ url: baseOf(c.req.url) }] }
|
|
627
|
+
|
|
628
|
+
return c.body(stringifyJSON(served), 200, { "Content-Type": OGCMediaType.OpenAPI30 })
|
|
533
629
|
})
|
|
534
630
|
|
|
535
631
|
app.openapi(collectionsRoute, (c) => {
|
|
@@ -548,7 +644,7 @@ export function registerOGCRoutes(app: OpenAPIHono, engine: OGCEngine, { baseURL
|
|
|
548
644
|
return errorResponse(c, 404, "collection not found", `no collection has the id "${collectionId}"`)
|
|
549
645
|
}
|
|
550
646
|
|
|
551
|
-
const body = buildCollection(baseOf(c.req.url), served, format)
|
|
647
|
+
const body = buildCollection(baseOf(c.req.url), served, format, knownLayersOf(collections))
|
|
552
648
|
|
|
553
649
|
return format === OGCFormat.HTML ? c.html(collectionHTML(body), 200) : c.json(body, 200)
|
|
554
650
|
})
|
|
@@ -567,7 +663,11 @@ export function registerOGCRoutes(app: OpenAPIHono, engine: OGCEngine, { baseURL
|
|
|
567
663
|
|
|
568
664
|
if (typeof request === "string") return errorResponse(c, 400, "invalid query parameter", request)
|
|
569
665
|
|
|
570
|
-
const
|
|
666
|
+
const hits = selectHits(await engine.search(request.q), request)
|
|
667
|
+
|
|
668
|
+
const features = await Promise.all(
|
|
669
|
+
hits.map(async (hit) => searchFeature(hit, await searchHitHref(base, hit, collections)))
|
|
670
|
+
)
|
|
571
671
|
|
|
572
672
|
const body: OGCSearchResponse = {
|
|
573
673
|
type: "FeatureCollection",
|
|
@@ -585,3 +685,168 @@ export function registerOGCRoutes(app: OpenAPIHono, engine: OGCEngine, { baseURL
|
|
|
585
685
|
return c.body(stringifyJSON(body), 200, { "Content-Type": OGCMediaType.GeoJSON })
|
|
586
686
|
})
|
|
587
687
|
}
|
|
688
|
+
|
|
689
|
+
const itemsRoute = createRoute({
|
|
690
|
+
method: "get",
|
|
691
|
+
path: "/collections/{collectionId}/items",
|
|
692
|
+
operationId: "getFeatures",
|
|
693
|
+
summary: "Items",
|
|
694
|
+
description:
|
|
695
|
+
"One page of the collection's places in ascending WOF id order, as a GeoJSON FeatureCollection. A `next` link continues after the page's last place while more places remain.",
|
|
696
|
+
tags: ["data"],
|
|
697
|
+
request: { params: OGCCollectionPathSchema, query: OGCItemsQuerySchema },
|
|
698
|
+
responses: {
|
|
699
|
+
200: {
|
|
700
|
+
description: "One page of the collection's items.",
|
|
701
|
+
content: { [OGCMediaType.GeoJSON]: { schema: OGCItemsResponseSchema }, ...htmlContent },
|
|
702
|
+
},
|
|
703
|
+
404: { description: "No collection has this id, or the collection serves no items.", content: errorContent },
|
|
704
|
+
...errorResponses,
|
|
705
|
+
},
|
|
706
|
+
})
|
|
707
|
+
|
|
708
|
+
const itemRoute = createRoute({
|
|
709
|
+
method: "get",
|
|
710
|
+
path: "/collections/{collectionId}/items/{featureId}",
|
|
711
|
+
operationId: "getFeature",
|
|
712
|
+
summary: "Item",
|
|
713
|
+
description: "One place, by the gazetteer place ID `/search` reports as `place_id`.",
|
|
714
|
+
tags: ["data"],
|
|
715
|
+
request: { params: OGCItemPathSchema, query: OGCFormatQuerySchema },
|
|
716
|
+
responses: {
|
|
717
|
+
200: {
|
|
718
|
+
description: "The item, as a GeoJSON Feature.",
|
|
719
|
+
content: { [OGCMediaType.GeoJSON]: { schema: OGCItemFeatureSchema }, ...htmlContent },
|
|
720
|
+
},
|
|
721
|
+
404: {
|
|
722
|
+
description: "No collection has this id, it serves no items, or it holds no such item.",
|
|
723
|
+
content: errorContent,
|
|
724
|
+
},
|
|
725
|
+
...errorResponses,
|
|
726
|
+
},
|
|
727
|
+
})
|
|
728
|
+
|
|
729
|
+
/**
|
|
730
|
+
* The served collection with items for `collectionId`, or the 404 response that states why there's none.
|
|
731
|
+
*/
|
|
732
|
+
function collectionWithItems(c: Context, collections: readonly OGCServedCollection[], collectionId: string) {
|
|
733
|
+
const served = collections.find(({ id }) => id === collectionId)
|
|
734
|
+
|
|
735
|
+
if (!served) return errorResponse(c, 404, "collection not found", `no collection has the id "${collectionId}"`)
|
|
736
|
+
|
|
737
|
+
if (!served.items) {
|
|
738
|
+
return errorResponse(c, 404, "collection serves no items", `the collection "${collectionId}" has no items resource`)
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
return { served, items: served.items }
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
/**
|
|
745
|
+
* Answer both items paths with the native 404 when no collection serves items.
|
|
746
|
+
*
|
|
747
|
+
* The handlers stay out of the API definition, so it declares no items route.
|
|
748
|
+
*/
|
|
749
|
+
function registerItemRoutesNotFound(app: OpenAPIHono, collections: readonly OGCServedCollection[]): void {
|
|
750
|
+
const notFound = (c: Context) => {
|
|
751
|
+
const found = collectionWithItems(c, collections, c.req.param("collectionId") ?? "")
|
|
752
|
+
|
|
753
|
+
return found instanceof Response ? found : errorResponse(c, 404, "collection serves no items")
|
|
754
|
+
}
|
|
755
|
+
|
|
756
|
+
app.get("/collections/:collectionId/items", notFound)
|
|
757
|
+
app.get("/collections/:collectionId/items/:featureId", notFound)
|
|
758
|
+
}
|
|
759
|
+
|
|
760
|
+
/**
|
|
761
|
+
* Register `/collections/{collectionId}/items` and `/collections/{collectionId}/items/{featureId}`.
|
|
762
|
+
*/
|
|
763
|
+
function registerItemRoutes(
|
|
764
|
+
app: OpenAPIHono,
|
|
765
|
+
collections: readonly OGCServedCollection[],
|
|
766
|
+
baseOf: (url: string) => string
|
|
767
|
+
): void {
|
|
768
|
+
app.openapi(itemsRoute, async (c) => {
|
|
769
|
+
const query = c.req.valid("query")
|
|
770
|
+
const format = negotiateFormat(c, query.f)
|
|
771
|
+
const found = collectionWithItems(c, collections, c.req.valid("param").collectionId)
|
|
772
|
+
|
|
773
|
+
if (found instanceof Response) return found
|
|
774
|
+
|
|
775
|
+
const request = parseItemsQuery(query)
|
|
776
|
+
|
|
777
|
+
if (typeof request === "string") return errorResponse(c, 400, "invalid query parameter", request)
|
|
778
|
+
|
|
779
|
+
// One row past the page tells whether a next page exists without counting the box.
|
|
780
|
+
const rows = await found.items.page({ ...request, limit: request.limit + 1 })
|
|
781
|
+
const page = rows.slice(0, request.limit)
|
|
782
|
+
const base = baseOf(c.req.url)
|
|
783
|
+
const path = `${collectionPath(found.served.id)}/items`
|
|
784
|
+
const { title } = OGC_COLLECTION_CATALOG[found.served.id]
|
|
785
|
+
const pairs = queryPairsOf(c)
|
|
786
|
+
|
|
787
|
+
const links = selfLinks(base, path, format, `${title} items`, { query: pairs, json: OGCMediaType.GeoJSON })
|
|
788
|
+
const last = page.at(-1)
|
|
789
|
+
|
|
790
|
+
if (rows.length > request.limit && last) {
|
|
791
|
+
links.push({
|
|
792
|
+
href: hrefOf(base, path, format, [...pairs.filter(([key]) => key !== "after"), ["after", String(last.wofID)]]),
|
|
793
|
+
rel: OGCLinkRelation.Next,
|
|
794
|
+
type: mediaTypeOf(format, OGCMediaType.GeoJSON),
|
|
795
|
+
title: "Next page",
|
|
796
|
+
})
|
|
797
|
+
}
|
|
798
|
+
|
|
799
|
+
links.push({
|
|
800
|
+
href: hrefOf(base, collectionPath(found.served.id), OGCFormat.JSON),
|
|
801
|
+
rel: OGCLinkRelation.Collection,
|
|
802
|
+
type: OGCMediaType.JSON,
|
|
803
|
+
title,
|
|
804
|
+
})
|
|
805
|
+
|
|
806
|
+
const body: OGCItemsResponse = {
|
|
807
|
+
type: "FeatureCollection",
|
|
808
|
+
timeStamp: isoSeconds(),
|
|
809
|
+
links,
|
|
810
|
+
numberReturned: page.length,
|
|
811
|
+
features: page.map((item) => itemFeature(item)),
|
|
812
|
+
}
|
|
813
|
+
|
|
814
|
+
if (format === OGCFormat.HTML) return c.html(itemsHTML(body, title, found.served.id), 200)
|
|
815
|
+
|
|
816
|
+
return c.body(stringifyJSON(body), 200, { "Content-Type": OGCMediaType.GeoJSON })
|
|
817
|
+
})
|
|
818
|
+
|
|
819
|
+
app.openapi(itemRoute, async (c) => {
|
|
820
|
+
const format = negotiateFormat(c, c.req.valid("query").f)
|
|
821
|
+
const { collectionId, featureId } = c.req.valid("param")
|
|
822
|
+
const found = collectionWithItems(c, collections, collectionId)
|
|
823
|
+
|
|
824
|
+
if (found instanceof Response) return found
|
|
825
|
+
|
|
826
|
+
const wofID = wofIDFromPlaceID(featureId)
|
|
827
|
+
const item = wofID == null ? null : await found.items.item(wofID)
|
|
828
|
+
|
|
829
|
+
if (!item) {
|
|
830
|
+
return errorResponse(c, 404, "feature not found", `the collection "${collectionId}" has no item "${featureId}"`)
|
|
831
|
+
}
|
|
832
|
+
|
|
833
|
+
const base = baseOf(c.req.url)
|
|
834
|
+
const { title } = OGC_COLLECTION_CATALOG[found.served.id]
|
|
835
|
+
|
|
836
|
+
const links: OGCLink[] = [
|
|
837
|
+
...selfLinks(base, itemPath(found.served.id, item), format, item.name, { json: OGCMediaType.GeoJSON }),
|
|
838
|
+
{
|
|
839
|
+
href: hrefOf(base, collectionPath(found.served.id), OGCFormat.JSON),
|
|
840
|
+
rel: OGCLinkRelation.Collection,
|
|
841
|
+
type: OGCMediaType.JSON,
|
|
842
|
+
title,
|
|
843
|
+
},
|
|
844
|
+
]
|
|
845
|
+
|
|
846
|
+
const feature: OGCItemFeature = itemFeature(item, links)
|
|
847
|
+
|
|
848
|
+
if (format === OGCFormat.HTML) return c.html(itemHTML(feature, title), 200)
|
|
849
|
+
|
|
850
|
+
return c.body(stringifyJSON(feature), 200, { "Content-Type": OGCMediaType.GeoJSON })
|
|
851
|
+
})
|
|
852
|
+
}
|