@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.
- package/README.md +288 -2
- package/cli/geocoder.ts +50 -0
- package/cli/main.ts +407 -0
- package/lib/app.ts +107 -0
- package/lib/bbox.ts +69 -0
- package/lib/collections.ts +213 -0
- package/lib/engine.ts +143 -0
- package/lib/format.ts +46 -0
- package/lib/html.ts +298 -0
- package/lib/index.ts +24 -0
- package/lib/items.ts +169 -0
- package/lib/projection.ts +226 -0
- package/lib/provenance.ts +86 -0
- package/lib/refusal.ts +34 -0
- package/lib/routes.ts +852 -0
- package/lib/schema.ts +457 -0
- package/out/app.d.ts +41 -0
- package/out/app.d.ts.map +1 -0
- package/out/app.js +67 -0
- package/out/app.js.map +1 -0
- 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.d.ts +3 -0
- package/out/cli/main.d.ts.map +1 -0
- package/out/cli/main.js +297 -0
- package/out/cli/main.js.map +1 -0
- package/out/collections.d.ts +99 -0
- package/out/collections.d.ts.map +1 -0
- package/out/collections.js +132 -0
- package/out/collections.js.map +1 -0
- package/out/engine.d.ts +133 -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 +52 -0
- package/out/html.d.ts.map +1 -0
- package/out/html.js +200 -0
- package/out/html.js.map +1 -0
- package/out/index.d.ts +21 -0
- package/out/index.d.ts.map +1 -0
- package/out/index.js +23 -0
- package/out/index.js.map +1 -0
- 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 +40 -0
- package/out/projection.d.ts.map +1 -0
- package/out/projection.js +181 -0
- package/out/projection.js.map +1 -0
- 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 +95 -0
- package/out/routes.d.ts.map +1 -0
- package/out/routes.js +624 -0
- package/out/routes.js.map +1 -0
- package/out/schema.d.ts +422 -0
- package/out/schema.d.ts.map +1 -0
- package/out/schema.js +378 -0
- package/out/schema.js.map +1 -0
- 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 +120 -3
- package/tools/ets.run.ts +258 -0
package/lib/index.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
|
|
2
|
+
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* `@mailwoman/ogc` serves Mailwoman through the OGC API family of standards.
|
|
6
|
+
*
|
|
7
|
+
* It serves the OGC API Common Part 1 resources an OGC client reads first
|
|
8
|
+
* (the landing page, the conformance declaration and the API definition), the OGC API
|
|
9
|
+
* Common Part 2 collections, and forward geocoding at `/search` in PDOK's form.
|
|
10
|
+
* The conformance declaration names only the classes these routes implement.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
export * from "#app"
|
|
14
|
+
export * from "#bbox"
|
|
15
|
+
export * from "#collections"
|
|
16
|
+
export * from "#engine"
|
|
17
|
+
export * from "#format"
|
|
18
|
+
export * from "#html"
|
|
19
|
+
export * from "#items"
|
|
20
|
+
export * from "#projection"
|
|
21
|
+
export * from "#provenance"
|
|
22
|
+
export * from "#refusal"
|
|
23
|
+
export * from "#routes"
|
|
24
|
+
export * from "#schema"
|
package/lib/items.ts
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
|
|
2
|
+
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The query parsing and feature encoding of the OGC API Features Part 1 items resources,
|
|
6
|
+
* `/collections/{collectionId}/items` and `/collections/{collectionId}/items/{featureId}`.
|
|
7
|
+
*
|
|
8
|
+
* A page is read in ascending WOF id order and continues after the last id it returned,
|
|
9
|
+
* so a deep page costs what the first page costs and no item repeats or goes missing between pages.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { isRFC3339DateTime } from "@mailwoman/core/utils"
|
|
13
|
+
import type { BrowsedPlace, WOFPlaceBrowser } from "@mailwoman/resolver-wof-sqlite/lookup/browse"
|
|
14
|
+
|
|
15
|
+
import { parseBBox } from "#bbox"
|
|
16
|
+
import type { OGCItem, OGCItemPageRequest, OGCItemSource } from "#engine"
|
|
17
|
+
import { type AncestorsOf, placeLabel } from "#projection"
|
|
18
|
+
import { type OGCItemFeature, OGC_ITEMS_DEFAULT_LIMIT, OGC_ITEMS_MAX_LIMIT } from "#schema"
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The open end of a `datetime` interval: `..`, or an empty string.
|
|
22
|
+
*/
|
|
23
|
+
function isOpenEnd(value: string): boolean {
|
|
24
|
+
return value === ".." || value === ""
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Whether `raw` is a `datetime` value as OGC API Features Part 1, Requirement 26 D defines it:
|
|
29
|
+
* an RFC 3339 `date-time`, or an interval of two with at most one open end.
|
|
30
|
+
*/
|
|
31
|
+
export function isFeaturesDateTime(raw: string): boolean {
|
|
32
|
+
const parts = raw.split("/")
|
|
33
|
+
|
|
34
|
+
if (parts.length === 1) return isRFC3339DateTime(raw)
|
|
35
|
+
|
|
36
|
+
if (parts.length !== 2) return false
|
|
37
|
+
|
|
38
|
+
const [start, end] = parts as [string, string]
|
|
39
|
+
|
|
40
|
+
if (isOpenEnd(start) && isOpenEnd(end)) return false
|
|
41
|
+
|
|
42
|
+
return (isOpenEnd(start) || isRFC3339DateTime(start)) && (isOpenEnd(end) || isRFC3339DateTime(end))
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Parse the `after` cursor a `next` link sets: a WOF id.
|
|
47
|
+
*/
|
|
48
|
+
function parseCursor(raw: string): number | null {
|
|
49
|
+
if (!/^\d{1,16}$/.test(raw)) return null
|
|
50
|
+
|
|
51
|
+
const value = Number(raw)
|
|
52
|
+
|
|
53
|
+
return Number.isSafeInteger(value) ? value : null
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Validate a `/collections/{collectionId}/items` query the schema admitted.
|
|
58
|
+
*
|
|
59
|
+
* A `limit` above {@link OGC_ITEMS_MAX_LIMIT} is served as the maximum, as Requirement 22 C asks.
|
|
60
|
+
* A valid `datetime` selects every place, because no place has a temporal geometry (Requirement 26 C).
|
|
61
|
+
*
|
|
62
|
+
* @returns The page request, or a message that identifies the invalid parameter.
|
|
63
|
+
*/
|
|
64
|
+
export function parseItemsQuery(query: Readonly<Record<string, string | undefined>>): OGCItemPageRequest | string {
|
|
65
|
+
let limit = OGC_ITEMS_DEFAULT_LIMIT
|
|
66
|
+
const rawLimit = query["limit"]
|
|
67
|
+
|
|
68
|
+
if (rawLimit != null) {
|
|
69
|
+
limit = /^\d+$/.test(rawLimit) ? Number(rawLimit) : Number.NaN
|
|
70
|
+
|
|
71
|
+
if (!(limit >= 1)) return "limit: a positive integer is required"
|
|
72
|
+
|
|
73
|
+
limit = Math.min(limit, OGC_ITEMS_MAX_LIMIT)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
let bbox: OGCItemPageRequest["bbox"] = null
|
|
77
|
+
const rawBBox = query["bbox"]
|
|
78
|
+
|
|
79
|
+
if (rawBBox != null) {
|
|
80
|
+
const parsed = parseBBox(rawBBox, { withHeight: true })
|
|
81
|
+
|
|
82
|
+
if (typeof parsed === "string") return parsed
|
|
83
|
+
|
|
84
|
+
bbox = parsed
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const datetime = query["datetime"]
|
|
88
|
+
|
|
89
|
+
if (datetime != null && !isFeaturesDateTime(datetime)) {
|
|
90
|
+
return "datetime: an RFC 3339 date-time, or an interval of two separated by / with .. for an open end, is required"
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
let after: number | null = null
|
|
94
|
+
const rawAfter = query["after"]
|
|
95
|
+
|
|
96
|
+
if (rawAfter != null) {
|
|
97
|
+
after = parseCursor(rawAfter)
|
|
98
|
+
|
|
99
|
+
if (after == null) return "after: the WOF id a next link carries is required"
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return { bbox, after, limit }
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* The reader {@link placeItemSource} pages through, such as a `WOFPlaceBrowser` over the admin gazetteer.
|
|
107
|
+
*/
|
|
108
|
+
export type PlaceBrowser = Pick<WOFPlaceBrowser, "page" | "place">
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* The item source of a collection of gazetteer places, read through `browser`.
|
|
112
|
+
*
|
|
113
|
+
* @param ancestorsOf The ancestry reader `/search` labels its places with,
|
|
114
|
+
* so an item's `display_name` matches the label `/search` gives the same place.
|
|
115
|
+
*/
|
|
116
|
+
export function placeItemSource(browser: PlaceBrowser, ancestorsOf: AncestorsOf | undefined): OGCItemSource {
|
|
117
|
+
const toItem = (place: BrowsedPlace): OGCItem => {
|
|
118
|
+
const countryCode = place.country || null
|
|
119
|
+
const name = place.name ?? ""
|
|
120
|
+
|
|
121
|
+
return {
|
|
122
|
+
wofID: place.id,
|
|
123
|
+
name,
|
|
124
|
+
placetype: place.placetype ?? "",
|
|
125
|
+
countryCode,
|
|
126
|
+
lon: place.longitude,
|
|
127
|
+
lat: place.latitude,
|
|
128
|
+
displayName: placeLabel({ name, placeID: `wof:${place.id}`, countryCode }, ancestorsOf),
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
return {
|
|
133
|
+
async page({ bbox, after, limit }) {
|
|
134
|
+
const box = bbox ? { minLon: bbox[0], minLat: bbox[1], maxLon: bbox[2], maxLat: bbox[3] } : null
|
|
135
|
+
|
|
136
|
+
return (await browser.page({ box, after, limit })).map(toItem)
|
|
137
|
+
},
|
|
138
|
+
async item(wofID) {
|
|
139
|
+
const place = await browser.place(wofID)
|
|
140
|
+
|
|
141
|
+
return place ? toItem(place) : null
|
|
142
|
+
},
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* The gazetteer place ID of an item, the `id` of its feature and the `place_id` `/search` reports.
|
|
148
|
+
*/
|
|
149
|
+
export function itemPlaceID(item: Pick<OGCItem, "wofID">): string {
|
|
150
|
+
return `wof:${item.wofID}`
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* One item as a GeoJSON feature, with the links the caller supplies.
|
|
155
|
+
*/
|
|
156
|
+
export function itemFeature(item: OGCItem, links?: OGCItemFeature["links"]): OGCItemFeature {
|
|
157
|
+
return {
|
|
158
|
+
type: "Feature",
|
|
159
|
+
id: itemPlaceID(item),
|
|
160
|
+
geometry: { type: "Point", coordinates: [item.lon, item.lat] },
|
|
161
|
+
properties: {
|
|
162
|
+
name: item.name,
|
|
163
|
+
placetype: item.placetype,
|
|
164
|
+
country_code: item.countryCode,
|
|
165
|
+
display_name: item.displayName,
|
|
166
|
+
},
|
|
167
|
+
...(links ? { links } : {}),
|
|
168
|
+
}
|
|
169
|
+
}
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
|
|
2
|
+
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The projection from a geocode result to `/search` matches.
|
|
6
|
+
*
|
|
7
|
+
* The result's coordinate is the first match, in the collection its tier names.
|
|
8
|
+
* The places the resolver weighed for the same part of the input follow,
|
|
9
|
+
* each in the collection its placetype names.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { formatAddress } from "@mailwoman/codex/address/format"
|
|
13
|
+
import type { ComponentDict } from "@mailwoman/codex/address/render"
|
|
14
|
+
import { ISO2_TO_NAME } from "@mailwoman/codex/country"
|
|
15
|
+
import type { GeocodeCandidate, GeocodeResult } from "@mailwoman/core/geocode"
|
|
16
|
+
import { type Ancestor, ScoreUnmeasuredReason, wofIDFromPlaceID } from "@mailwoman/core/resolver"
|
|
17
|
+
|
|
18
|
+
import { collectionOfPlacetype, collectionOfTier, OGCCollectionID } from "#collections"
|
|
19
|
+
import type { OGCSearchHit } from "#engine"
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The country a record's ISO 3166-1 alpha-2 code names, read from the code table.
|
|
23
|
+
*
|
|
24
|
+
* `matchCountry` reads free-form address text and resolves only some two-letter codes,
|
|
25
|
+
* such as `FR` but not `EE`, so a recorded code is looked up directly.
|
|
26
|
+
*/
|
|
27
|
+
function countryOfCode(code: string | null): { iso2: string; name: string } | null {
|
|
28
|
+
const iso2 = code?.trim().toUpperCase()
|
|
29
|
+
const name = iso2 ? ISO2_TO_NAME.get(iso2) : undefined
|
|
30
|
+
|
|
31
|
+
return iso2 && name ? { iso2, name } : null
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function hierarchyName(result: GeocodeResult, tag: string): string | null {
|
|
35
|
+
return result.hierarchy.find((entry) => entry.tag === tag)?.name ?? null
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* The resolved names, most specific first, with a repeated name dropped
|
|
40
|
+
* and the country appended when the hierarchy doesn't name it.
|
|
41
|
+
*/
|
|
42
|
+
function hierarchyLabel(result: GeocodeResult): string {
|
|
43
|
+
const names: string[] = []
|
|
44
|
+
|
|
45
|
+
for (const { name } of result.hierarchy) {
|
|
46
|
+
if (name && !names.includes(name)) {
|
|
47
|
+
names.push(name)
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const country = countryOfCode(result.countryCode)?.name
|
|
52
|
+
|
|
53
|
+
if (names.length && country && !names.includes(country)) {
|
|
54
|
+
names.push(country)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return names.join(", ")
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* The one-line address of a house-grade or street-grade answer, in the country's own order.
|
|
62
|
+
*/
|
|
63
|
+
function addressLabel(result: GeocodeResult, withHouseNumber: boolean): string {
|
|
64
|
+
const country = countryOfCode(result.countryCode)
|
|
65
|
+
|
|
66
|
+
const postcode = result.postcode ?? result.rooftop?.postcode ?? null
|
|
67
|
+
const locality = hierarchyName(result, "locality") ?? result.locality
|
|
68
|
+
const region = hierarchyName(result, "region") ?? result.region
|
|
69
|
+
|
|
70
|
+
const components: ComponentDict = {
|
|
71
|
+
...(withHouseNumber && result.house_number ? { house_number: result.house_number } : {}),
|
|
72
|
+
...(result.street ? { street: result.street } : {}),
|
|
73
|
+
...(result.unit && withHouseNumber ? { unit: result.unit } : {}),
|
|
74
|
+
...(postcode ? { postcode } : {}),
|
|
75
|
+
...(locality ? { locality } : {}),
|
|
76
|
+
...(region ? { region } : {}),
|
|
77
|
+
...(country ? { country: country.name } : {}),
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const formatted = country ? formatAddress(components, country.iso2, { singleLine: true, withRegion: true }) : ""
|
|
81
|
+
|
|
82
|
+
// A country with no recorded layout renders an empty string, so the parts join in the order they were read.
|
|
83
|
+
return formatted || Object.values(components).join(", ")
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* The candidate the answer's coordinate came from, when the coordinate is a gazetteer place's.
|
|
88
|
+
*/
|
|
89
|
+
function answeringCandidate(result: GeocodeResult): GeocodeCandidate | null {
|
|
90
|
+
if (result.resolution_tier !== "admin" && result.resolution_tier !== "postcode") return null
|
|
91
|
+
|
|
92
|
+
return result.candidates.find((candidate) => candidate.lat === result.lat && candidate.lon === result.lon) ?? null
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function scoreOf(candidate: GeocodeCandidate | null): Pick<OGCSearchHit, "score" | "scoreUnmeasured"> {
|
|
96
|
+
if (candidate && typeof candidate.score === "number") return { score: candidate.score }
|
|
97
|
+
|
|
98
|
+
return { score: null, scoreUnmeasured: candidate?.scoreUnmeasured ?? ScoreUnmeasuredReason.Unrecorded }
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Reads a gazetteer place's ancestors by WOF id, nearest first, as `ResolverBackend.ancestors` does.
|
|
103
|
+
*/
|
|
104
|
+
export type AncestorsOf = (wofID: number) => readonly Ancestor[]
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* The fields of a gazetteer place its label reads.
|
|
108
|
+
*/
|
|
109
|
+
export type LabeledPlace = Pick<GeocodeCandidate, "name" | "placeID" | "countryCode">
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* A gazetteer place's label: its name, the ancestors the gazetteer records between it
|
|
113
|
+
* and its country, most specific first, and the country.
|
|
114
|
+
*
|
|
115
|
+
* `/search` labels its place matches with it, and `/collections/{collectionId}/items` its features.
|
|
116
|
+
* A name repeated by an ancestor appears once.
|
|
117
|
+
*
|
|
118
|
+
* Without a WOF place ID or a recorded ancestry the label is the name and the country.
|
|
119
|
+
*/
|
|
120
|
+
export function placeLabel(candidate: LabeledPlace, ancestorsOf: AncestorsOf | undefined): string {
|
|
121
|
+
const names = [candidate.name]
|
|
122
|
+
const wofID = wofIDFromPlaceID(candidate.placeID)
|
|
123
|
+
|
|
124
|
+
if (ancestorsOf && wofID != null) {
|
|
125
|
+
for (const ancestor of ancestorsOf(wofID)) {
|
|
126
|
+
if (ancestor.placetype !== "country" && ancestor.name && !names.includes(ancestor.name)) {
|
|
127
|
+
names.push(ancestor.name)
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const country = countryOfCode(candidate.countryCode)?.name
|
|
133
|
+
|
|
134
|
+
if (country && !names.includes(country)) {
|
|
135
|
+
names.push(country)
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
return names.join(", ")
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* The gazetteer identity of a place match, so two places that share a label stay apart.
|
|
143
|
+
*/
|
|
144
|
+
function placeIdentity(candidate: GeocodeCandidate): Pick<OGCSearchHit, "placeID" | "placetype"> {
|
|
145
|
+
return { ...(candidate.placeID ? { placeID: candidate.placeID } : {}), placetype: candidate.placetype }
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* The `/search` matches for one geocode result, best first.
|
|
150
|
+
*
|
|
151
|
+
* The answer's own coordinate comes first.
|
|
152
|
+
* Its score is the resolver's score for the gazetteer place it came from.
|
|
153
|
+
*
|
|
154
|
+
* An answer placed by an address point, an interpolation or a street centroid
|
|
155
|
+
* has no measured score, so its score is null.
|
|
156
|
+
*
|
|
157
|
+
* The places the resolver weighed for the same span as its first candidate follow, each with its own score.
|
|
158
|
+
* The result lists at most 10 places for a span, so a call reads at most 10 ancestries.
|
|
159
|
+
*
|
|
160
|
+
* @param ancestorsOf The gazetteer's ancestry reader.
|
|
161
|
+
* The label of each place lists the region and county it returns.
|
|
162
|
+
* Absent, a place's label is its name and its country.
|
|
163
|
+
* @returns An empty array when the result has no coordinate.
|
|
164
|
+
*/
|
|
165
|
+
export function geocodeSearchHits(result: GeocodeResult, ancestorsOf?: AncestorsOf): OGCSearchHit[] {
|
|
166
|
+
const hits: OGCSearchHit[] = []
|
|
167
|
+
const answering = answeringCandidate(result)
|
|
168
|
+
const collectionID = collectionOfTier(result.resolution_tier)
|
|
169
|
+
|
|
170
|
+
if (result.lat != null && result.lon != null && collectionID) {
|
|
171
|
+
const houseGrade = collectionID === OGCCollectionID.Addresses
|
|
172
|
+
|
|
173
|
+
if (houseGrade || collectionID === OGCCollectionID.Streets) {
|
|
174
|
+
hits.push({
|
|
175
|
+
collectionID,
|
|
176
|
+
lon: result.lon,
|
|
177
|
+
lat: result.lat,
|
|
178
|
+
displayName: addressLabel(result, houseGrade),
|
|
179
|
+
...scoreOf(answering),
|
|
180
|
+
})
|
|
181
|
+
} else {
|
|
182
|
+
hits.push({
|
|
183
|
+
collectionID,
|
|
184
|
+
lon: result.lon,
|
|
185
|
+
lat: result.lat,
|
|
186
|
+
displayName: answering ? placeLabel(answering, ancestorsOf) : hierarchyLabel(result) || result.input.trim(),
|
|
187
|
+
...scoreOf(answering),
|
|
188
|
+
...(answering ? placeIdentity(answering) : {}),
|
|
189
|
+
})
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
const [first] = result.candidates
|
|
194
|
+
|
|
195
|
+
if (!first) return hits
|
|
196
|
+
|
|
197
|
+
// A place appears once, so the answer's own place and a place listed twice are skipped.
|
|
198
|
+
// Two places at one coordinate stay two matches, because the place ID tells them apart.
|
|
199
|
+
const seen = new Set<string>()
|
|
200
|
+
const placeKey = (candidate: GeocodeCandidate) => candidate.placeID ?? `${candidate.lon},${candidate.lat}`
|
|
201
|
+
|
|
202
|
+
if (answering) {
|
|
203
|
+
seen.add(placeKey(answering))
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
for (const candidate of result.candidates) {
|
|
207
|
+
if (candidate.tag !== first.tag || candidate.value !== first.value) continue
|
|
208
|
+
|
|
209
|
+
const key = placeKey(candidate)
|
|
210
|
+
|
|
211
|
+
if (seen.has(key)) continue
|
|
212
|
+
|
|
213
|
+
seen.add(key)
|
|
214
|
+
|
|
215
|
+
hits.push({
|
|
216
|
+
collectionID: collectionOfPlacetype(candidate.placetype),
|
|
217
|
+
lon: candidate.lon,
|
|
218
|
+
lat: candidate.lat,
|
|
219
|
+
displayName: placeLabel(candidate, ancestorsOf),
|
|
220
|
+
...scoreOf(candidate),
|
|
221
|
+
...placeIdentity(candidate),
|
|
222
|
+
})
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
return hits
|
|
226
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
|
|
2
|
+
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The license and source links of a collection, read from the manifests of its layers.
|
|
6
|
+
*
|
|
7
|
+
* {@linkcode resolveLayerTerms} reads the manifests.
|
|
8
|
+
* Its license links become `license` links (RFC 4946: "Refers to a license
|
|
9
|
+
* associated with this context"), and its source links become `via` links
|
|
10
|
+
* (the RFC 4287 relation for the source of the information in the link's context).
|
|
11
|
+
*
|
|
12
|
+
* A license that requires credit adds an `attribution`, the member OGC API Common Part
|
|
13
|
+
* 2 gives a collection for "a short attribution for data being visualized".
|
|
14
|
+
*
|
|
15
|
+
* A collection lists each link once, whatever the count of regional layers that record it.
|
|
16
|
+
* A manifest field without a value, and a value without a known URL,
|
|
17
|
+
* adds one sentence to the description rather than a link.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import { type LayerTerms, ProvenanceLinkKind, resolveLayerTerms } from "@mailwoman/core/layers/provenance"
|
|
21
|
+
import { gazetteerSourceLocation } from "@mailwoman/resolver-wof-sqlite/build/sources"
|
|
22
|
+
|
|
23
|
+
import type { OGCServedCollection } from "#engine"
|
|
24
|
+
import { OGCLinkRelation, OGCMediaType, type OGCLink } from "#schema"
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* The links, attribution and description sentences a collection's layers contribute.
|
|
28
|
+
*/
|
|
29
|
+
export interface OGCCollectionTerms {
|
|
30
|
+
links: OGCLink[]
|
|
31
|
+
/**
|
|
32
|
+
* The credit a client shows beside the data, present when a layer's license requires attribution.
|
|
33
|
+
*/
|
|
34
|
+
attribution?: string
|
|
35
|
+
/**
|
|
36
|
+
* Sentences for the description, each one stating a manifest field the links couldn't come from.
|
|
37
|
+
*/
|
|
38
|
+
notes: string[]
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function countPhrase(count: number, singular: string, plural: string): string {
|
|
42
|
+
return count === 1 ? `1 ${singular}` : `${count} ${plural}`
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* The license and source links of `served`, from its own layers' manifests.
|
|
47
|
+
*
|
|
48
|
+
* A source that neither the layer's provider nor the gazetteer's source table locates is stated in a note.
|
|
49
|
+
*
|
|
50
|
+
* @param knownLayers Every layer the engine opened, so a source that matches a layer resolves to that layer.
|
|
51
|
+
*/
|
|
52
|
+
export function collectionTerms(served: OGCServedCollection, knownLayers: readonly LayerTerms[]): OGCCollectionTerms {
|
|
53
|
+
const unread = served.unreadRegions
|
|
54
|
+
? `${countPhrase(served.unreadRegions, "region has", "regions have")} a layer without a readable layer_manifest, so ${served.unreadRegions === 1 ? "it contributes" : "they contribute"} no link.`
|
|
55
|
+
: null
|
|
56
|
+
|
|
57
|
+
if (!served.layers) {
|
|
58
|
+
return {
|
|
59
|
+
links: [],
|
|
60
|
+
notes: ["The server read no layer manifest for this collection, so it links no license or source."],
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const provenance = resolveLayerTerms(served.layers, { knownLayers, locate: gazetteerSourceLocation })
|
|
65
|
+
|
|
66
|
+
const links: OGCLink[] = provenance.links.map((link) => ({
|
|
67
|
+
href: link.href,
|
|
68
|
+
rel: link.kind === ProvenanceLinkKind.License ? OGCLinkRelation.License : OGCLinkRelation.Via,
|
|
69
|
+
type: OGCMediaType.HTML,
|
|
70
|
+
title: link.title,
|
|
71
|
+
}))
|
|
72
|
+
|
|
73
|
+
const { withoutLicense, withoutSource, unlinkedLicenses, unlocatedSources, credits } = provenance
|
|
74
|
+
|
|
75
|
+
const notes = [
|
|
76
|
+
...(unread ? [unread] : []),
|
|
77
|
+
...(withoutLicense ? [`${countPhrase(withoutLicense, "layer records", "layers record")} no license.`] : []),
|
|
78
|
+
...(withoutSource ? [`${countPhrase(withoutSource, "layer records", "layers record")} no source.`] : []),
|
|
79
|
+
...(unlinkedLicenses.length ? [`No published text is linked for the license ${unlinkedLicenses.join(", ")}.`] : []),
|
|
80
|
+
...(unlocatedSources.length ? [`The server knows no location for the source ${unlocatedSources.join(", ")}.`] : []),
|
|
81
|
+
]
|
|
82
|
+
|
|
83
|
+
return provenance.attributionRequired && credits.length
|
|
84
|
+
? { links, notes, attribution: credits.join("; ") }
|
|
85
|
+
: { links, notes }
|
|
86
|
+
}
|
package/lib/refusal.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
|
|
2
|
+
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Whether the operator's refused obligation classes keep a layer from being served in bulk.
|
|
6
|
+
*
|
|
7
|
+
* The items routes page through a whole gazetteer, so `MAILWOMAN_REFUSE_OBLIGATIONS`
|
|
8
|
+
* applies to them as it applies to attaching an OpenStreetMap layer.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { obligationFindings, ObligationRefusal } from "@mailwoman/license/refusals"
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Why the refused classes keep a layer with this recorded license from serving items,
|
|
15
|
+
* or null when none applies.
|
|
16
|
+
*
|
|
17
|
+
* A refused class among the license's obligations refuses it.
|
|
18
|
+
* A license the manifest doesn't record has unknown obligations, so `unresolved` refuses it too.
|
|
19
|
+
*
|
|
20
|
+
* @param license The SPDX expression the layer's manifest records, or null when it records none.
|
|
21
|
+
*/
|
|
22
|
+
export function itemsRefusal(license: string | null, refuse: readonly ObligationRefusal[]): string | null {
|
|
23
|
+
if (!license) {
|
|
24
|
+
return refuse.includes(ObligationRefusal.Unresolved)
|
|
25
|
+
? "MAILWOMAN_REFUSE_OBLIGATIONS refuses unresolved obligations, and the database records no license"
|
|
26
|
+
: null
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const findings = obligationFindings(license, refuse)
|
|
30
|
+
|
|
31
|
+
return findings.length
|
|
32
|
+
? `MAILWOMAN_REFUSE_OBLIGATIONS refuses ${findings.map(({ reason }) => reason).join("; ")}`
|
|
33
|
+
: null
|
|
34
|
+
}
|