@vectoriahq/quac 0.16.0 → 0.17.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.
@@ -286,6 +286,14 @@ export declare function generateStacFromNormalized(project: NormalizedProject):
286
286
  * Server type name this package does not map, gets no `data_type`.
287
287
  */
288
288
  export declare function withDataType(column: TableColumn): TableColumn;
289
+ /**
290
+ * The column with `quac:length` and `quac:precision` as its data defines the
291
+ * field: a length for a text or number column, a precision for a `double`
292
+ * one, each where it is a whole number above nothing. QGIS gives 0 or -1
293
+ * where the data defines none, and a length for other types too, such as 1
294
+ * for a GeoPackage boolean, which says nothing about the values it holds.
295
+ */
296
+ export declare function withFieldSize(column: TableColumn, length: number | undefined, precision: number | undefined): TableColumn;
289
297
  /**
290
298
  * Reads `quac:active_collection` back off a Catalog: the id of the Collection
291
299
  * the map opens with as the active layer, or null where the source names none.
@@ -339,6 +339,27 @@ export function withDataType(column) {
339
339
  return column;
340
340
  return { ...column, data_type: DATA_TYPE_BY_COLUMN_TYPE[type] };
341
341
  }
342
+ /** The column types a length applies to: text and numbers. */
343
+ const SIZED_COLUMN_TYPES = new Set(['string', 'int', 'long', 'double']);
344
+ /**
345
+ * The column with `quac:length` and `quac:precision` as its data defines the
346
+ * field: a length for a text or number column, a precision for a `double`
347
+ * one, each where it is a whole number above nothing. QGIS gives 0 or -1
348
+ * where the data defines none, and a length for other types too, such as 1
349
+ * for a GeoPackage boolean, which says nothing about the values it holds.
350
+ */
351
+ export function withFieldSize(column, length, precision) {
352
+ const sized = { ...column };
353
+ const type = column.type ?? '';
354
+ if (SIZED_COLUMN_TYPES.has(type) && isWholeAboveNothing(length))
355
+ sized['quac:length'] = length;
356
+ if (type === 'double' && isWholeAboveNothing(precision))
357
+ sized['quac:precision'] = precision;
358
+ return sized;
359
+ }
360
+ function isWholeAboveNothing(value) {
361
+ return Number.isSafeInteger(value) && value > 0;
362
+ }
342
363
  /**
343
364
  * Reads `quac:active_collection` back off a Catalog: the id of the Collection
344
365
  * the map opens with as the active layer, or null where the source names none.
@@ -62,15 +62,29 @@ export declare function generateServerStacCatalogue(project: QgsProject, options
62
62
  * served address would repeat.
63
63
  */
64
64
  export declare function serverWmsEndpoint(serverAddress: string): string | null;
65
- /** A field's type as GetProjectSettings reports it: its Qt type name, such as `qlonglong`, and its provider's type name. */
65
+ /**
66
+ * A field as QGIS gives it. GetProjectSettings reports its Qt type name, such
67
+ * as `qlonglong`, its provider's type name, the comment its data holds, such
68
+ * as a PostgreSQL column comment (`QgsField::comment`), its length and
69
+ * precision (`QgsField::length`, `QgsField::precision`), and whether it is
70
+ * part of the layer's primary key (`QgsVectorLayer::primaryKeyAttributes`).
71
+ * `filledByData` is whether the data gives it a value of its own when a new
72
+ * feature leaves it empty (`QgsVectorDataProvider::defaultValueClause`), which
73
+ * QGIS Server does not report.
74
+ */
66
75
  export interface QgisFieldType {
67
76
  type: string;
68
77
  typeName: string;
78
+ comment?: string;
79
+ length?: number;
80
+ precision?: number;
81
+ filledByData?: boolean;
82
+ primaryKey?: boolean;
69
83
  }
70
84
  export interface ServerCatalogueOptions {
71
85
  /** The address QGIS Server serves the project from, as its own `wmsServiceUrl` gives it. */
72
86
  serverAddress: string;
73
- /** Each layer's field types, by layer id and then field name. */
87
+ /** Each layer's field types and comments, by layer id and then field name. */
74
88
  fieldTypes?: Record<string, Record<string, QgisFieldType>>;
75
89
  /** The WGS84 box of a layer whose project saves no `<wgs84extent>`, by layer id. */
76
90
  layerWgs84Extents?: Record<string, BoundingBox>;
@@ -1,4 +1,4 @@
1
- import { buildStacExtent, unionBboxes, withDataType } from "./normalizedProject.js";
1
+ import { buildStacExtent, unionBboxes, withDataType, withFieldSize } from "./normalizedProject.js";
2
2
  import { serverLayerTree, wmsPublishedLayerIds } from "./serverLayerTree.js";
3
3
  import { generateStacCatalogue, QUAC_CATEGORISED_FIELD } from "./stacCatalogue.js";
4
4
  import { wfsGetFeatureAsset } from "./wfsStacCatalogue.js";
@@ -212,7 +212,7 @@ function serveCollection(source, context) {
212
212
  hiddenOf: (id) => hiddenFieldsOf(context.layersById.get(id)),
213
213
  servedId: (id) => context.servedIds.get(id) ?? null
214
214
  });
215
- serveFieldTypes(shown, context.fieldTypes[layerId] ?? {});
215
+ serveFieldTypes(shown, context.fieldTypes[layerId] ?? {}, context.layersById.get(layerId)?.fieldComments ?? {});
216
216
  return shown;
217
217
  }
218
218
  /**
@@ -295,11 +295,31 @@ function serveColumns(collection, hidden, context) {
295
295
  else
296
296
  delete collection['table:columns'];
297
297
  }
298
- /** Each column with the `type` QGIS Server reports for its field (`withFieldType`). */
299
- function serveFieldTypes(collection, fieldTypes) {
298
+ /**
299
+ * Each column with what QGIS gives for its field (`withFieldType`), and the
300
+ * Collection with its primary key (`primaryKeyOf`).
301
+ */
302
+ function serveFieldTypes(collection, fieldTypes, projectComments) {
300
303
  const columns = collection['table:columns'];
301
- if (Array.isArray(columns))
302
- collection['table:columns'] = columns.map((column) => withFieldType(column, fieldTypes[column.name]));
304
+ if (!Array.isArray(columns))
305
+ return;
306
+ const typed = columns.map((column) => withFieldType(column, fieldTypes[column.name], projectComments));
307
+ collection['table:columns'] = typed;
308
+ const primaryKey = primaryKeyOf(typed, fieldTypes);
309
+ if (primaryKey)
310
+ collection['quac:primary_key'] = primaryKey;
311
+ }
312
+ /**
313
+ * The names of the fields QGIS gives as the layer's primary key, in the
314
+ * columns' order, or null where it gives none or where a key field is not
315
+ * among the columns, such as one hidden: part of a key identifies nothing.
316
+ */
317
+ function primaryKeyOf(columns, fieldTypes) {
318
+ const keys = Object.entries(fieldTypes).filter(([, fieldType]) => fieldType.primaryKey).map(([name]) => name);
319
+ const names = new Set(columns.map((column) => column.name));
320
+ if (keys.length === 0 || keys.some((name) => !names.has(name)))
321
+ return null;
322
+ return columns.filter((column) => keys.includes(column.name)).map((column) => column.name);
303
323
  }
304
324
  /** A style asset's categorised field, dropped where the layer hides it. */
305
325
  function serveStyleFields(collection, hidden) {
@@ -317,10 +337,29 @@ function serveStyleFields(collection, hidden) {
317
337
  }
318
338
  collection.assets = assets;
319
339
  }
320
- /** The column with the `type` QGIS Server reports for its field, and the `data_type` beside it, where QGIS Server reports one. */
321
- function withFieldType(column, fieldType) {
322
- const type = fieldType ? mapQtTypeToStacTableType(fieldType.type, fieldType.typeName) : undefined;
323
- return type ? withDataType({ ...column, type }) : column;
340
+ /**
341
+ * The column with the `type` QGIS Server reports for its field, and the
342
+ * `data_type` beside it, where QGIS Server reports one. Its `description` is
343
+ * the comment the field's data holds where the project saves no comment of its
344
+ * own for the field, `projectComments` holding those, a blank one included:
345
+ * QGIS's form shows the project's comment, hides the data's behind a blank
346
+ * one, and shows the data's otherwise (`QgsAttributeForm`,
347
+ * `src/gui/qgsattributeform.cpp`, `qgis/QGIS@master`, read 2026-11-25). It
348
+ * takes `quac:length` and `quac:precision` (`withFieldSize`), and
349
+ * `quac:filled_by_data` where the data fills the field.
350
+ */
351
+ function withFieldType(column, fieldType, projectComments) {
352
+ if (!fieldType)
353
+ return column;
354
+ const type = mapQtTypeToStacTableType(fieldType.type, fieldType.typeName);
355
+ const typed = type ? withDataType({ ...column, type }) : { ...column };
356
+ const comment = fieldType.comment?.trim();
357
+ if (comment && !Object.hasOwn(projectComments, column.name))
358
+ typed.description = comment;
359
+ const sized = withFieldSize(typed, fieldType.length, fieldType.precision);
360
+ if (fieldType.filledByData)
361
+ sized['quac:filled_by_data'] = true;
362
+ return sized;
324
363
  }
325
364
  function serveColumn(source, hidden, context) {
326
365
  const column = { ...source };
@@ -1,4 +1,4 @@
1
- import { slugify, generateStacFromNormalized } from "./normalizedProject.js";
1
+ import { slugify, generateStacFromNormalized, withFieldSize } from "./normalizedProject.js";
2
2
  import { wfsGetFeatureAsset } from "./wfsStacCatalogue.js";
3
3
  import { chooseWmsCrs, WEB_MAP_LINKS_EXTENSION, wmsEndpoint } from "../model/stacWms.js";
4
4
  export function generateStacCatalogueFromWms(project, options = {}) {
@@ -80,7 +80,7 @@ function wmsLayerToNormalized(layer, wfsSource) {
80
80
  identifiable: layer.queryable,
81
81
  scaleRange: layer.scaleRange,
82
82
  opacity: layer.opacity,
83
- vendorFields: buildVendorFields(wfsSource)
83
+ vendorFields: { ...buildVendorFields(wfsSource), ...primaryKeyField(layer) }
84
84
  };
85
85
  }
86
86
  /**
@@ -146,6 +146,16 @@ function buildVendorFields(wfsSource) {
146
146
  fields['quac:wfs_type_name'] = wfsSource.typeName;
147
147
  return fields;
148
148
  }
149
+ /**
150
+ * The layer's `quac:primary_key`, where QGIS Server lists one and every field
151
+ * of it is among the layer's attributes: part of a key identifies nothing.
152
+ */
153
+ function primaryKeyField(layer) {
154
+ const names = new Set(layer.attributes.map((attribute) => attribute.name));
155
+ if (layer.primaryKey.length === 0 || layer.primaryKey.some((name) => !names.has(name)))
156
+ return {};
157
+ return { 'quac:primary_key': layer.attributes.filter((attribute) => layer.primaryKey.includes(attribute.name)).map((attribute) => attribute.name) };
158
+ }
149
159
  function wmsBboxToNormalized(bbox) {
150
160
  return {
151
161
  xmin: bbox.westBoundLongitude,
@@ -175,7 +185,7 @@ function deriveTableColumnsFromAttributes(attributes) {
175
185
  const stacType = mapQtTypeToStacTableType(attr.type, attr.typeName);
176
186
  if (stacType)
177
187
  column.type = stacType;
178
- return column;
188
+ return withFieldSize(column, attr.length, attr.precision);
179
189
  });
180
190
  }
181
191
  /**
@@ -236,8 +236,9 @@ export interface LayerEntry {
236
236
  fieldAliases: Record<string, string>;
237
237
  /**
238
238
  * Each field's comment from the Attributes Form's Comment, a custom comment
239
- * QGIS 4.2 and later saves in the project as `<customComments>`. Fields with
240
- * none, or a blank one, are absent.
239
+ * QGIS 4.2 and later saves in the project as `<customComments>`. A blank one,
240
+ * which hides the comment the field's data gives, is an empty string. Fields
241
+ * with none are absent.
241
242
  */
242
243
  fieldComments?: Record<string, string>;
243
244
  /**
@@ -190,6 +190,12 @@ export interface TableColumn {
190
190
  'quac:editable_when'?: string;
191
191
  /** The `https://` address of the icon drawn beside the column's name. */
192
192
  'quac:icon'?: string;
193
+ /** The most characters a text column holds, or the most digits a number column holds, as its data defines the field. */
194
+ 'quac:length'?: number;
195
+ /** The most digits after the decimal point a number column holds, as its data defines the field. */
196
+ 'quac:precision'?: number;
197
+ /** Written only as `true`: the data gives the column a value of its own when a new feature leaves it empty. */
198
+ 'quac:filled_by_data'?: true;
193
199
  /** The control the source asks the edit form to draw the column's choices with. */
194
200
  'quac:control'?: 'buttons' | 'dropdown';
195
201
  /** Written only as `true`: the source's form puts the column's name above its value. */
@@ -107,6 +107,8 @@ export interface WmsLayer {
107
107
  styles: WmsStyle[];
108
108
  /** Attribute schema (vector layers; empty for raster). */
109
109
  attributes: WmsAttribute[];
110
+ /** The names of the fields of the layer's primary key, as `<PrimaryKey>` lists them; empty where it lists none. */
111
+ primaryKey: string[];
110
112
  /** Display name in the layer panel. */
111
113
  treeName: string;
112
114
  }
@@ -106,6 +106,8 @@ export function parseDataSource(raw, providerHint = null) {
106
106
  const url = params.url;
107
107
  const typeName = params.typename ?? params.typeName;
108
108
  if (url && typeName) {
109
+ if (wfsSourceSignsIn(params))
110
+ return { kind: 'unsupported', raw, hint: providerHint ?? 'WFS' };
109
111
  // QGIS sends the server a `SELECT` statement where the source has one, and the `filter`
110
112
  // otherwise (`QgsWFSProvider::QgsWFSProvider`, `src/providers/wfs/qgswfsprovider.cpp`,
111
113
  // `qgis/QGIS@master`, read 2026-10-26).
@@ -148,6 +150,14 @@ function needsCredentialsOrHeaders(params) {
148
150
  }
149
151
  return false;
150
152
  }
153
+ /**
154
+ * Whether a WFS source signs in to its server: QGIS reads its login from
155
+ * `authcfg`, or a user name from `user` or `username` and a `password`
156
+ * (`QgsDataSourceUri`, QGIS 3.34).
157
+ */
158
+ function wfsSourceSignsIn(params) {
159
+ return ['authcfg', 'user', 'username', 'password'].some((key) => params[key]?.trim());
160
+ }
151
161
  /**
152
162
  * An XYZ tile layer. QGIS's `{q}` is Bing's quadkey, written as the `{quadkey}`
153
163
  * the tile layer fills in. Null for a template that is not an absolute
@@ -407,12 +417,14 @@ function basename(pathLike) {
407
417
  */
408
418
  function parseSpaceSeparatedKeyValues(raw) {
409
419
  const out = {};
410
- // Match key='...' or key="..."
411
- const re = /(\w+)\s*=\s*(?:'((?:\\.|[^'\\])*)'|"((?:\\.|[^"\\])*)")/g;
420
+ // Match key='...', key="..." or a bare key=value, which is how QGIS writes `authcfg`
421
+ // (`QgsDataSourceUri::uri`, QGIS 3.34).
422
+ // A bare value starts straight after `=`, so a key written with no value never takes the next pair.
423
+ const re = /(\w+)\s*(?:=\s*'((?:\\.|[^'\\])*)'|=\s*"((?:\\.|[^"\\])*)"|=([^\s'"]+))/g;
412
424
  let m;
413
425
  while ((m = re.exec(raw)) !== null) {
414
426
  const key = m[1];
415
- const value = (m[2] ?? m[3] ?? '').replace(/\\(.)/g, '$1');
427
+ const value = m[4] ?? (m[2] ?? m[3] ?? '').replace(/\\(.)/g, '$1');
416
428
  out[key] = value;
417
429
  }
418
430
  return out;
@@ -130,6 +130,10 @@ function parseLeafLayer(layerEl, name, title, bbox, visibilityChecked, treeName)
130
130
  editType: attrEl.getAttribute('editType') ?? '',
131
131
  comment: attrEl.getAttribute('comment') ?? ''
132
132
  })).filter((a) => a.name);
133
+ // QGIS Server lists the key's fields by name, the ones it leaves out of `<Attributes>` among them (`qgswmsgetcapabilities.cpp`, `qgis/QGIS@master`, read 2026-11-25).
134
+ const primaryKey = Array.from(layerEl.querySelectorAll(':scope > PrimaryKey > PrimaryKeyAttribute'))
135
+ .map((node) => node.textContent?.trim() ?? '')
136
+ .filter(Boolean);
133
137
  const crsList = Array.from(layerEl.querySelectorAll(':scope > CRS'))
134
138
  .map((node) => node.textContent?.trim() ?? '')
135
139
  .filter(Boolean);
@@ -147,6 +151,7 @@ function parseLeafLayer(layerEl, name, title, bbox, visibilityChecked, treeName)
147
151
  crsList,
148
152
  styles,
149
153
  attributes,
154
+ primaryKey,
150
155
  treeName
151
156
  };
152
157
  }
@@ -935,8 +935,10 @@ function parseFieldAliases(maplayer) {
935
935
  * (`QgsVectorLayer::readSymbology` and `writeSymbology`,
936
936
  * `src/core/vector/qgsvectorlayer.cpp`, `qgis/QGIS@master`, read 2026-11-19).
937
937
  * A blank comment, which QGIS saves to hide the comment the field's data gives,
938
- * is left out: a project file does not carry the data's own comment. Where two
939
- * entries name a field, the later stands, as QGIS's map of them keeps it.
938
+ * is kept as an empty string, so a producer that has the data's comment knows
939
+ * to leave it out (`QgsAttributeForm`, `src/gui/qgsattributeform.cpp`, same
940
+ * revision, read 2026-11-25). Where two entries name a field, the later
941
+ * stands, as QGIS's map of them keeps it.
940
942
  */
941
943
  function parseFieldComments(maplayer) {
942
944
  const out = nameRecord();
@@ -944,11 +946,7 @@ function parseFieldComments(maplayer) {
944
946
  const field = node.getAttribute('field');
945
947
  if (!field)
946
948
  continue;
947
- const comment = node.getAttribute('value')?.trim();
948
- if (comment)
949
- out[field] = comment;
950
- else
951
- delete out[field];
949
+ out[field] = node.getAttribute('value')?.trim() ?? '';
952
950
  }
953
951
  return out;
954
952
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vectoriahq/quac",
3
- "version": "0.16.0",
3
+ "version": "0.17.0",
4
4
  "license": "Apache-2.0",
5
5
  "type": "module",
6
6
  "description": "Writes and reads QUAC, which adds map layer settings to STAC catalogues.",
@@ -272,6 +272,14 @@
272
272
  "type": "string",
273
273
  "minLength": 1
274
274
  },
275
+ "quac:primary_key": {
276
+ "description": "The names of the fields whose values identify a feature in the layer's data, its primary key. Written from QGIS, which gives a layer's primary key, where every field of the key is among the columns.",
277
+ "type": "array",
278
+ "minItems": 1,
279
+ "items": {
280
+ "$ref": "#/definitions/name"
281
+ }
282
+ },
275
283
  "quac:form_order": {
276
284
  "description": "The names of the fields the source's form layout places, in its order. A column not listed is left out of the form. Written from a QGIS layer's Drag and Drop Designer form.",
277
285
  "type": "array",
@@ -597,6 +605,20 @@
597
605
  "dropdown"
598
606
  ]
599
607
  },
608
+ "quac:length": {
609
+ "description": "The most characters a text field holds, or the most digits a number field holds, as the field's data defines it. Written from QGIS, which gives a field's length.",
610
+ "type": "integer",
611
+ "minimum": 1
612
+ },
613
+ "quac:precision": {
614
+ "description": "The most digits after the decimal point a decimal number field holds, as the field's data defines it. Written from QGIS, which gives a field's precision.",
615
+ "type": "integer",
616
+ "minimum": 1
617
+ },
618
+ "quac:filled_by_data": {
619
+ "description": "The field's data gives it a value of its own, such as the next number of a sequence, when a new feature leaves it empty. Written from QGIS, where the data source states a default for the field.",
620
+ "const": true
621
+ },
600
622
  "quac:label_on_top": {
601
623
  "description": "The form puts the field's name above its value, across the form's width. Written from QGIS's Label on top in the Attributes Form.",
602
624
  "const": true