@vectoriahq/quac 0.8.0 → 0.9.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.
@@ -0,0 +1,27 @@
1
+ import type { QgsProject } from '../model/QgsProject.ts';
2
+ import type { FeatureCollection, Geometry } from 'geojson';
3
+ /**
4
+ * Why a layer's features are not published: the project leaves the layer out
5
+ * of what QGIS Server serves (**Exclude layers**, or a group it excludes), the
6
+ * layer has a feature filter this cannot read, or the project has no such layer.
7
+ */
8
+ export type PublishRefusal = 'excluded' | 'unreadable-filter' | 'unknown-layer';
9
+ export type PublishedLayer<G extends Geometry | null = Geometry | null> = {
10
+ kind: 'published';
11
+ features: FeatureCollection<G>;
12
+ } | {
13
+ kind: 'refused';
14
+ reason: PublishRefusal;
15
+ };
16
+ /**
17
+ * A layer's features as QGIS Server would publish them from the project: none
18
+ * for a layer it does not serve, only those the layer's feature filter keeps,
19
+ * and without the fields it hides from WMS or WFS. For a layer whose data
20
+ * Groundwork, not QGIS Server, hands to the public, so the public gets no more
21
+ * than a QGIS Server would give them.
22
+ *
23
+ * It fails closed: a layer the project leaves out, or one whose filter is not a
24
+ * GDAL SQL filter this reads (`readSqlFeatureTest`), is refused rather than
25
+ * published whole. The layer is matched by its QGIS layer id.
26
+ */
27
+ export declare function publishLayerFeatures<G extends Geometry | null>(project: QgsProject, layerId: string, collection: FeatureCollection<G>): PublishedLayer<G>;
@@ -0,0 +1,35 @@
1
+ import { serverLayerTree, wmsPublishedLayerIds } from "./serverLayerTree.js";
2
+ import { readSqlFeatureTest } from "../parse/sqlFeatureFilter.js";
3
+ /**
4
+ * A layer's features as QGIS Server would publish them from the project: none
5
+ * for a layer it does not serve, only those the layer's feature filter keeps,
6
+ * and without the fields it hides from WMS or WFS. For a layer whose data
7
+ * Groundwork, not QGIS Server, hands to the public, so the public gets no more
8
+ * than a QGIS Server would give them.
9
+ *
10
+ * It fails closed: a layer the project leaves out, or one whose filter is not a
11
+ * GDAL SQL filter this reads (`readSqlFeatureTest`), is refused rather than
12
+ * published whole. The layer is matched by its QGIS layer id.
13
+ */
14
+ export function publishLayerFeatures(project, layerId, collection) {
15
+ const layer = project.layers.find((candidate) => candidate.id === layerId);
16
+ if (!layer)
17
+ return { kind: 'refused', reason: 'unknown-layer' };
18
+ if (!wmsPublishedLayerIds(serverLayerTree(project)).has(layer.id))
19
+ return { kind: 'refused', reason: 'excluded' };
20
+ const filter = 'featureFilter' in layer.dataSource ? layer.dataSource.featureFilter ?? null : null;
21
+ const test = filter ? readSqlFeatureTest(filter) : null;
22
+ if (test?.kind === 'unreadable')
23
+ return { kind: 'refused', reason: 'unreadable-filter' };
24
+ const hidden = new Set([...(layer.serverHiddenFields?.wms ?? []), ...(layer.serverHiddenFields?.wfs ?? [])]);
25
+ const features = collection.features
26
+ .filter((feature) => !test || test.keeps(feature.properties))
27
+ .map((feature) => withoutFields(feature, hidden));
28
+ return { kind: 'published', features: { ...collection, features } };
29
+ }
30
+ function withoutFields(feature, hidden) {
31
+ if (hidden.size === 0 || !feature.properties)
32
+ return feature;
33
+ const properties = Object.fromEntries(Object.entries(feature.properties).filter(([name]) => !hidden.has(name)));
34
+ return { ...feature, properties };
35
+ }
package/dist/index.d.ts CHANGED
@@ -19,6 +19,10 @@ export { qmlForLayer } from './generate/qmlForLayer.ts';
19
19
  export { generateStacCatalogue, parseQgisStyleAssetHref, isQgisPlaceholderDescription, QUAC_GEOMETRY_FILTER, QUAC_LAYER_NAME, readCollectionGeometryFilter, readCollectionLayerName } from './generate/stacCatalogue.ts';
20
20
  export { DEFAULT_DELIMITED_TEXT_OPTIONS, QUAC_DELIMITED_TEXT, readCollectionDelimitedText } from './model/delimitedText.ts';
21
21
  export { QUAC_FEATURE_FILTER, readCollectionFeatureFilter } from './model/featureFilter.ts';
22
+ export { readSqlFeatureTest } from './parse/sqlFeatureFilter.ts';
23
+ export type { FeatureTest } from './parse/sqlFeatureFilter.ts';
24
+ export { publishLayerFeatures } from './generate/publishLayer.ts';
25
+ export type { PublishedLayer, PublishRefusal } from './generate/publishLayer.ts';
22
26
  export type { FeatureFilter, FeatureFilterDialect } from './model/featureFilter.ts';
23
27
  export type { DelimitedTextOptions } from './model/delimitedText.ts';
24
28
  export { decodeAssetPath, encodeAssetPath, isRelativeHref, readCollectionDataPath, readCollectionOnlineFile } from './model/stacHref.ts';
package/dist/index.js CHANGED
@@ -15,6 +15,8 @@ export { qmlForLayer } from "./generate/qmlForLayer.js";
15
15
  export { generateStacCatalogue, parseQgisStyleAssetHref, isQgisPlaceholderDescription, QUAC_GEOMETRY_FILTER, QUAC_LAYER_NAME, readCollectionGeometryFilter, readCollectionLayerName } from "./generate/stacCatalogue.js";
16
16
  export { DEFAULT_DELIMITED_TEXT_OPTIONS, QUAC_DELIMITED_TEXT, readCollectionDelimitedText } from "./model/delimitedText.js";
17
17
  export { QUAC_FEATURE_FILTER, readCollectionFeatureFilter } from "./model/featureFilter.js";
18
+ export { readSqlFeatureTest } from "./parse/sqlFeatureFilter.js";
19
+ export { publishLayerFeatures } from "./generate/publishLayer.js";
18
20
  export { decodeAssetPath, encodeAssetPath, isRelativeHref, readCollectionDataPath, readCollectionOnlineFile } from "./model/stacHref.js";
19
21
  export { DATA_ROLE, findAssetByRole, readDataAsset } from "./model/stacAssetRoles.js";
20
22
  export { THUMBNAIL_ROLE, readCollectionAttribution, readCollectionDeprecation, readCollectionSelfHref, readCollectionThumbnail } from "./model/stacCollectionNotes.js";
@@ -0,0 +1,34 @@
1
+ import type { FeatureFilter } from '../model/featureFilter.ts';
2
+ /**
3
+ * A layer's feature filter as a test of a feature's properties: `keeps` says
4
+ * whether the layer holds a feature. Unreadable where the filter holds
5
+ * something this does not read, so a caller leaves the layer out rather than
6
+ * show it whole.
7
+ */
8
+ export type FeatureTest = {
9
+ kind: 'readable';
10
+ filter: FeatureFilter;
11
+ keeps: (properties: Readonly<Record<string, unknown>> | null | undefined) => boolean;
12
+ } | {
13
+ kind: 'unreadable';
14
+ expression: string;
15
+ };
16
+ /**
17
+ * A GDAL SQL filter (`ogr_sql`, the Provider Feature Filter of a layer QGIS
18
+ * reads through OGR, such as a GeoJSON file) as a feature test.
19
+ *
20
+ * It reads the part of GDAL's SQL that a QGIS expression writes and means
21
+ * alike: fields, text, numbers, `NULL`, the comparisons, `IS [NOT] NULL`,
22
+ * `LIKE`, `ILIKE`, `IN`, `BETWEEN`, `AND`, `OR` and `NOT`. A function,
23
+ * arithmetic, a boolean literal, `~` or a whole `SELECT` makes it unreadable,
24
+ * as does any other dialect. `=`, `<>`, `!=` and `IN` against text with case
25
+ * ignore case, as GDAL's string equality does (`ogr_sql_dialect.html` §
26
+ * "WHERE", gdal.org, read 2026-10-26).
27
+ *
28
+ * Values compare as a QGIS expression compares them: a comparison with a NULL
29
+ * is unknown, numbers compare as numbers unless both sides are text, and
30
+ * `AND`, `OR` and `NOT` follow three-valued logic. A feature is kept where the
31
+ * filter comes out true. A field is found by its name, then by its name
32
+ * ignoring case.
33
+ */
34
+ export declare function readSqlFeatureTest(filter: FeatureFilter): FeatureTest;
@@ -0,0 +1,253 @@
1
+ import { readQgisExpression } from "./qgisExpression.js";
2
+ /**
3
+ * A GDAL SQL filter (`ogr_sql`, the Provider Feature Filter of a layer QGIS
4
+ * reads through OGR, such as a GeoJSON file) as a feature test.
5
+ *
6
+ * It reads the part of GDAL's SQL that a QGIS expression writes and means
7
+ * alike: fields, text, numbers, `NULL`, the comparisons, `IS [NOT] NULL`,
8
+ * `LIKE`, `ILIKE`, `IN`, `BETWEEN`, `AND`, `OR` and `NOT`. A function,
9
+ * arithmetic, a boolean literal, `~` or a whole `SELECT` makes it unreadable,
10
+ * as does any other dialect. `=`, `<>`, `!=` and `IN` against text with case
11
+ * ignore case, as GDAL's string equality does (`ogr_sql_dialect.html` §
12
+ * "WHERE", gdal.org, read 2026-10-26).
13
+ *
14
+ * Values compare as a QGIS expression compares them: a comparison with a NULL
15
+ * is unknown, numbers compare as numbers unless both sides are text, and
16
+ * `AND`, `OR` and `NOT` follow three-valued logic. A feature is kept where the
17
+ * filter comes out true. A field is found by its name, then by its name
18
+ * ignoring case.
19
+ */
20
+ export function readSqlFeatureTest(filter) {
21
+ const unreadable = { kind: 'unreadable', expression: filter.expression };
22
+ if (filter.dialect !== 'ogr_sql')
23
+ return unreadable;
24
+ const node = readQgisExpression(filter.expression);
25
+ if (!node || !isSharedSql(node))
26
+ return unreadable;
27
+ return {
28
+ kind: 'readable',
29
+ filter,
30
+ keeps: (properties) => {
31
+ const value = evaluate(node, properties ?? {});
32
+ return value !== null && toBoolean(value);
33
+ }
34
+ };
35
+ }
36
+ const SHARED_PREDICATES = new Set(['is', 'like', 'ilike', 'in', 'between']);
37
+ /** Whether a node holds only what GDAL's SQL and a QGIS expression write and mean alike. */
38
+ function isSharedSql(node) {
39
+ switch (node.kind) {
40
+ case 'field':
41
+ case 'text':
42
+ case 'number':
43
+ case 'null':
44
+ return true;
45
+ case 'compare':
46
+ return isSharedSql(node.left) && isSharedSql(node.right);
47
+ case 'logic':
48
+ return node.args.every(isSharedSql);
49
+ case 'not':
50
+ return isSharedSql(node.arg);
51
+ case 'predicate':
52
+ // GDAL's SQL has no `~`, so only these predicates mean the same in both.
53
+ if (!SHARED_PREDICATES.has(node.op))
54
+ return false;
55
+ if (node.op === 'is' && node.args[1]?.kind !== 'null')
56
+ return false;
57
+ return node.args.every(isSharedSql);
58
+ default:
59
+ return false;
60
+ }
61
+ }
62
+ /** A field's value: by its name, then by its name ignoring case; null where the feature has neither. */
63
+ function fieldValue(properties, name) {
64
+ if (Object.hasOwn(properties, name))
65
+ return properties[name] ?? null;
66
+ const lower = name.toLowerCase();
67
+ const match = Object.keys(properties).find((key) => key.toLowerCase() === lower);
68
+ return match === undefined ? null : properties[match] ?? null;
69
+ }
70
+ /** Text whose upper and lower case differ, which is all that ignoring case changes. */
71
+ function isCasedText(node) {
72
+ return node.kind === 'text' && node.value.toLowerCase() !== node.value.toUpperCase();
73
+ }
74
+ /** A value in lower case, as text; null stays null. */
75
+ function lowered(value) {
76
+ return isNull(value) ? null : toText(value).toLowerCase();
77
+ }
78
+ /** A node's value: a condition gives 1, 0 or null, as QGIS gives one. */
79
+ function evaluate(node, properties) {
80
+ switch (node.kind) {
81
+ case 'field':
82
+ return fieldValue(properties, node.name);
83
+ case 'text':
84
+ case 'number':
85
+ return node.value;
86
+ case 'null':
87
+ return null;
88
+ case 'compare': {
89
+ const caseless = (node.op === '=' || node.op === '<>' || node.op === '!=') && (isCasedText(node.left) || isCasedText(node.right));
90
+ const read = (side) => (caseless ? lowered(evaluate(side, properties)) : evaluate(side, properties));
91
+ return fromTruth(compareValues(node.op, read(node.left), read(node.right)));
92
+ }
93
+ case 'logic':
94
+ return fromTruth(combineTruths(node.args.map((arg) => toTruth(evaluate(arg, properties))), node.op === 'or'));
95
+ case 'not': {
96
+ const truth = toTruth(evaluate(node.arg, properties));
97
+ return fromTruth(truth === null ? null : !truth);
98
+ }
99
+ case 'predicate': {
100
+ const truth = predicateTruth(node, properties);
101
+ return fromTruth(node.negate && truth !== null ? !truth : truth);
102
+ }
103
+ default:
104
+ return null;
105
+ }
106
+ }
107
+ function predicateTruth(node, properties) {
108
+ if (node.op === 'in') {
109
+ const caseless = node.args.slice(1).some(isCasedText);
110
+ const [value, ...list] = node.args.map((arg) => (caseless ? lowered(evaluate(arg, properties)) : evaluate(arg, properties)));
111
+ return isIn(value, list);
112
+ }
113
+ const [value, second, third] = node.args.map((arg) => evaluate(arg, properties));
114
+ switch (node.op) {
115
+ case 'is':
116
+ // Only `IS NULL` is read, so the other side is always null.
117
+ return isNull(value);
118
+ case 'like':
119
+ case 'ilike':
120
+ if (isNull(value) || isNull(second))
121
+ return null;
122
+ return likePattern(toText(second), node.op === 'ilike').test(toText(value));
123
+ case 'between':
124
+ if (isNull(value))
125
+ return null;
126
+ return combineTruths([compareValues('>=', value, second), compareValues('<=', value, third)], false);
127
+ default:
128
+ return null;
129
+ }
130
+ }
131
+ function isNull(value) {
132
+ return value === null || value === undefined;
133
+ }
134
+ function toText(value) {
135
+ return typeof value === 'string' ? value : String(value);
136
+ }
137
+ /** Text that Qt's `QString::toDouble` reads as a number: decimal digits, an optional exponent, and spaces at either end. */
138
+ const NUMBER_TEXT = /^\s*[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?\s*$/;
139
+ /** QGIS's `qgsDoubleNear` tolerance: four times the gap between 1 and the next double. */
140
+ const DOUBLE_NEAR = 4 * Number.EPSILON;
141
+ /** A number as QGIS reads one: a number, or text that is one. Null for anything else. */
142
+ function toNumber(value) {
143
+ if (typeof value === 'number')
144
+ return Number.isFinite(value) ? value : null;
145
+ if (typeof value !== 'string' || !NUMBER_TEXT.test(value))
146
+ return null;
147
+ const number = Number(value);
148
+ return Number.isFinite(number) ? number : null;
149
+ }
150
+ /** A value as QGIS's `toBool` reads it: false for null, 0, and for blank text, `0` and `false` in any case. */
151
+ function toBoolean(value) {
152
+ if (typeof value === 'string')
153
+ return !['', '0', 'false'].includes(value.toLowerCase());
154
+ return Boolean(value);
155
+ }
156
+ /** A value as QGIS's `getTVLValue` reads it for a condition: unknown for null, and for text that is not a number. */
157
+ function toTruth(value) {
158
+ if (isNull(value))
159
+ return null;
160
+ if (typeof value === 'boolean')
161
+ return value;
162
+ const number = toNumber(value);
163
+ return number === null ? null : Math.abs(number) > DOUBLE_NEAR;
164
+ }
165
+ function fromTruth(truth) {
166
+ return truth === null ? null : truth ? 1 : 0;
167
+ }
168
+ /** `AND` (deciding false) or `OR` (deciding true) over three-valued truths. */
169
+ function combineTruths(truths, deciding) {
170
+ if (truths.includes(deciding))
171
+ return deciding;
172
+ return truths.includes(null) ? null : !deciding;
173
+ }
174
+ /**
175
+ * QGIS's `compareNonNullValues` for two values that are not null, as a
176
+ * difference: as numbers when both read as numbers and they are not both text,
177
+ * as true and false when either is a boolean, and otherwise as text.
178
+ */
179
+ function compareNonNull(left, right) {
180
+ const leftNumber = toNumber(left);
181
+ const rightNumber = toNumber(right);
182
+ if (!(typeof left === 'string' && typeof right === 'string') && leftNumber !== null && rightNumber !== null) {
183
+ return { diff: leftNumber - rightNumber, numeric: true };
184
+ }
185
+ if (typeof left === 'boolean' || typeof right === 'boolean') {
186
+ return { diff: Number(toBoolean(left)) - Number(toBoolean(right)), numeric: false };
187
+ }
188
+ const leftText = toText(left);
189
+ const rightText = toText(right);
190
+ return { diff: leftText < rightText ? -1 : leftText > rightText ? 1 : 0, numeric: false };
191
+ }
192
+ const COMPARISONS = {
193
+ '=': (diff, numeric) => (numeric ? Math.abs(diff) <= DOUBLE_NEAR : diff === 0),
194
+ '<>': (diff, numeric) => (numeric ? Math.abs(diff) > DOUBLE_NEAR : diff !== 0),
195
+ '!=': (diff, numeric) => (numeric ? Math.abs(diff) > DOUBLE_NEAR : diff !== 0),
196
+ '<': (diff) => diff < 0,
197
+ '<=': (diff) => diff <= 0,
198
+ '>': (diff) => diff > 0,
199
+ '>=': (diff) => diff >= 0
200
+ };
201
+ /** A comparison of two values: unknown when either is null. */
202
+ function compareValues(op, left, right) {
203
+ if (isNull(left) || isNull(right))
204
+ return null;
205
+ const { diff, numeric } = compareNonNull(left, right);
206
+ return COMPARISONS[op](diff, numeric);
207
+ }
208
+ /**
209
+ * QGIS's `IN`: unknown for a null value, true when a list item equals it, as
210
+ * numbers when both read as numbers and are not both text and as text
211
+ * otherwise, and unknown rather than false when the list holds a null.
212
+ */
213
+ function isIn(value, list) {
214
+ if (isNull(value))
215
+ return null;
216
+ let listHasNull = false;
217
+ for (const item of list) {
218
+ if (isNull(item)) {
219
+ listHasNull = true;
220
+ continue;
221
+ }
222
+ const valueNumber = toNumber(value);
223
+ const itemNumber = toNumber(item);
224
+ const equal = !(typeof value === 'string' && typeof item === 'string') && valueNumber !== null && itemNumber !== null
225
+ ? Math.abs(valueNumber - itemNumber) <= DOUBLE_NEAR
226
+ : toText(value) === toText(item);
227
+ if (equal)
228
+ return true;
229
+ }
230
+ return listHasNull ? null : false;
231
+ }
232
+ /** A `LIKE` pattern as a regular expression: `%` any run, `_` one character, and `\%` or `\_` the character itself. */
233
+ function likePattern(pattern, caseInsensitive) {
234
+ let source = '';
235
+ for (let index = 0; index < pattern.length; index += 1) {
236
+ const char = pattern[index];
237
+ const following = pattern[index + 1];
238
+ if (char === '\\' && (following === '%' || following === '_')) {
239
+ source += following;
240
+ index += 1;
241
+ }
242
+ else if (char === '%') {
243
+ source += '[\\s\\S]*';
244
+ }
245
+ else if (char === '_') {
246
+ source += '[\\s\\S]';
247
+ }
248
+ else {
249
+ source += char.replace(/[\\^$.*+?()[\]{}|/]/g, '\\$&');
250
+ }
251
+ }
252
+ return new RegExp(`^(?:${source})$`, caseInsensitive ? 'iu' : 'u');
253
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vectoriahq/quac",
3
- "version": "0.8.0",
3
+ "version": "0.9.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.",