@vectoriahq/quac 0.0.0-stage → 0.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.
Files changed (134) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +35 -2
  3. package/dist/generate/arcgisStacCatalogue.d.ts +46 -0
  4. package/dist/generate/arcgisStacCatalogue.js +189 -0
  5. package/dist/generate/externalStacCatalogue.d.ts +53 -0
  6. package/dist/generate/externalStacCatalogue.js +143 -0
  7. package/dist/generate/fileLayerCollection.d.ts +44 -0
  8. package/dist/generate/fileLayerCollection.js +72 -0
  9. package/dist/generate/folderStacCatalogue.d.ts +48 -0
  10. package/dist/generate/folderStacCatalogue.js +69 -0
  11. package/dist/generate/normalizedProject.d.ts +452 -0
  12. package/dist/generate/normalizedProject.js +877 -0
  13. package/dist/generate/ogcFeaturesStacCatalogue.d.ts +49 -0
  14. package/dist/generate/ogcFeaturesStacCatalogue.js +207 -0
  15. package/dist/generate/qmlForLayer.d.ts +17 -0
  16. package/dist/generate/qmlForLayer.js +36 -0
  17. package/dist/generate/serverLayerTree.d.ts +44 -0
  18. package/dist/generate/serverLayerTree.js +50 -0
  19. package/dist/generate/serverStacCatalogue.d.ts +84 -0
  20. package/dist/generate/serverStacCatalogue.js +327 -0
  21. package/dist/generate/stacCatalogue.d.ts +110 -0
  22. package/dist/generate/stacCatalogue.js +887 -0
  23. package/dist/generate/wfsStacCatalogue.d.ts +37 -0
  24. package/dist/generate/wfsStacCatalogue.js +127 -0
  25. package/dist/generate/wmsStacCatalogue.d.ts +46 -0
  26. package/dist/generate/wmsStacCatalogue.js +229 -0
  27. package/dist/index.d.ts +77 -0
  28. package/dist/index.js +52 -0
  29. package/dist/model/QgsProject.d.ts +724 -0
  30. package/dist/model/QgsProject.js +15 -0
  31. package/dist/model/Stac.d.ts +322 -0
  32. package/dist/model/Stac.js +16 -0
  33. package/dist/model/WmsProject.d.ts +105 -0
  34. package/dist/model/WmsProject.js +1 -0
  35. package/dist/model/arcgis.d.ts +134 -0
  36. package/dist/model/arcgis.js +1 -0
  37. package/dist/model/delimitedText.d.ts +38 -0
  38. package/dist/model/delimitedText.js +46 -0
  39. package/dist/model/featureFilter.d.ts +41 -0
  40. package/dist/model/featureFilter.js +24 -0
  41. package/dist/model/fieldIconAddress.d.ts +27 -0
  42. package/dist/model/fieldIconAddress.js +93 -0
  43. package/dist/model/ogcFeatures.d.ts +40 -0
  44. package/dist/model/ogcFeatures.js +6 -0
  45. package/dist/model/quacExtension.d.ts +28 -0
  46. package/dist/model/quacExtension.js +75 -0
  47. package/dist/model/stacAssetRoles.d.ts +33 -0
  48. package/dist/model/stacAssetRoles.js +44 -0
  49. package/dist/model/stacCollectionNotes.d.ts +21 -0
  50. package/dist/model/stacCollectionNotes.js +44 -0
  51. package/dist/model/stacHref.d.ts +56 -0
  52. package/dist/model/stacHref.js +93 -0
  53. package/dist/model/stacPmtiles.d.ts +38 -0
  54. package/dist/model/stacPmtiles.js +69 -0
  55. package/dist/model/stacSizeAndCount.d.ts +14 -0
  56. package/dist/model/stacSizeAndCount.js +20 -0
  57. package/dist/model/stacStyles.d.ts +93 -0
  58. package/dist/model/stacStyles.js +87 -0
  59. package/dist/model/stacTreeWalk.d.ts +36 -0
  60. package/dist/model/stacTreeWalk.js +76 -0
  61. package/dist/model/stacWms.d.ts +18 -0
  62. package/dist/model/stacWms.js +73 -0
  63. package/dist/model/stacWmts.d.ts +34 -0
  64. package/dist/model/stacWmts.js +90 -0
  65. package/dist/model/stacXyz.d.ts +24 -0
  66. package/dist/model/stacXyz.js +46 -0
  67. package/dist/model/wfs.d.ts +136 -0
  68. package/dist/model/wfs.js +1 -0
  69. package/dist/parse/collectLayerVisibility.d.ts +10 -0
  70. package/dist/parse/collectLayerVisibility.js +22 -0
  71. package/dist/parse/customProperties.d.ts +18 -0
  72. package/dist/parse/customProperties.js +43 -0
  73. package/dist/parse/geometrySiblings.d.ts +25 -0
  74. package/dist/parse/geometrySiblings.js +83 -0
  75. package/dist/parse/parseBrandColour.d.ts +14 -0
  76. package/dist/parse/parseBrandColour.js +25 -0
  77. package/dist/parse/parseDataDefinedEditable.d.ts +38 -0
  78. package/dist/parse/parseDataDefinedEditable.js +76 -0
  79. package/dist/parse/parseDataSource.d.ts +53 -0
  80. package/dist/parse/parseDataSource.js +484 -0
  81. package/dist/parse/parseExtent.d.ts +17 -0
  82. package/dist/parse/parseExtent.js +49 -0
  83. package/dist/parse/parseFormLayout.d.ts +26 -0
  84. package/dist/parse/parseFormLayout.js +60 -0
  85. package/dist/parse/parseGetProjectSettings.d.ts +15 -0
  86. package/dist/parse/parseGetProjectSettings.js +172 -0
  87. package/dist/parse/parseLayers.d.ts +23 -0
  88. package/dist/parse/parseLayers.js +1125 -0
  89. package/dist/parse/parseLegendTree.d.ts +20 -0
  90. package/dist/parse/parseLegendTree.js +66 -0
  91. package/dist/parse/parseMeasurement.d.ts +20 -0
  92. package/dist/parse/parseMeasurement.js +66 -0
  93. package/dist/parse/parseMergin.d.ts +19 -0
  94. package/dist/parse/parseMergin.js +51 -0
  95. package/dist/parse/parseProjectVariables.d.ts +9 -0
  96. package/dist/parse/parseProjectVariables.js +19 -0
  97. package/dist/parse/parseQfieldSync.d.ts +47 -0
  98. package/dist/parse/parseQfieldSync.js +95 -0
  99. package/dist/parse/parseQgsXml.d.ts +15 -0
  100. package/dist/parse/parseQgsXml.js +100 -0
  101. package/dist/parse/parseRelations.d.ts +19 -0
  102. package/dist/parse/parseRelations.js +64 -0
  103. package/dist/parse/parseStyle.d.ts +33 -0
  104. package/dist/parse/parseStyle.js +128 -0
  105. package/dist/parse/parseWmsPublishing.d.ts +21 -0
  106. package/dist/parse/parseWmsPublishing.js +38 -0
  107. package/dist/parse/projectProperties.d.ts +15 -0
  108. package/dist/parse/projectProperties.js +27 -0
  109. package/dist/parse/qgisExpression.d.ts +194 -0
  110. package/dist/parse/qgisExpression.js +859 -0
  111. package/dist/parse/qtDateFormat.d.ts +74 -0
  112. package/dist/parse/qtDateFormat.js +324 -0
  113. package/dist/parse/qtRegex.d.ts +67 -0
  114. package/dist/parse/qtRegex.js +397 -0
  115. package/dist/parse/readProjectFile.d.ts +7 -0
  116. package/dist/parse/readProjectFile.js +27 -0
  117. package/dist/sources/qgisServerUrl.d.ts +6 -0
  118. package/dist/sources/qgisServerUrl.js +17 -0
  119. package/dist/sources/stacDocuments.d.ts +40 -0
  120. package/dist/sources/stacDocuments.js +85 -0
  121. package/dist/sources/stacItems.d.ts +109 -0
  122. package/dist/sources/stacItems.js +287 -0
  123. package/dist/util/catalogueFileFormats.d.ts +58 -0
  124. package/dist/util/catalogueFileFormats.js +64 -0
  125. package/dist/util/devLog.d.ts +10 -0
  126. package/dist/util/devLog.js +22 -0
  127. package/dist/util/fileName.d.ts +18 -0
  128. package/dist/util/fileName.js +45 -0
  129. package/dist/util/nonBlankText.d.ts +2 -0
  130. package/dist/util/nonBlankText.js +4 -0
  131. package/dist/util/wfsVersions.d.ts +9 -0
  132. package/dist/util/wfsVersions.js +13 -0
  133. package/package.json +55 -4
  134. package/src/schema/quac.schema.json +939 -0
@@ -0,0 +1,877 @@
1
+ import { readHttpsAddress } from "../model/fieldIconAddress.js";
2
+ import { declareQuacExtension } from "../model/quacExtension.js";
3
+ import { STAC_VERSION } from "../model/Stac.js";
4
+ import { collectCatalogueCollections } from "../model/stacTreeWalk.js";
5
+ import { readNonBlank } from "../util/nonBlankText.js";
6
+ /**
7
+ * Source-agnostic intermediate model + the single STAC generator that
8
+ * consumes it.
9
+ *
10
+ * Both source pipelines — the static `.qgs` path (QgsProject) and the live
11
+ * QGIS Server path (WmsProject) — project their model into a
12
+ * `NormalizedProject`, then hand it to `generateStacFromNormalized`. All the
13
+ * shared tree-walk / catalog / collection / link assembly is here once;
14
+ * the per-source wording, id fallbacks, bbox field names, `table:columns`
15
+ * derivation and asset shapes are resolved by the source adapters *before*
16
+ * they build the normalized tree, so this core has no source
17
+ * conditionals.
18
+ *
19
+ * This is the convergence point for the STAC data-model direction: new
20
+ * source types only need a `*ToNormalized` adapter, not a fresh generator.
21
+ */
22
+ /**
23
+ * STAC extension URL for the Table extension. Pinned to the version we wrote
24
+ * against — changes here imply schema review since the extension is still
25
+ * maturing.
26
+ */
27
+ export const TABLE_EXTENSION = 'https://stac-extensions.github.io/table/v1.3.0/schema.json';
28
+ /** STAC extension URL for the Vector extension, whose `vector:geometry_types` a Collection carries. */
29
+ export const VECTOR_EXTENSION = 'https://stac-extensions.github.io/vector/v0.1.0/schema.json';
30
+ /**
31
+ * The key a Collection takes for the relations that make it a related dataset
32
+ * rather than a map layer (`packages/quac/spec/README.md`). Its value is
33
+ * an array, because one set of records can hang off more than one parent.
34
+ */
35
+ export const QUAC_RELATED_TO_FIELD = 'quac:related_to';
36
+ /**
37
+ * The key a Collection takes when its source keeps it out of the legend: a QGIS
38
+ * layer's Private flag (`packages/quac/spec/README.md`). Written only as
39
+ * `true`.
40
+ */
41
+ export const QUAC_PRIVATE_FIELD = 'quac:private';
42
+ /**
43
+ * The key a Collection takes for the edits its source locks: `{ add?,
44
+ * attributes?, geometry?, delete? }`, each `true` or `{ expression }`. Written
45
+ * only where the source locks an edit.
46
+ */
47
+ export const QUAC_LOCKS_FIELD = 'quac:locks';
48
+ /**
49
+ * The key a Collection takes for the filters its source sets up: a list of
50
+ * `{ field, name? }`, in the source's order. Written only where it sets one.
51
+ */
52
+ export const QUAC_FILTERS_FIELD = 'quac:filters';
53
+ export const QUAC_LEGEND_IMAGE_FIELD = 'quac:legend_image';
54
+ /**
55
+ * The key a Collection takes for the icon drawn beside its name: an `https://`
56
+ * address, the same form as a column's `quac:icon`.
57
+ */
58
+ export const QUAC_ICON_FIELD = 'quac:icon';
59
+ /**
60
+ * The key a Collection takes for the area and distance units its source gives
61
+ * `$area`, `$length` and `$perimeter` in: `{ area_unit, distance_unit }`, each
62
+ * the name QGIS saves.
63
+ */
64
+ export const QUAC_MEASUREMENT_FIELD = 'quac:measurement';
65
+ /**
66
+ * The key a Collection takes for the fields its source's form layout places, in
67
+ * the layout's order: a QGIS Drag and Drop Designer form
68
+ * (`packages/quac/spec/README.md`). A Collection whose source has no such layout has no such field.
69
+ */
70
+ export const QUAC_FORM_ORDER_FIELD = 'quac:form_order';
71
+ /**
72
+ * The key a group Catalog takes when its source shows only one of its children
73
+ * at a time: a QGIS mutually exclusive group. Written only when true.
74
+ */
75
+ export const QUAC_MUTUALLY_EXCLUSIVE_FIELD = 'quac:mutually_exclusive';
76
+ /**
77
+ * The key a top-level Catalog takes for the box its source's author saved the
78
+ * map showing, in WGS84, as STAC writes a bbox.
79
+ */
80
+ const INITIAL_VIEW_FIELD = 'quac:initial_view';
81
+ /**
82
+ * The key a top-level Catalog takes for the id of the Collection its source
83
+ * names as the one the map opens with as the active layer.
84
+ */
85
+ export const QUAC_ACTIVE_COLLECTION_FIELD = 'quac:active_collection';
86
+ /**
87
+ * Records where a catalogue came from as the Catalog's `self` link, so the
88
+ * document says it rather than a reader holding it alongside. Sources with no
89
+ * address of their own leave it off.
90
+ */
91
+ export function withSelfLink(catalogue, selfHref) {
92
+ if (!selfHref)
93
+ return catalogue;
94
+ catalogue.links = [
95
+ ...catalogue.links.filter((link) => link.rel !== 'self'),
96
+ { rel: 'self', href: selfHref, type: 'application/json' }
97
+ ];
98
+ return catalogue;
99
+ }
100
+ /**
101
+ * Builds a STAC Catalog tree from a NormalizedProject, listing the QUAC schema
102
+ * in `stac_extensions` where `declareQuacExtension` says:
103
+ * NormalizedProject → top-level Catalog
104
+ * group nodes → nested Catalogs
105
+ * layer nodes → Collections
106
+ */
107
+ export function generateStacFromNormalized(project) {
108
+ const { collections, catalogs, links } = walkNodes(project.nodes, project.providers ?? []);
109
+ const catalog = {
110
+ type: 'Catalog',
111
+ stac_version: STAC_VERSION,
112
+ id: project.id,
113
+ title: project.title || undefined,
114
+ description: project.description,
115
+ links: [
116
+ { rel: 'self', href: './catalog.json' },
117
+ { rel: 'root', href: './catalog.json' },
118
+ ...links
119
+ ]
120
+ };
121
+ if (project.bbox) {
122
+ catalog['extent'] = buildStacExtent(project.bbox);
123
+ }
124
+ if (project.initialView) {
125
+ const { xmin, ymin, xmax, ymax } = project.initialView;
126
+ catalog[INITIAL_VIEW_FIELD] = [xmin, ymin, xmax, ymax];
127
+ }
128
+ for (const [key, value] of Object.entries(project.vendorFields ?? {})) {
129
+ catalog[key] = value;
130
+ }
131
+ if (collections.length > 0)
132
+ catalog.collections = collections;
133
+ if (catalogs.length > 0)
134
+ catalog.catalogs = catalogs;
135
+ const activeCollectionId = project.activeCollectionId;
136
+ if (activeCollectionId && collectCatalogueCollections(catalog).some((collection) => collection.id === activeCollectionId)) {
137
+ catalog[QUAC_ACTIVE_COLLECTION_FIELD] = activeCollectionId;
138
+ }
139
+ return declareQuacExtension(catalog);
140
+ }
141
+ function walkNodes(nodes, defaultProviders) {
142
+ const collections = [];
143
+ const catalogs = [];
144
+ const links = [];
145
+ for (const node of nodes) {
146
+ if (node.kind === 'layer') {
147
+ const collection = buildCollection(node, defaultProviders);
148
+ collections.push(collection);
149
+ links.push({
150
+ rel: 'child',
151
+ href: `./collections/${collection.id}.json`,
152
+ type: 'application/json',
153
+ title: collection.title
154
+ });
155
+ }
156
+ else {
157
+ const sub = walkNodes(node.children, defaultProviders);
158
+ const subCatalog = {
159
+ type: 'Catalog',
160
+ stac_version: STAC_VERSION,
161
+ id: node.id,
162
+ title: node.title || undefined,
163
+ description: node.description,
164
+ links: [
165
+ { rel: 'self', href: `./catalogs/${node.id}/catalog.json` },
166
+ { rel: 'parent', href: '../../catalog.json' },
167
+ { rel: 'root', href: '../../catalog.json' },
168
+ ...sub.links
169
+ ]
170
+ };
171
+ if (node.mutuallyExclusive)
172
+ subCatalog[QUAC_MUTUALLY_EXCLUSIVE_FIELD] = true;
173
+ if (sub.collections.length > 0)
174
+ subCatalog.collections = sub.collections;
175
+ if (sub.catalogs.length > 0)
176
+ subCatalog.catalogs = sub.catalogs;
177
+ catalogs.push(subCatalog);
178
+ links.push({
179
+ rel: 'child',
180
+ href: `./catalogs/${node.id}/catalog.json`,
181
+ type: 'application/json',
182
+ title: node.title
183
+ });
184
+ }
185
+ }
186
+ return { collections, catalogs, links };
187
+ }
188
+ function buildCollection(layer, defaultProviders) {
189
+ const stacExtensions = [...(layer.extensions ?? [])];
190
+ if (layer.tableColumns.length > 0) {
191
+ stacExtensions.push(TABLE_EXTENSION);
192
+ }
193
+ const vectorGeometryTypes = geoJsonGeometryTypes(layer.wkbTypes ?? []);
194
+ if (vectorGeometryTypes)
195
+ stacExtensions.push(VECTOR_EXTENSION);
196
+ const summaries = {};
197
+ if (layer.geometryType)
198
+ summaries['quac:geometry_type'] = [layer.geometryType];
199
+ const providers = [...defaultProviders, ...(layer.providers ?? [])];
200
+ const collection = {
201
+ type: 'Collection',
202
+ stac_version: STAC_VERSION,
203
+ id: layer.id,
204
+ title: layer.title,
205
+ description: layer.description,
206
+ license: layer.license ?? 'other',
207
+ providers: providers.length > 0 ? providers : undefined,
208
+ extent: buildStacExtent(layer.bbox, layer.temporalInterval ?? undefined),
209
+ summaries: Object.keys(summaries).length > 0 ? summaries : undefined,
210
+ assets: layer.assets,
211
+ links: [
212
+ { rel: 'self', href: `./collections/${layer.id}.json` },
213
+ { rel: 'parent', href: '../catalog.json' },
214
+ { rel: 'root', href: '../catalog.json' },
215
+ ...(layer.links ?? [])
216
+ ]
217
+ };
218
+ if (layer.keywords && layer.keywords.length > 0)
219
+ collection['keywords'] = layer.keywords;
220
+ if (layer.created)
221
+ collection['created'] = layer.created;
222
+ if (layer.updated)
223
+ collection['updated'] = layer.updated;
224
+ // Per-layer source declarations that STAC core does not model. Attached
225
+ // directly on the Collection (not under summaries) since they're single
226
+ // values per layer, not facet ranges.
227
+ if (layer.displayField)
228
+ collection['quac:display_field'] = layer.displayField;
229
+ if (layer.visibilityChecked !== null)
230
+ collection['quac:visibility_checked'] = layer.visibilityChecked;
231
+ if (layer.editable !== null)
232
+ collection['quac:editable'] = layer.editable;
233
+ if (layer.identifiable != null)
234
+ collection['quac:identifiable'] = layer.identifiable;
235
+ if (layer.scaleRange?.minScale)
236
+ collection['quac:min_scale'] = layer.scaleRange.minScale;
237
+ if (layer.scaleRange?.maxScale)
238
+ collection['quac:max_scale'] = layer.scaleRange.maxScale;
239
+ if (layer.opacity != null)
240
+ collection['quac:opacity'] = layer.opacity;
241
+ // A Collection with this is a related dataset rather than a map layer,
242
+ // and the field states what its records hang off.
243
+ if (layer.relatedTo && layer.relatedTo.length > 0) {
244
+ collection[QUAC_RELATED_TO_FIELD] = layer.relatedTo.map(relationToJson);
245
+ }
246
+ if (layer.isPrivate)
247
+ collection[QUAC_PRIVATE_FIELD] = true;
248
+ if (layer.locks && Object.keys(layer.locks).length > 0)
249
+ collection[QUAC_LOCKS_FIELD] = { ...layer.locks };
250
+ if (layer.filters && layer.filters.length > 0) {
251
+ collection[QUAC_FILTERS_FIELD] = layer.filters.map(({ field, name }) => (name ? { field, name } : { field }));
252
+ }
253
+ if (layer.legendImage)
254
+ collection[QUAC_LEGEND_IMAGE_FIELD] = layer.legendImage;
255
+ if (layer.icon)
256
+ collection[QUAC_ICON_FIELD] = layer.icon;
257
+ if (layer.formOrder)
258
+ collection[QUAC_FORM_ORDER_FIELD] = layer.formOrder;
259
+ // Source-declaring vendor fields (`quac:wfs_*`), copied verbatim so a consumer reads the source structurally rather than by
260
+ // sniffing the asset href.
261
+ for (const [key, value] of Object.entries(layer.vendorFields ?? {})) {
262
+ collection[key] = value;
263
+ }
264
+ if (stacExtensions.length > 0)
265
+ collection.stac_extensions = stacExtensions;
266
+ if (vectorGeometryTypes)
267
+ collection['vector:geometry_types'] = vectorGeometryTypes;
268
+ if (layer.qgisServerNames?.wmsName)
269
+ collection['quac:qgis_server_wms_name'] = layer.qgisServerNames.wmsName;
270
+ if (layer.qgisServerNames?.wfsTypeName)
271
+ collection['quac:qgis_server_wfs_type_name'] = layer.qgisServerNames.wfsTypeName;
272
+ if (layer.tableColumns.length > 0)
273
+ collection['table:columns'] = layer.tableColumns.map(withDataType);
274
+ return collection;
275
+ }
276
+ /** The geometry types GeoJSON defines, the only values `vector:geometry_types` takes. */
277
+ const GEOJSON_GEOMETRY_TYPES = new Set(['Point', 'MultiPoint', 'LineString', 'MultiLineString', 'Polygon', 'MultiPolygon', 'GeometryCollection']);
278
+ /**
279
+ * The GeoJSON geometry types of WKB type names, each once: a name less any Z,
280
+ * M, ZM or 25D suffix, where that is a GeoJSON type. Null where there are no
281
+ * names, or where one has no GeoJSON type, such as `CompoundCurve` or
282
+ * `NoGeometry`, since a list missing it would misstate the layer.
283
+ */
284
+ function geoJsonGeometryTypes(wkbTypes) {
285
+ const types = new Set();
286
+ for (const wkbType of wkbTypes) {
287
+ const base = wkbType.replace(/(ZM|Z|M|25D)$/, '');
288
+ if (!GEOJSON_GEOMETRY_TYPES.has(base))
289
+ return null;
290
+ types.add(base);
291
+ }
292
+ return types.size > 0 ? [...types] : null;
293
+ }
294
+ /**
295
+ * The STAC common metadata data type each column type holds its values in
296
+ * (`commons/common-metadata.md` § Data Types, `radiantearth/stac-spec@master`,
297
+ * read 2026-11-03). `int` is a 32-bit integer, as Qt's `int` and ArcGIS's
298
+ * integer field are; `long` is 64-bit and `double` a 64-bit float.
299
+ */
300
+ const DATA_TYPE_BY_COLUMN_TYPE = {
301
+ int: 'int32',
302
+ long: 'int64',
303
+ double: 'float64',
304
+ string: 'other',
305
+ boolean: 'other',
306
+ date: 'other',
307
+ time: 'other',
308
+ timestamp: 'other'
309
+ };
310
+ /**
311
+ * The column with `data_type` written beside its `type`, as the Table
312
+ * extension recommends. A `type` outside `TableColumnType`, such as a QGIS
313
+ * Server type name this package does not map, gets no `data_type`.
314
+ */
315
+ export function withDataType(column) {
316
+ const type = column.type;
317
+ if (!type || !Object.hasOwn(DATA_TYPE_BY_COLUMN_TYPE, type))
318
+ return column;
319
+ return { ...column, data_type: DATA_TYPE_BY_COLUMN_TYPE[type] };
320
+ }
321
+ /**
322
+ * Reads `quac:active_collection` back off a Catalog: the id of the Collection
323
+ * the map opens with as the active layer, or null where the source names none.
324
+ */
325
+ export function readCatalogueActiveCollection(catalog) {
326
+ const value = catalog[QUAC_ACTIVE_COLLECTION_FIELD];
327
+ return typeof value === 'string' && value !== '' ? value : null;
328
+ }
329
+ /**
330
+ * Reads `quac:initial_view` back off a Catalog.
331
+ *
332
+ * Returns null for a Catalog that declares nothing, which is what every source
333
+ * with no saved view behind it publishes. Null means "the source said nothing
334
+ * about where to open", so a caller frames the map its own way rather than
335
+ * reading a missing field as an instruction.
336
+ */
337
+ export function readCatalogueInitialView(catalog) {
338
+ const value = catalog[INITIAL_VIEW_FIELD];
339
+ if (!Array.isArray(value) || value.length !== 4)
340
+ return null;
341
+ const numbers = value.filter((entry) => typeof entry === 'number' && Number.isFinite(entry));
342
+ if (numbers.length !== 4)
343
+ return null;
344
+ const [xmin, ymin, xmax, ymax] = numbers;
345
+ // A box with no width or height frames nothing, and an inverted one frames
346
+ // the whole world — neither is a view, so neither is read as one.
347
+ if (xmin >= xmax || ymin >= ymax)
348
+ return null;
349
+ return { xmin, ymin, xmax, ymax };
350
+ }
351
+ /**
352
+ * Reads shared source declarations back off a Collection.
353
+ *
354
+ * Each returns null for a Collection that declares nothing, which is what every
355
+ * source with no author behind it publishes. Null means "the source said
356
+ * nothing", never "the source said no", so a caller supplies its own default
357
+ * rather than reading a missing field as `false`.
358
+ */
359
+ /** The field the source nominates as the layer's title attribute. */
360
+ export function readCollectionDisplayField(collection) {
361
+ return readNonBlank(collection['quac:display_field']);
362
+ }
363
+ /**
364
+ * Each entry of a Collection's `table:columns` that is an object with a
365
+ * non-blank `name`, as `[trimmed name, column]`, in declared order. An entry
366
+ * of any other shape, or a `table:columns` that is not a list, yields nothing.
367
+ */
368
+ function* declaredColumns(collection) {
369
+ const columns = collection['table:columns'];
370
+ if (!Array.isArray(columns))
371
+ return;
372
+ for (const column of columns) {
373
+ const candidate = readObject(column);
374
+ const name = readNonBlank(candidate?.name);
375
+ if (candidate && name)
376
+ yield [name, candidate];
377
+ }
378
+ }
379
+ /** Field display labels from STAC Table column titles or descriptions. */
380
+ export function readCollectionFieldAliases(collection) {
381
+ const aliases = {};
382
+ for (const [name, column] of declaredColumns(collection)) {
383
+ const alias = readNonBlank(column.title) ?? readNonBlank(column.description);
384
+ if (alias)
385
+ aliases[name] = alias;
386
+ }
387
+ return aliases;
388
+ }
389
+ /**
390
+ * Every field name a Collection declares, in the order it declares them.
391
+ * Empty for a Collection with no `table:columns`. Read from the catalogue
392
+ * alone, so it lists a column no loaded record holds, which
393
+ * `collectFieldNames` does not.
394
+ */
395
+ export function readCollectionFieldNames(collection) {
396
+ return Array.from(declaredColumns(collection), ([name]) => name);
397
+ }
398
+ /** Each declared column `type`, keyed by field name. A column with no `type` is absent. */
399
+ export function readCollectionFieldTypes(collection) {
400
+ const types = {};
401
+ for (const [name, column] of declaredColumns(collection)) {
402
+ const type = readNonBlank(column.type);
403
+ if (type)
404
+ types[name] = type;
405
+ }
406
+ return types;
407
+ }
408
+ /**
409
+ * Each column's `values` picklist, keyed by field name. A value is returned as
410
+ * stored, string or number. An entry with no label, or with a value of any
411
+ * other kind, is dropped, and a column left with no entries is absent.
412
+ */
413
+ export function readCollectionFieldValues(collection) {
414
+ const picklists = {};
415
+ for (const [name, column] of declaredColumns(collection)) {
416
+ if (!Array.isArray(column.values))
417
+ continue;
418
+ const entries = [];
419
+ for (const entry of column.values) {
420
+ if (!entry || typeof entry !== 'object')
421
+ continue;
422
+ const { value, label, icon } = entry;
423
+ const isValue = typeof value === 'string' || (typeof value === 'number' && Number.isFinite(value));
424
+ if (!isValue || typeof label !== 'string' || !label.trim())
425
+ continue;
426
+ const address = readHttpsAddress(icon);
427
+ entries.push(address ? { value, label, icon: address } : { value, label });
428
+ }
429
+ if (entries.length > 0)
430
+ picklists[name] = entries;
431
+ }
432
+ return picklists;
433
+ }
434
+ /**
435
+ * Each column's `quac:value_relation`, keyed by field name — the
436
+ * counterpart of the write in `stacCatalogue.ts`. A column declaring none is
437
+ * absent from the result. An entry missing its Collection id, `key` or `value`
438
+ * is dropped, and an `order_by` that is not `value`, or `field` without an
439
+ * `order_field`, reads as ordered by key.
440
+ */
441
+ export function readCollectionFieldValueRelations(collection) {
442
+ const relations = {};
443
+ for (const [name, column] of declaredColumns(collection)) {
444
+ const declared = column['quac:value_relation'];
445
+ if (!declared || typeof declared !== 'object')
446
+ continue;
447
+ const entry = declared;
448
+ const lookup = readTrimmedString(entry.collection);
449
+ const key = readTrimmedString(entry.key);
450
+ const value = readTrimmedString(entry.value);
451
+ if (!lookup || !key || !value)
452
+ continue;
453
+ const filter = readTrimmedString(entry.filter);
454
+ const orderField = readTrimmedString(entry.order_field);
455
+ const orderBy = entry.order_by === 'value' ? 'value' : entry.order_by === 'field' && orderField ? 'field' : null;
456
+ relations[name] = {
457
+ collection: lookup,
458
+ key,
459
+ value,
460
+ ...(filter ? { filter } : {}),
461
+ ...(entry.allow_null === true ? { allow_null: true } : {}),
462
+ ...(entry.allow_multi === true ? { allow_multi: true } : {}),
463
+ ...(orderBy ? { order_by: orderBy } : {}),
464
+ ...(orderBy === 'field' ? { order_field: orderField } : {}),
465
+ ...(entry.descending === true ? { descending: true } : {})
466
+ };
467
+ }
468
+ return relations;
469
+ }
470
+ /**
471
+ * The units a Collection's source measures in, or null where it names none or
472
+ * names anything this does not read, which is read as QGIS's default of square
473
+ * metres and metres.
474
+ */
475
+ export function readCollectionMeasurement(collection) {
476
+ const value = collection?.[QUAC_MEASUREMENT_FIELD];
477
+ if (typeof value?.area_unit !== 'string' || typeof value.distance_unit !== 'string')
478
+ return null;
479
+ return { areaUnit: value.area_unit, distanceUnit: value.distance_unit };
480
+ }
481
+ /** The image a Collection shows in place of its legend (`quac:legend_image`), or null where it names none a browser draws. */
482
+ export function readCollectionLegendImage(collection) {
483
+ const image = collection?.[QUAC_LEGEND_IMAGE_FIELD];
484
+ return typeof image === 'string' && /^data:image\/[\w+.-]+;base64,/.test(image) ? image : null;
485
+ }
486
+ /** The icon drawn beside a Collection's name (`quac:icon`), or null where it names no `https://` address. */
487
+ export function readCollectionIcon(collection) {
488
+ return readHttpsAddress(collection?.[QUAC_ICON_FIELD]);
489
+ }
490
+ const LOCKED_EDITS = ['add', 'attributes', 'geometry', 'delete'];
491
+ /**
492
+ * Reads `quac:locks` back off a Collection: the edits it locks, each `true` or
493
+ * `{ expression }`. A member that is neither, or an expression that is blank,
494
+ * is not read, so the edit stays as the rest of the Collection allows.
495
+ */
496
+ export function readCollectionLocks(collection) {
497
+ const value = collection[QUAC_LOCKS_FIELD];
498
+ if (!value || typeof value !== 'object' || Array.isArray(value))
499
+ return {};
500
+ const locks = {};
501
+ for (const edit of LOCKED_EDITS) {
502
+ const lock = readEditLock(value[edit]);
503
+ if (lock)
504
+ locks[edit] = lock;
505
+ }
506
+ return locks;
507
+ }
508
+ function readEditLock(value) {
509
+ if (value === true)
510
+ return true;
511
+ if (!value || typeof value !== 'object')
512
+ return null;
513
+ const expression = value['expression'];
514
+ return typeof expression === 'string' && expression.trim() !== '' ? { expression } : null;
515
+ }
516
+ /**
517
+ * Reads `quac:filters` back off a Collection, in order. An entry with no
518
+ * field is not read, and a later entry on a field already read is dropped,
519
+ * since a layer holds one filter per field.
520
+ */
521
+ export function readCollectionFilters(collection) {
522
+ const value = collection[QUAC_FILTERS_FIELD];
523
+ if (!Array.isArray(value))
524
+ return [];
525
+ const filters = [];
526
+ for (const entry of value) {
527
+ if (!entry || typeof entry !== 'object')
528
+ continue;
529
+ const { field, name } = entry;
530
+ if (typeof field !== 'string' || field === '' || filters.some((filter) => filter.field === field))
531
+ continue;
532
+ filters.push({ field, name: typeof name === 'string' && name.trim() !== '' ? name : null });
533
+ }
534
+ return filters;
535
+ }
536
+ /** Whether a Collection is private: its source keeps it out of the legend. */
537
+ export function readCollectionPrivate(collection) {
538
+ return collection[QUAC_PRIVATE_FIELD] === true;
539
+ }
540
+ /**
541
+ * The fields a Collection declares as holding a file, keyed by field name —
542
+ * the counterpart of the `quac:attachment` write in `stacCatalogue.ts`.
543
+ * A column declaring none is absent from the result.
544
+ *
545
+ * An omitted `viewer`, `filter` or `default_root` reads as null, and an
546
+ * omitted or unrecognised `relative_storage` as absolute.
547
+ */
548
+ export function readCollectionFieldAttachments(collection) {
549
+ const attachments = {};
550
+ for (const [name, column] of declaredColumns(collection)) {
551
+ const declared = column['quac:attachment'];
552
+ if (!declared || typeof declared !== 'object' || Array.isArray(declared))
553
+ continue;
554
+ const fields = declared;
555
+ attachments[name] = {
556
+ viewer: ATTACHMENT_VIEWERS.includes(fields.viewer) ? fields.viewer ?? null : null,
557
+ filter: readNonBlank(fields.filter),
558
+ defaultRoot: readNonBlank(fields.default_root),
559
+ relativeStorage: ATTACHMENT_RELATIVE_STORAGE.includes(fields.relative_storage)
560
+ ? fields.relative_storage ?? 'absolute'
561
+ : 'absolute'
562
+ };
563
+ }
564
+ return attachments;
565
+ }
566
+ /**
567
+ * What each column says about changing it, keyed by field name — the
568
+ * counterpart of the `quac:editable`, `quac:default`,
569
+ * `quac:constraints`, `quac:hidden`, `quac:searchable` and
570
+ * `quac:reuse_last_value` writes in `stacCatalogue.ts`. A column
571
+ * declaring none of them is absent from the result.
572
+ *
573
+ * Only `quac:editable: false` makes a field read-only, only
574
+ * `quac:searchable: false` keeps it out of a search, and only
575
+ * `quac:hidden: true`, `quac:reuse_last_value: true` and `quac:color: true`
576
+ * set theirs. A `quac:icon` that is not an `https://` address is left out, as
577
+ * is a `quac:control` other than `buttons` or `dropdown`.
578
+ * A default of an unrecognised `kind`, or a `fixed` one without a `value`, is
579
+ * left out; a `uuid` one of an omitted or unrecognised `format` reads as
580
+ * `with_braces`. A `not_null` or `unique` other than `enforced` or `warn` reads
581
+ * as none, and a `quac:duplicate_policy` other than `default` or `unset`
582
+ * reads as none. A constraint `expression` or a default of `kind` `expression`
583
+ * with no expression text is left out.
584
+ */
585
+ export function readCollectionFieldEditRules(collection) {
586
+ const relations = readCollectionFieldValueRelations(collection);
587
+ const rules = {};
588
+ for (const [name, candidate] of declaredColumns(collection)) {
589
+ const readOnly = candidate['quac:editable'] === false;
590
+ const defaultValue = readColumnDefault(candidate['quac:default']);
591
+ const constraints = readObject(candidate['quac:constraints']);
592
+ const notNull = constraints?.not_null === 'enforced' || constraints?.not_null === 'warn' ? constraints.not_null : null;
593
+ const unique = constraints?.unique === 'enforced' || constraints?.unique === 'warn' ? constraints.unique : null;
594
+ const constraintExpression = readConstraintExpression(constraints?.expression);
595
+ const hidden = candidate['quac:hidden'] === true;
596
+ const reuseLastValue = candidate['quac:reuse_last_value'] === true;
597
+ const duplicatePolicy = candidate['quac:duplicate_policy'] === 'default' || candidate['quac:duplicate_policy'] === 'unset'
598
+ ? candidate['quac:duplicate_policy']
599
+ : null;
600
+ const checkbox = readColumnCheckbox(candidate['quac:checkbox']);
601
+ const range = readColumnRange(candidate['quac:range']);
602
+ const dateFormat = readNonBlank(candidate['quac:date_format']);
603
+ const colour = candidate['quac:color'] === true;
604
+ const notSearchable = candidate['quac:searchable'] === false;
605
+ const icon = readHttpsAddress(candidate['quac:icon']);
606
+ const control = candidate['quac:control'] === 'buttons' || candidate['quac:control'] === 'dropdown' ? candidate['quac:control'] : null;
607
+ const defaultExpression = readDefaultExpression(candidate['quac:default']);
608
+ const relation = relations[name];
609
+ // A field storing several keys gets no list, so no rule about its blank choice.
610
+ const valueRelation = relation && relation.allow_multi !== true ? { allowNull: relation.allow_null === true, relation } : null;
611
+ if (valueRelation || readOnly || defaultValue || defaultExpression || notNull || unique || constraintExpression || hidden || reuseLastValue || duplicatePolicy || checkbox || range || dateFormat || colour || notSearchable || icon || control) {
612
+ rules[name] = {
613
+ readOnly, defaultValue, notNull, unique, hidden, reuseLastValue, duplicatePolicy,
614
+ ...(constraintExpression ? { constraintExpression } : {}),
615
+ ...(defaultExpression ? { defaultExpression } : {}),
616
+ ...(checkbox ? { checkbox } : {}),
617
+ ...(range ? { range } : {}),
618
+ ...(dateFormat ? { dateFormat } : {}),
619
+ ...(colour ? { colour } : {}),
620
+ ...(notSearchable ? { notSearchable } : {}),
621
+ ...(icon ? { icon } : {}),
622
+ ...(control ? { control } : {}),
623
+ ...(valueRelation ? { valueRelation } : {})
624
+ };
625
+ }
626
+ }
627
+ return rules;
628
+ }
629
+ /**
630
+ * The form layout the source declared, counterpart of the `quac:form_order`,
631
+ * `quac:visible_when`, `quac:in_form` and `quac:editable_when` writes in `stacCatalogue.ts`. Null for
632
+ * a Collection with no `quac:form_order`, whose form places every field.
633
+ *
634
+ * A placed field's `visibleWhen` is its column's `quac:visible_when`, with a
635
+ * blank or non-text entry left out and each expression kept as written: whether
636
+ * it can be read is for the caller. A name in the order that is not text is
637
+ * skipped. Its `editableWhen` is its column's `quac:editable_when`, as written.
638
+ * A column is unplaced only when it says `quac:in_form: false` and the
639
+ * order does not list it.
640
+ */
641
+ export function readCollectionFormLayout(collection) {
642
+ const order = collection[QUAC_FORM_ORDER_FIELD];
643
+ if (!Array.isArray(order))
644
+ return null;
645
+ const columns = new Map(declaredColumns(collection));
646
+ const fields = [];
647
+ const placed = new Set();
648
+ for (const entry of order) {
649
+ const name = readNonBlank(entry);
650
+ if (!name || placed.has(name))
651
+ continue;
652
+ placed.add(name);
653
+ const chain = columns.get(name)?.['quac:visible_when'];
654
+ const visibleWhen = Array.isArray(chain) ? chain.flatMap((expression) => readNonBlank(expression) ?? []) : [];
655
+ const editableWhen = readNonBlank(columns.get(name)?.['quac:editable_when']);
656
+ fields.push({ name, visibleWhen, ...(editableWhen ? { editableWhen } : {}) });
657
+ }
658
+ const unplaced = Array.from(columns).filter(([name, column]) => column['quac:in_form'] === false && !placed.has(name)).map(([name]) => name);
659
+ return { fields, unplaced };
660
+ }
661
+ /** The `expression` of `quac:constraints`; one with no expression text, or a strength other than `enforced` or `warn`, is left out. */
662
+ function readConstraintExpression(declared) {
663
+ const fields = readObject(declared);
664
+ const expression = readNonBlank(fields?.expression);
665
+ if (!fields || !expression || (fields.strength !== 'enforced' && fields.strength !== 'warn'))
666
+ return null;
667
+ const description = readNonBlank(fields.description);
668
+ return { expression, ...(description ? { description } : {}), strength: fields.strength };
669
+ }
670
+ /** A `quac:default` of `kind` `expression`; one with no expression text is left out. */
671
+ function readDefaultExpression(declared) {
672
+ const fields = readObject(declared);
673
+ const expression = readNonBlank(fields?.expression);
674
+ return fields?.kind === 'expression' && expression ? { expression, onUpdate: fields.on_update === true } : null;
675
+ }
676
+ /** A `quac:checkbox`. Any object reads as a checkbox: its members default to unset, show Yes and No, and no blank. */
677
+ function readColumnCheckbox(declared) {
678
+ const fields = readObject(declared);
679
+ if (!fields)
680
+ return null;
681
+ return {
682
+ checkedValue: readNonBlank(fields.checked),
683
+ uncheckedValue: readNonBlank(fields.unchecked),
684
+ showStoredValue: fields.show_stored === true,
685
+ allowNull: fields.allow_null === true
686
+ };
687
+ }
688
+ /** A `quac:range`; a member that is not a finite number, or a suffix that is not text, is left out. */
689
+ function readColumnRange(declared) {
690
+ const fields = readObject(declared);
691
+ if (!fields)
692
+ return null;
693
+ const range = {};
694
+ for (const key of ['min', 'max', 'step']) {
695
+ const value = fields[key];
696
+ if (typeof value === 'number' && Number.isFinite(value))
697
+ range[key] = value;
698
+ }
699
+ const precision = fields.precision;
700
+ if (typeof precision === 'number' && Number.isInteger(precision) && precision >= 0)
701
+ range.precision = precision;
702
+ if (typeof fields.suffix === 'string' && fields.suffix)
703
+ range.suffix = fields.suffix;
704
+ return Object.keys(range).length > 0 ? range : null;
705
+ }
706
+ function readColumnDefault(declared) {
707
+ const fields = readObject(declared);
708
+ if (!fields)
709
+ return null;
710
+ const onUpdate = fields.on_update === true;
711
+ switch (fields.kind) {
712
+ case 'now': return { kind: 'now', onUpdate };
713
+ case 'uuid': return { kind: 'uuid', format: UUID_FORMATS.find((format) => format === fields.format) ?? 'with_braces', onUpdate };
714
+ case 'fixed': return isFixedValue(fields.value) ? { kind: 'fixed', value: fields.value, onUpdate } : null;
715
+ default: return null;
716
+ }
717
+ }
718
+ const UUID_FORMATS = ['with_braces', 'without_braces', 'id128'];
719
+ function isFixedValue(value) {
720
+ return value === null || typeof value === 'string' || typeof value === 'boolean' || (typeof value === 'number' && Number.isFinite(value));
721
+ }
722
+ function readObject(value) {
723
+ return value && typeof value === 'object' && !Array.isArray(value) ? value : null;
724
+ }
725
+ const ATTACHMENT_VIEWERS = ['image', 'web', 'audio', 'video'];
726
+ const ATTACHMENT_RELATIVE_STORAGE = ['absolute', 'project', 'default-root'];
727
+ /** Whether the source declares the layer editable. */
728
+ export function readCollectionEditable(collection) {
729
+ const value = collection['quac:editable'];
730
+ return typeof value === 'boolean' ? value : null;
731
+ }
732
+ /** Whether the source lets the layer be the active layer. */
733
+ export function readCollectionIdentifiable(collection) {
734
+ const value = collection['quac:identifiable'];
735
+ return typeof value === 'boolean' ? value : null;
736
+ }
737
+ /** Whether the source has the layer, or the group a Catalog stands for, switched on. */
738
+ export function readCollectionVisibility(collection) {
739
+ const value = collection['quac:visibility_checked'];
740
+ return typeof value === 'boolean' ? value : null;
741
+ }
742
+ /** The layer's geometry type as QGIS names it ("Point", "Line", "Polygon", "No geometry"); null when the source gives none. */
743
+ export function readCollectionGeometryType(collection) {
744
+ const summary = collection.summaries?.['quac:geometry_type'];
745
+ const value = Array.isArray(summary) ? summary[0] : null;
746
+ return typeof value === 'string' ? value : null;
747
+ }
748
+ /** The scales the source draws the layer between; null when it sets no limit. */
749
+ export function readCollectionScaleRange(collection) {
750
+ const minScale = readScaleDenominator(collection['quac:min_scale']);
751
+ const maxScale = readScaleDenominator(collection['quac:max_scale']);
752
+ if (minScale === null && maxScale === null)
753
+ return null;
754
+ return { minScale, maxScale };
755
+ }
756
+ function readScaleDenominator(value) {
757
+ return typeof value === 'number' && Number.isFinite(value) && value > 0 ? value : null;
758
+ }
759
+ /**
760
+ * The opacity a Collection declares for its layer, as `quac:opacity` (0 to 1).
761
+ *
762
+ * Null for a Collection that states none, which is every source with no
763
+ * project author behind it, and for a value that is not a number above 0: a
764
+ * layer declared fully transparent would open invisible with nothing saying
765
+ * why, so it is read as stating nothing. A caller draws a layer that states
766
+ * none at full opacity.
767
+ */
768
+ export function readCollectionOpacity(collection) {
769
+ return statedOpacity(collection['quac:opacity']);
770
+ }
771
+ /** A layer's opacity as `readCollectionOpacity` reads it, for a source that holds the number without a Collection. */
772
+ export function statedOpacity(value) {
773
+ if (typeof value !== 'number' || !Number.isFinite(value) || value <= 0)
774
+ return null;
775
+ return Math.min(1, value);
776
+ }
777
+ /** One relation as `quac:related_to` stores it. */
778
+ function relationToJson(relation) {
779
+ return {
780
+ id: relation.id,
781
+ ...(relation.name ? { name: relation.name } : {}),
782
+ parent_collection_id: relation.parentCollectionId,
783
+ fields: relation.fields.map((field) => ({ parent: field.parent, child: field.child }))
784
+ };
785
+ }
786
+ /**
787
+ * The relations a Collection declares — the counterpart of the
788
+ * `quac:related_to` write in `buildCollection` above, kept beside it so
789
+ * the two cannot drift.
790
+ *
791
+ * An empty result means the Collection is a map layer like any other. A relation
792
+ * missing its id, its parent Collection id or every field pair is dropped, since
793
+ * none of those describes a join a reader could follow.
794
+ */
795
+ export function readCollectionRelatedTo(collection) {
796
+ const value = collection[QUAC_RELATED_TO_FIELD];
797
+ if (!Array.isArray(value))
798
+ return [];
799
+ const relations = [];
800
+ for (const entry of value) {
801
+ if (!entry || typeof entry !== 'object')
802
+ continue;
803
+ const candidate = entry;
804
+ const id = readTrimmedString(candidate.id);
805
+ const parentCollectionId = readTrimmedString(candidate.parent_collection_id);
806
+ const fields = readRelationFields(candidate.fields);
807
+ if (!id || !parentCollectionId || fields.length === 0)
808
+ continue;
809
+ relations.push({ id, name: readTrimmedString(candidate.name), parentCollectionId, fields });
810
+ }
811
+ return relations;
812
+ }
813
+ function readRelationFields(value) {
814
+ if (!Array.isArray(value))
815
+ return [];
816
+ const fields = [];
817
+ for (const entry of value) {
818
+ if (!entry || typeof entry !== 'object')
819
+ continue;
820
+ const candidate = entry;
821
+ const parent = readTrimmedString(candidate.parent);
822
+ const child = readTrimmedString(candidate.child);
823
+ if (!parent || !child)
824
+ continue;
825
+ fields.push({ parent, child });
826
+ }
827
+ return fields;
828
+ }
829
+ function readTrimmedString(value) {
830
+ return typeof value === 'string' ? value.trim() : '';
831
+ }
832
+ /** The box covering every given box, or null when none is given. */
833
+ export function unionBboxes(boxes) {
834
+ let union = null;
835
+ for (const box of boxes) {
836
+ if (!box)
837
+ continue;
838
+ union = union
839
+ ? {
840
+ xmin: Math.min(union.xmin, box.xmin),
841
+ ymin: Math.min(union.ymin, box.ymin),
842
+ xmax: Math.max(union.xmax, box.xmax),
843
+ ymax: Math.max(union.ymax, box.ymax)
844
+ }
845
+ : { ...box };
846
+ }
847
+ return union;
848
+ }
849
+ /**
850
+ * STAC's `extent.spatial.bbox` is an array of bboxes. We emit one, falling
851
+ * back to the world extent when no bbox is known.
852
+ *
853
+ * `temporal` is required on a Collection extent, so it is always emitted.
854
+ * A source that declares no temporal range gets the open interval
855
+ * `[[null, null]]` — the spec's "unbounded at both ends".
856
+ */
857
+ export function buildStacExtent(bbox, interval = [null, null]) {
858
+ return {
859
+ spatial: {
860
+ bbox: bbox
861
+ ? [[bbox.xmin, bbox.ymin, bbox.xmax, bbox.ymax]]
862
+ : [[-180, -90, 180, 90]]
863
+ },
864
+ temporal: { interval: [interval] }
865
+ };
866
+ }
867
+ /**
868
+ * Lower-cases, replaces non-alphanumerics with hyphens, collapses repeats.
869
+ * STAC ids should be URL-safe; this is a conservative slugifier.
870
+ */
871
+ export function slugify(input) {
872
+ return input
873
+ .toLowerCase()
874
+ .replace(/[^a-z0-9_-]+/g, '-')
875
+ .replace(/-+/g, '-')
876
+ .replace(/^-|-$/g, '');
877
+ }