@vectoriahq/quac 0.0.0-stage → 0.1.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/LICENSE +202 -0
- package/README.md +35 -2
- package/dist/generate/arcgisStacCatalogue.d.ts +46 -0
- package/dist/generate/arcgisStacCatalogue.js +189 -0
- package/dist/generate/externalStacCatalogue.d.ts +53 -0
- package/dist/generate/externalStacCatalogue.js +145 -0
- package/dist/generate/fileLayerCollection.d.ts +44 -0
- package/dist/generate/fileLayerCollection.js +72 -0
- package/dist/generate/folderStacCatalogue.d.ts +48 -0
- package/dist/generate/folderStacCatalogue.js +69 -0
- package/dist/generate/normalizedProject.d.ts +451 -0
- package/dist/generate/normalizedProject.js +925 -0
- package/dist/generate/ogcFeaturesStacCatalogue.d.ts +49 -0
- package/dist/generate/ogcFeaturesStacCatalogue.js +207 -0
- package/dist/generate/qmlForLayer.d.ts +17 -0
- package/dist/generate/qmlForLayer.js +36 -0
- package/dist/generate/serverLayerTree.d.ts +44 -0
- package/dist/generate/serverLayerTree.js +50 -0
- package/dist/generate/serverStacCatalogue.d.ts +84 -0
- package/dist/generate/serverStacCatalogue.js +327 -0
- package/dist/generate/stacCatalogue.d.ts +110 -0
- package/dist/generate/stacCatalogue.js +882 -0
- package/dist/generate/wfsStacCatalogue.d.ts +37 -0
- package/dist/generate/wfsStacCatalogue.js +127 -0
- package/dist/generate/wmsStacCatalogue.d.ts +46 -0
- package/dist/generate/wmsStacCatalogue.js +228 -0
- package/dist/index.d.ts +77 -0
- package/dist/index.js +52 -0
- package/dist/model/QgsProject.d.ts +722 -0
- package/dist/model/QgsProject.js +15 -0
- package/dist/model/Stac.d.ts +320 -0
- package/dist/model/Stac.js +16 -0
- package/dist/model/WmsProject.d.ts +105 -0
- package/dist/model/WmsProject.js +1 -0
- package/dist/model/arcgis.d.ts +134 -0
- package/dist/model/arcgis.js +1 -0
- package/dist/model/delimitedText.d.ts +38 -0
- package/dist/model/delimitedText.js +46 -0
- package/dist/model/featureFilter.d.ts +41 -0
- package/dist/model/featureFilter.js +24 -0
- package/dist/model/fieldIconAddress.d.ts +27 -0
- package/dist/model/fieldIconAddress.js +93 -0
- package/dist/model/ogcFeatures.d.ts +40 -0
- package/dist/model/ogcFeatures.js +6 -0
- package/dist/model/quacExtension.d.ts +28 -0
- package/dist/model/quacExtension.js +75 -0
- package/dist/model/stacAssetRoles.d.ts +33 -0
- package/dist/model/stacAssetRoles.js +44 -0
- package/dist/model/stacCollectionNotes.d.ts +21 -0
- package/dist/model/stacCollectionNotes.js +44 -0
- package/dist/model/stacHref.d.ts +56 -0
- package/dist/model/stacHref.js +93 -0
- package/dist/model/stacPmtiles.d.ts +38 -0
- package/dist/model/stacPmtiles.js +69 -0
- package/dist/model/stacSizeAndCount.d.ts +14 -0
- package/dist/model/stacSizeAndCount.js +20 -0
- package/dist/model/stacStyles.d.ts +93 -0
- package/dist/model/stacStyles.js +87 -0
- package/dist/model/stacTreeWalk.d.ts +36 -0
- package/dist/model/stacTreeWalk.js +76 -0
- package/dist/model/stacWms.d.ts +18 -0
- package/dist/model/stacWms.js +73 -0
- package/dist/model/stacWmts.d.ts +34 -0
- package/dist/model/stacWmts.js +90 -0
- package/dist/model/stacXyz.d.ts +24 -0
- package/dist/model/stacXyz.js +46 -0
- package/dist/model/wfs.d.ts +136 -0
- package/dist/model/wfs.js +1 -0
- package/dist/parse/collectLayerVisibility.d.ts +10 -0
- package/dist/parse/collectLayerVisibility.js +22 -0
- package/dist/parse/customProperties.d.ts +18 -0
- package/dist/parse/customProperties.js +43 -0
- package/dist/parse/geometrySiblings.d.ts +25 -0
- package/dist/parse/geometrySiblings.js +83 -0
- package/dist/parse/parseBrandColour.d.ts +14 -0
- package/dist/parse/parseBrandColour.js +25 -0
- package/dist/parse/parseDataDefinedEditable.d.ts +38 -0
- package/dist/parse/parseDataDefinedEditable.js +76 -0
- package/dist/parse/parseDataSource.d.ts +53 -0
- package/dist/parse/parseDataSource.js +484 -0
- package/dist/parse/parseExtent.d.ts +17 -0
- package/dist/parse/parseExtent.js +49 -0
- package/dist/parse/parseFormLayout.d.ts +26 -0
- package/dist/parse/parseFormLayout.js +60 -0
- package/dist/parse/parseGetProjectSettings.d.ts +15 -0
- package/dist/parse/parseGetProjectSettings.js +172 -0
- package/dist/parse/parseLayers.d.ts +23 -0
- package/dist/parse/parseLayers.js +1122 -0
- package/dist/parse/parseLegendTree.d.ts +20 -0
- package/dist/parse/parseLegendTree.js +66 -0
- package/dist/parse/parseMeasurement.d.ts +21 -0
- package/dist/parse/parseMeasurement.js +67 -0
- package/dist/parse/parseMergin.d.ts +19 -0
- package/dist/parse/parseMergin.js +51 -0
- package/dist/parse/parseProjectVariables.d.ts +9 -0
- package/dist/parse/parseProjectVariables.js +19 -0
- package/dist/parse/parseQfieldSync.d.ts +47 -0
- package/dist/parse/parseQfieldSync.js +95 -0
- package/dist/parse/parseQgsXml.d.ts +15 -0
- package/dist/parse/parseQgsXml.js +100 -0
- package/dist/parse/parseRelations.d.ts +19 -0
- package/dist/parse/parseRelations.js +64 -0
- package/dist/parse/parseStyle.d.ts +33 -0
- package/dist/parse/parseStyle.js +128 -0
- package/dist/parse/parseWmsPublishing.d.ts +21 -0
- package/dist/parse/parseWmsPublishing.js +38 -0
- package/dist/parse/projectProperties.d.ts +15 -0
- package/dist/parse/projectProperties.js +27 -0
- package/dist/parse/qgisExpression.d.ts +194 -0
- package/dist/parse/qgisExpression.js +859 -0
- package/dist/parse/qtDateFormat.d.ts +74 -0
- package/dist/parse/qtDateFormat.js +324 -0
- package/dist/parse/qtRegex.d.ts +67 -0
- package/dist/parse/qtRegex.js +397 -0
- package/dist/parse/readProjectFile.d.ts +7 -0
- package/dist/parse/readProjectFile.js +27 -0
- package/dist/sources/qgisServerUrl.d.ts +6 -0
- package/dist/sources/qgisServerUrl.js +17 -0
- package/dist/sources/stacDocuments.d.ts +40 -0
- package/dist/sources/stacDocuments.js +85 -0
- package/dist/sources/stacItems.d.ts +109 -0
- package/dist/sources/stacItems.js +287 -0
- package/dist/util/catalogueFileFormats.d.ts +58 -0
- package/dist/util/catalogueFileFormats.js +64 -0
- package/dist/util/devLog.d.ts +11 -0
- package/dist/util/devLog.js +23 -0
- package/dist/util/fileName.d.ts +18 -0
- package/dist/util/fileName.js +45 -0
- package/dist/util/wfsVersions.d.ts +9 -0
- package/dist/util/wfsVersions.js +13 -0
- package/package.json +47 -4
- package/src/schema/quac.schema.json +935 -0
|
@@ -0,0 +1,859 @@
|
|
|
1
|
+
import { isFormattableQtDatePattern } from "./qtDateFormat.js";
|
|
2
|
+
import { compileQtRegex } from "./qtRegex.js";
|
|
3
|
+
/** Words QGIS's lexer reads as keywords rather than field names, ignoring case. */
|
|
4
|
+
const QGIS_KEYWORDS = new Set([
|
|
5
|
+
'and', 'between', 'case', 'else', 'end', 'false', 'ilike', 'in', 'is', 'like', 'not', 'null', 'or', 'then', 'true', 'when'
|
|
6
|
+
]);
|
|
7
|
+
/** The one-argument QGIS functions a label expression may call, and the GeoStyler function each becomes. */
|
|
8
|
+
const ONE_ARGUMENT_FUNCTIONS = {
|
|
9
|
+
upper: 'strToUpperCase',
|
|
10
|
+
lower: 'strToLowerCase',
|
|
11
|
+
title: 'strCapitalize',
|
|
12
|
+
trim: 'strTrim'
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* The QGIS functions with no GeoStyler function of the same meaning, and the
|
|
16
|
+
* fewest and most arguments each takes by position. Each becomes a GeoStyler
|
|
17
|
+
* `custom` function with the QGIS name as its `fnName`.
|
|
18
|
+
*/
|
|
19
|
+
const QGIS_ONLY_FUNCTIONS = {
|
|
20
|
+
coalesce: [1, Infinity],
|
|
21
|
+
if: [3, 3],
|
|
22
|
+
length: [1, 1],
|
|
23
|
+
round: [1, 2],
|
|
24
|
+
format_number: [1, 5],
|
|
25
|
+
left: [2, 2],
|
|
26
|
+
right: [2, 2],
|
|
27
|
+
substr: [2, 3],
|
|
28
|
+
replace: [3, 3],
|
|
29
|
+
current_value: [1, 1],
|
|
30
|
+
to_int: [1, 1],
|
|
31
|
+
to_string: [1, 1],
|
|
32
|
+
lpad: [2, 3],
|
|
33
|
+
maximum: [1, 1],
|
|
34
|
+
minimum: [1, 1],
|
|
35
|
+
format_date: [2, 3],
|
|
36
|
+
now: [0, 0],
|
|
37
|
+
make_interval: [0, 7],
|
|
38
|
+
max: [0, Infinity],
|
|
39
|
+
min: [0, Infinity],
|
|
40
|
+
abs: [1, 1],
|
|
41
|
+
floor: [1, 1],
|
|
42
|
+
ceil: [1, 1],
|
|
43
|
+
to_real: [1, 1],
|
|
44
|
+
rpad: [2, 3],
|
|
45
|
+
strpos: [2, 2],
|
|
46
|
+
nullif: [2, 2],
|
|
47
|
+
wordwrap: [2, 3],
|
|
48
|
+
year: [1, 1],
|
|
49
|
+
month: [1, 1],
|
|
50
|
+
day: [1, 1],
|
|
51
|
+
week: [1, 1],
|
|
52
|
+
day_of_week: [1, 1],
|
|
53
|
+
hour: [1, 1],
|
|
54
|
+
minute: [1, 1],
|
|
55
|
+
second: [1, 1],
|
|
56
|
+
regexp_match: [2, 2],
|
|
57
|
+
regexp_replace: [3, 3],
|
|
58
|
+
regexp_substr: [2, 2],
|
|
59
|
+
array_first: [1, 1],
|
|
60
|
+
array_get: [2, 2],
|
|
61
|
+
overlay_within: [1, 5],
|
|
62
|
+
overlay_intersects: [1, 5],
|
|
63
|
+
aggregate: [3, 6],
|
|
64
|
+
geometry: [1, 1],
|
|
65
|
+
intersects: [2, 2]
|
|
66
|
+
};
|
|
67
|
+
/** The functions whose second argument is a regular expression, which QGIS compiles with Unicode properties. */
|
|
68
|
+
const REGEXP_FUNCTIONS = ['regexp_match', 'regexp_replace', 'regexp_substr'];
|
|
69
|
+
/**
|
|
70
|
+
* The names of the functions that take a named value, such as
|
|
71
|
+
* `make_interval(days:=14)`, in the order QGIS gives them by position, and the
|
|
72
|
+
* value each takes when it is left out (`QgsExpressionFunction::Parameter`,
|
|
73
|
+
* `src/core/expression/qgsexpressionfunction.cpp`, `qgis/QGIS@master`, read
|
|
74
|
+
* 2026-10-27).
|
|
75
|
+
*/
|
|
76
|
+
const NAMED_PARAMETERS = {
|
|
77
|
+
make_interval: { names: ['years', 'months', 'weeks', 'days', 'hours', 'minutes', 'seconds'], omitted: { kind: 'number', value: 0 } },
|
|
78
|
+
// QGIS's own lists go on (`min_overlap` and others); a value for any of
|
|
79
|
+
// those leaves the expression unread.
|
|
80
|
+
overlay_within: { names: ['layer', 'expression', 'filter', 'limit', 'cache'], omitted: { kind: 'null' } },
|
|
81
|
+
overlay_intersects: { names: ['layer', 'expression', 'filter', 'limit', 'cache'], omitted: { kind: 'null' } },
|
|
82
|
+
aggregate: { names: ['layer', 'aggregate', 'expression', 'filter', 'concatenator', 'order_by'], omitted: { kind: 'null' } }
|
|
83
|
+
};
|
|
84
|
+
/** The calls that read another layer of the project: `LayerLookup`. */
|
|
85
|
+
const LOOKUPS = ['overlay_within', 'overlay_intersects', 'aggregate'];
|
|
86
|
+
/**
|
|
87
|
+
* What `aggregate` reads the filter `intersects($geometry, geometry(@parent))`
|
|
88
|
+
* through, which stand nowhere else.
|
|
89
|
+
*/
|
|
90
|
+
const PARENT_GEOMETRY_CALLS = ['intersects', 'geometry', '$geometry', '@parent'];
|
|
91
|
+
/** The `aggregate` kinds Groundwork works out (`QgsAggregateCalculator::aggregates`). */
|
|
92
|
+
export const LOOKUP_AGGREGATES = ['count', 'min', 'max', 'concatenate'];
|
|
93
|
+
/**
|
|
94
|
+
* The `fnName` of the GeoStyler `custom` function that stands for a
|
|
95
|
+
* `LayerLookup`, with its `key` as its one argument. The evaluator is handed
|
|
96
|
+
* the answers before it starts.
|
|
97
|
+
*/
|
|
98
|
+
export const LAYER_LOOKUP = 'layer_lookup';
|
|
99
|
+
/** The functions that take a date as their first argument, where `now()` may stand. */
|
|
100
|
+
const DATE_TAKERS = ['format_date', 'year', 'month', 'day', 'week', 'day_of_week', 'hour', 'minute', 'second'];
|
|
101
|
+
/** The aggregates Groundwork reads, which scan the records of a layer. */
|
|
102
|
+
const AGGREGATES = ['maximum', 'minimum'];
|
|
103
|
+
/**
|
|
104
|
+
* The values QGIS gives a feature from its geometry that Groundwork reads, and
|
|
105
|
+
* the unit each is in: an area or a distance. They are written with no
|
|
106
|
+
* brackets (`$area`).
|
|
107
|
+
*/
|
|
108
|
+
export const GEOMETRY_VALUES = {
|
|
109
|
+
$area: 'area',
|
|
110
|
+
$length: 'distance',
|
|
111
|
+
$perimeter: 'distance'
|
|
112
|
+
};
|
|
113
|
+
/** The GeoStyler function each comparison becomes. */
|
|
114
|
+
const COMPARISON_FUNCTIONS = {
|
|
115
|
+
'=': 'equalTo',
|
|
116
|
+
'!=': 'notEqualTo',
|
|
117
|
+
'<>': 'notEqualTo',
|
|
118
|
+
'<': 'lessThan',
|
|
119
|
+
'<=': 'lessThanOrEqualTo',
|
|
120
|
+
'>': 'greaterThan',
|
|
121
|
+
'>=': 'greaterThanOrEqualTo'
|
|
122
|
+
};
|
|
123
|
+
/** The GeoStyler function each arithmetic operator becomes. */
|
|
124
|
+
const ARITHMETIC_FUNCTIONS = {
|
|
125
|
+
'+': 'add',
|
|
126
|
+
'-': 'sub',
|
|
127
|
+
'*': 'mul',
|
|
128
|
+
'/': 'div',
|
|
129
|
+
'//': 'intdiv',
|
|
130
|
+
'%': 'mod',
|
|
131
|
+
'^': 'pow'
|
|
132
|
+
};
|
|
133
|
+
/**
|
|
134
|
+
* The `fnName` of the GeoStyler `custom` function that wraps a condition read
|
|
135
|
+
* from a QGIS rule, which `src/style/labelFunction.ts` works out with QGIS's
|
|
136
|
+
* rules.
|
|
137
|
+
*/
|
|
138
|
+
export const QGIS_CONDITION = 'qgis_condition';
|
|
139
|
+
/** Whether a filter is a condition `qgisConditionToFilter` wrapped. */
|
|
140
|
+
export function isQgisConditionFilter(filter) {
|
|
141
|
+
const call = filter;
|
|
142
|
+
return typeof call === 'object' && call !== null && call.name === 'custom' && call.fnName === QGIS_CONDITION && Array.isArray(call.args);
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Reads a QGIS expression into a tree: fields, text, numbers, `TRUE`, `FALSE`,
|
|
146
|
+
* `NULL`, `||`, `+`, `-`, `*`, `/`, `//`, `%`, `^`, conditions, `~`, `CASE`
|
|
147
|
+
* and calls to the functions below, `current_value` among them. Null for any
|
|
148
|
+
* other expression, such as another function, a named argument to a function
|
|
149
|
+
* outside `NAMED_PARAMETERS`, or a variable.
|
|
150
|
+
*
|
|
151
|
+
* A regular expression, the pattern of `~` or of a `regexp_` function, is read
|
|
152
|
+
* where it is text written into the expression that `compileQtRegex` can
|
|
153
|
+
* translate (`qtRegex.ts`).
|
|
154
|
+
*
|
|
155
|
+
* `make_interval` is read only as the right side of a `+` or `-`, where the
|
|
156
|
+
* evaluator adds it to a date or takes it away. QGIS reads an interval
|
|
157
|
+
* elsewhere too, which the evaluator does not.
|
|
158
|
+
*
|
|
159
|
+
* `overlay_within`, `overlay_intersects` and `aggregate` read another layer,
|
|
160
|
+
* and are read only where the caller says `lookups`: the caller then answers
|
|
161
|
+
* each `LayerLookup` before it evaluates (a default value). See
|
|
162
|
+
* `areLookupsReadable` for the forms read.
|
|
163
|
+
*
|
|
164
|
+
* `current_value` is read only where the caller says `currentValue`: the
|
|
165
|
+
* caller then supplies the form's values (a Value Relation's filter), or reads
|
|
166
|
+
* the expression where there is no form, which gives NULL (a label, a style
|
|
167
|
+
* rule). A constraint or a default value is not read when it calls it.
|
|
168
|
+
*
|
|
169
|
+
* `$area`, `$length` and `$perimeter` are read only where the caller says
|
|
170
|
+
* `geometry`: `true` reads their shape alone, as the project reader does before
|
|
171
|
+
* the units are known, and the project's `MeasureFactors` read them in those
|
|
172
|
+
* units. A unit with no factor makes the expression unreadable. A caller that
|
|
173
|
+
* does not pass `geometry` has no feature shape to measure, so the expression
|
|
174
|
+
* is not read.
|
|
175
|
+
*
|
|
176
|
+
* `maximum` and `minimum` are read only where the caller says `aggregates`:
|
|
177
|
+
* the caller then supplies the records to scan (a default value), and every
|
|
178
|
+
* other caller would scan them for each feature drawn or field shown. Only
|
|
179
|
+
* their first argument is read, so `group_by` and `filter` make them
|
|
180
|
+
* unreadable, and so does a shape or another aggregate inside one: the records
|
|
181
|
+
* scanned carry no shape.
|
|
182
|
+
*
|
|
183
|
+
* `format_date` is read where its pattern and any language are text written
|
|
184
|
+
* into the expression, and the pattern holds no `t` (`qtDateFormat.ts`).
|
|
185
|
+
* `now()`, or `$now`, is read only as the date of `format_date` or of a
|
|
186
|
+
* function that takes a part of a date, such as `year`: anywhere else it would
|
|
187
|
+
* be compared or added as a date, which the evaluator does not do.
|
|
188
|
+
*/
|
|
189
|
+
export function readQgisExpression(expression, options = {}) {
|
|
190
|
+
const tokens = tokenizeExpression(expression);
|
|
191
|
+
if (!tokens || tokens.length === 0)
|
|
192
|
+
return null;
|
|
193
|
+
const parsed = readOr(tokens, 0);
|
|
194
|
+
if (!parsed || parsed.next !== tokens.length)
|
|
195
|
+
return null;
|
|
196
|
+
if (!options.currentValue && findCalls(parsed.node, 'current_value').length > 0)
|
|
197
|
+
return null;
|
|
198
|
+
const aggregateCalls = findCalls(parsed.node, ...AGGREGATES);
|
|
199
|
+
if (aggregateCalls.length > 0 && !options.aggregates)
|
|
200
|
+
return null;
|
|
201
|
+
if (aggregateCalls.some((call) => findCalls(call.args[0], ...AGGREGATES, ...Object.keys(GEOMETRY_VALUES)).length > 0))
|
|
202
|
+
return null;
|
|
203
|
+
if (!findCalls(parsed.node, ...REGEXP_FUNCTIONS).every((call) => isReadableRegex(call.args[1], true)))
|
|
204
|
+
return null;
|
|
205
|
+
if (!findRegexpPredicates(parsed.node).every((predicate) => isReadableRegex(predicate.args[1], false)))
|
|
206
|
+
return null;
|
|
207
|
+
const formatDateCalls = findCalls(parsed.node, 'format_date');
|
|
208
|
+
if (!formatDateCalls.every(isReadableFormatDate))
|
|
209
|
+
return null;
|
|
210
|
+
const datedNows = new Set(findCalls(parsed.node, ...DATE_TAKERS).map((call) => call.args[0]));
|
|
211
|
+
if (findCalls(parsed.node, 'now').some((call) => !datedNows.has(call)))
|
|
212
|
+
return null;
|
|
213
|
+
if (!areLookupsReadable(parsed.node, options.lookups === true))
|
|
214
|
+
return null;
|
|
215
|
+
const addedIntervals = new Set(findAddedIntervals(parsed.node));
|
|
216
|
+
if (findCalls(parsed.node, 'make_interval').some((call) => !addedIntervals.has(call)))
|
|
217
|
+
return null;
|
|
218
|
+
const geometryCalls = findCalls(parsed.node, ...Object.keys(GEOMETRY_VALUES));
|
|
219
|
+
if (geometryCalls.length === 0)
|
|
220
|
+
return parsed.node;
|
|
221
|
+
if (!options.geometry)
|
|
222
|
+
return null;
|
|
223
|
+
if (options.geometry === true)
|
|
224
|
+
return parsed.node;
|
|
225
|
+
for (const call of geometryCalls) {
|
|
226
|
+
const factor = options.geometry[GEOMETRY_VALUES[call.name]];
|
|
227
|
+
if (factor === null)
|
|
228
|
+
return null;
|
|
229
|
+
call.factor = factor;
|
|
230
|
+
}
|
|
231
|
+
return parsed.node;
|
|
232
|
+
}
|
|
233
|
+
/** A regular expression written into the expression as text, which `compileQtRegex` translates. */
|
|
234
|
+
function isReadableRegex(pattern, unicode) {
|
|
235
|
+
return pattern?.kind === 'text' && compileQtRegex(pattern.value, unicode) !== null;
|
|
236
|
+
}
|
|
237
|
+
/** The `~` predicates at or under a node. */
|
|
238
|
+
function findRegexpPredicates(node) {
|
|
239
|
+
const own = node.kind === 'predicate' && node.op === 'regexp' ? [node] : [];
|
|
240
|
+
return [...own, ...childNodes(node).flatMap(findRegexpPredicates)];
|
|
241
|
+
}
|
|
242
|
+
/** A `format_date` call whose pattern and language are written-in text, and whose pattern Groundwork formats. */
|
|
243
|
+
function isReadableFormatDate(call) {
|
|
244
|
+
const [, pattern, language] = call.args;
|
|
245
|
+
return pattern?.kind === 'text' && isFormattableQtDatePattern(pattern.value) && (language === undefined || language.kind === 'text');
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* A QGIS rule's condition as a GeoStyler filter: the condition's GeoStyler
|
|
249
|
+
* function inside a `custom` function named `QGIS_CONDITION`, which
|
|
250
|
+
* `geostyler-openlayers-parser` hands to the evaluator Groundwork gives it.
|
|
251
|
+
* Null for a condition `readQgisExpression` cannot read. `measure` is the
|
|
252
|
+
* project's units, which `$area` and `$length` need.
|
|
253
|
+
*/
|
|
254
|
+
export function qgisConditionToFilter(expression, measure) {
|
|
255
|
+
const node = readQgisExpression(expression, { currentValue: true, geometry: measure });
|
|
256
|
+
return node && { name: 'custom', fnName: QGIS_CONDITION, args: [expressionToGeoStylerFunction(node)] };
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* A string literal's text, as QGIS's `stripText` makes it: `''` becomes a
|
|
260
|
+
* quote first, then a backslash and the character after it become that
|
|
261
|
+
* character, `\n` and `\t` as a newline and a tab, and any character but
|
|
262
|
+
* those, `\` and `'` as `?`.
|
|
263
|
+
*/
|
|
264
|
+
export function unescapeStringLiteral(text) {
|
|
265
|
+
const escapes = { n: '\n', t: '\t', '\\': '\\', "'": "'" };
|
|
266
|
+
return text.replace(/''/g, "'").replace(/\\(.)/gs, (_, chr) => escapes[chr] ?? '?');
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
269
|
+
* Splits an expression into field references, string literals, numbers, names
|
|
270
|
+
* and punctuation, following QGIS's lexer (`src/core/qgsexpressionlexer.ll`,
|
|
271
|
+
* `qgis/QGIS@master`, read 2026-10-23). A `char(n)` call is read as the text of
|
|
272
|
+
* the character with that code, so `char(10)` is a line break, as `'\n'` is.
|
|
273
|
+
* Null where the expression holds anything else.
|
|
274
|
+
*/
|
|
275
|
+
function tokenizeExpression(expression) {
|
|
276
|
+
const pattern = /\s+|'((?:''|\\.|[^'\\])*)'|"((?:""|[^"])*)"|[Cc][Hh][Aa][Rr]\s*\(\s*(\d+)\s*\)|(\d*\.\d+(?:[eE][-+]?\d+)?|\d+(?:[eE][-+]?\d+)?)|([$@]?[A-Za-z_\x80-\uFFFF][A-Za-z0-9_\x80-\uFFFF]*)|(\|\||<>|!=|<=|>=|:=|\/\/|[()[\],=<>+*/%^~-])/y;
|
|
277
|
+
const tokens = [];
|
|
278
|
+
pattern.lastIndex = 0;
|
|
279
|
+
while (pattern.lastIndex < expression.length) {
|
|
280
|
+
const match = pattern.exec(expression);
|
|
281
|
+
if (!match)
|
|
282
|
+
return null;
|
|
283
|
+
const [, text, quotedField, charCode, number, name, punct] = match;
|
|
284
|
+
if (text !== undefined || charCode !== undefined) {
|
|
285
|
+
const code = Number(charCode);
|
|
286
|
+
if (charCode !== undefined && (code < 1 || code > 0x10FFFF))
|
|
287
|
+
return null;
|
|
288
|
+
tokens.push({ type: 'text', value: text !== undefined ? unescapeStringLiteral(text) : String.fromCodePoint(code) });
|
|
289
|
+
}
|
|
290
|
+
else if (quotedField !== undefined) {
|
|
291
|
+
const value = quotedField.replace(/""/g, '"');
|
|
292
|
+
if (!value)
|
|
293
|
+
return null;
|
|
294
|
+
tokens.push({ type: 'field', value });
|
|
295
|
+
}
|
|
296
|
+
else if (number !== undefined) {
|
|
297
|
+
tokens.push({ type: 'number', value: Number(number) });
|
|
298
|
+
}
|
|
299
|
+
else if (name !== undefined) {
|
|
300
|
+
tokens.push({ type: 'name', value: name });
|
|
301
|
+
}
|
|
302
|
+
else if (punct !== undefined) {
|
|
303
|
+
tokens.push({ type: 'punct', value: punct });
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
return tokens;
|
|
307
|
+
}
|
|
308
|
+
function isPunct(token, value) {
|
|
309
|
+
return token?.type === 'punct' && token.value === value;
|
|
310
|
+
}
|
|
311
|
+
function isWord(token, word) {
|
|
312
|
+
return token?.type === 'name' && token.value.toLowerCase() === word;
|
|
313
|
+
}
|
|
314
|
+
// The readers below follow QGIS's operator precedence, loosest first: `OR`,
|
|
315
|
+
// `AND`, `NOT`, `BETWEEN`, the comparisons, `~`, `IS`, `LIKE` and `IN`, then `+` and
|
|
316
|
+
// `-`, `*`, `/`, `//` and `%`, then `^` (right to left), `||`, and a unary minus
|
|
317
|
+
// tightest of all, so `1 + 2 || 3` is `1 + (2 || 3)` and `-2 ^ 2` is 4
|
|
318
|
+
// (`%left` and `%right` lines of `src/core/qgsexpressionparser.yy`,
|
|
319
|
+
// `qgis/QGIS@master`, read 2026-10-23, and `qgis/QGIS@16eddb2`, read 2026-10-26).
|
|
320
|
+
function readOr(tokens, index) {
|
|
321
|
+
return readLogic(tokens, index, 'or', readAnd);
|
|
322
|
+
}
|
|
323
|
+
function readAnd(tokens, index) {
|
|
324
|
+
return readLogic(tokens, index, 'and', readNot);
|
|
325
|
+
}
|
|
326
|
+
function readLogic(tokens, index, op, readOperand) {
|
|
327
|
+
const first = readOperand(tokens, index);
|
|
328
|
+
if (!first)
|
|
329
|
+
return null;
|
|
330
|
+
const args = [first.node];
|
|
331
|
+
let next = first.next;
|
|
332
|
+
while (isWord(tokens[next], op)) {
|
|
333
|
+
const operand = readOperand(tokens, next + 1);
|
|
334
|
+
if (!operand)
|
|
335
|
+
return null;
|
|
336
|
+
args.push(operand.node);
|
|
337
|
+
next = operand.next;
|
|
338
|
+
}
|
|
339
|
+
return { node: args.length === 1 ? args[0] : { kind: 'logic', op, args }, next };
|
|
340
|
+
}
|
|
341
|
+
function readNot(tokens, index) {
|
|
342
|
+
if (!isWord(tokens[index], 'not'))
|
|
343
|
+
return readPredicate(tokens, index);
|
|
344
|
+
const operand = readNot(tokens, index + 1);
|
|
345
|
+
return operand && { node: { kind: 'not', arg: operand.node }, next: operand.next };
|
|
346
|
+
}
|
|
347
|
+
/** A value, and any comparison, `IS`, `LIKE`, `ILIKE`, `IN` or `BETWEEN` after it, left to right. */
|
|
348
|
+
function readPredicate(tokens, index) {
|
|
349
|
+
let read = readSum(tokens, index);
|
|
350
|
+
while (read) {
|
|
351
|
+
const left = read.node;
|
|
352
|
+
let next = read.next;
|
|
353
|
+
const token = tokens[next];
|
|
354
|
+
if (token?.type === 'punct' && Object.hasOwn(COMPARISON_FUNCTIONS, token.value)) {
|
|
355
|
+
const right = readSum(tokens, next + 1);
|
|
356
|
+
read = right && { node: { kind: 'compare', op: token.value, left, right: right.node }, next: right.next };
|
|
357
|
+
continue;
|
|
358
|
+
}
|
|
359
|
+
if (isPunct(token, '~')) {
|
|
360
|
+
const right = readSum(tokens, next + 1);
|
|
361
|
+
read = right && { node: { kind: 'predicate', op: 'regexp', negate: false, args: [left, right.node] }, next: right.next };
|
|
362
|
+
continue;
|
|
363
|
+
}
|
|
364
|
+
if (isWord(token, 'is')) {
|
|
365
|
+
const negate = isWord(tokens[next + 1], 'not');
|
|
366
|
+
const right = readSum(tokens, next + (negate ? 2 : 1));
|
|
367
|
+
read = right && { node: { kind: 'predicate', op: 'is', negate, args: [left, right.node] }, next: right.next };
|
|
368
|
+
continue;
|
|
369
|
+
}
|
|
370
|
+
const negate = isWord(token, 'not');
|
|
371
|
+
if (negate)
|
|
372
|
+
next += 1;
|
|
373
|
+
const op = ['like', 'ilike', 'in', 'between'].find((word) => isWord(tokens[next], word));
|
|
374
|
+
if (!op)
|
|
375
|
+
return negate ? null : read;
|
|
376
|
+
if (op === 'like' || op === 'ilike') {
|
|
377
|
+
const pattern = readSum(tokens, next + 1);
|
|
378
|
+
read = pattern && { node: { kind: 'predicate', op, negate, args: [left, pattern.node] }, next: pattern.next };
|
|
379
|
+
}
|
|
380
|
+
else if (op === 'in') {
|
|
381
|
+
const list = readList(tokens, next + 1);
|
|
382
|
+
read = list && { node: { kind: 'predicate', op, negate, args: [left, ...list.nodes] }, next: list.next };
|
|
383
|
+
}
|
|
384
|
+
else {
|
|
385
|
+
const low = readSum(tokens, next + 1);
|
|
386
|
+
const high = low && isWord(tokens[low.next], 'and') ? readSum(tokens, low.next + 1) : null;
|
|
387
|
+
read = high && { node: { kind: 'predicate', op, negate, args: [left, low.node, high.node] }, next: high.next };
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
return null;
|
|
391
|
+
}
|
|
392
|
+
/** `(a, b, …)` from `index`: the expressions, and the index after the closing bracket. */
|
|
393
|
+
function readList(tokens, index) {
|
|
394
|
+
if (!isPunct(tokens[index], '('))
|
|
395
|
+
return null;
|
|
396
|
+
const nodes = [];
|
|
397
|
+
let next = index + 1;
|
|
398
|
+
while (!isPunct(tokens[next], ')')) {
|
|
399
|
+
if (next >= tokens.length)
|
|
400
|
+
return null;
|
|
401
|
+
if (nodes.length > 0) {
|
|
402
|
+
if (!isPunct(tokens[next], ','))
|
|
403
|
+
return null;
|
|
404
|
+
next += 1;
|
|
405
|
+
}
|
|
406
|
+
const item = readOr(tokens, next);
|
|
407
|
+
if (!item)
|
|
408
|
+
return null;
|
|
409
|
+
nodes.push(item.node);
|
|
410
|
+
next = item.next;
|
|
411
|
+
}
|
|
412
|
+
return { nodes, next: next + 1 };
|
|
413
|
+
}
|
|
414
|
+
/** `a + b - c …` from `index`, left to right, or a lone product, and the index after it. */
|
|
415
|
+
function readSum(tokens, index) {
|
|
416
|
+
return readArithmetic(tokens, index, ['+', '-'], readProduct);
|
|
417
|
+
}
|
|
418
|
+
/** `a * b / c // d % e …` from `index`, left to right, or a lone power, and the index after it. */
|
|
419
|
+
function readProduct(tokens, index) {
|
|
420
|
+
return readArithmetic(tokens, index, ['*', '/', '//', '%'], readPower);
|
|
421
|
+
}
|
|
422
|
+
/** `a ^ b ^ …` from `index`, right to left, so `2 ^ 3 ^ 2` is `2 ^ 9`, or a lone concatenation, and the index after it. */
|
|
423
|
+
function readPower(tokens, index) {
|
|
424
|
+
const base = readConcat(tokens, index);
|
|
425
|
+
if (!base || !isPunct(tokens[base.next], '^'))
|
|
426
|
+
return base;
|
|
427
|
+
const exponent = readPower(tokens, base.next + 1);
|
|
428
|
+
return exponent && { node: { kind: 'arithmetic', op: '^', left: base.node, right: exponent.node }, next: exponent.next };
|
|
429
|
+
}
|
|
430
|
+
function readArithmetic(tokens, index, ops, readOperand) {
|
|
431
|
+
let read = readOperand(tokens, index);
|
|
432
|
+
while (read) {
|
|
433
|
+
const token = tokens[read.next];
|
|
434
|
+
if (token?.type !== 'punct' || !ops.includes(token.value))
|
|
435
|
+
return read;
|
|
436
|
+
const right = readOperand(tokens, read.next + 1);
|
|
437
|
+
read = right && { node: { kind: 'arithmetic', op: token.value, left: read.node, right: right.node }, next: right.next };
|
|
438
|
+
}
|
|
439
|
+
return null;
|
|
440
|
+
}
|
|
441
|
+
/**
|
|
442
|
+
* `a || b || …` from `index`, or a lone unary term, and the index after it. It
|
|
443
|
+
* is a call named `||`, kept apart from `concat`: QGIS gives NULL when any part
|
|
444
|
+
* of `||` is NULL, where `concat` skips it (`QgsExpressionNodeBinaryOperator::evalNode`,
|
|
445
|
+
* `boConcat`, `src/core/expression/qgsexpressionnodeimpl.cpp`, `qgis/QGIS@master`,
|
|
446
|
+
* read 2026-10-25).
|
|
447
|
+
*/
|
|
448
|
+
function readConcat(tokens, index) {
|
|
449
|
+
const first = readUnary(tokens, index);
|
|
450
|
+
if (!first)
|
|
451
|
+
return null;
|
|
452
|
+
const terms = [first.node];
|
|
453
|
+
let next = first.next;
|
|
454
|
+
while (isPunct(tokens[next], '||')) {
|
|
455
|
+
const term = readUnary(tokens, next + 1);
|
|
456
|
+
if (!term)
|
|
457
|
+
return null;
|
|
458
|
+
terms.push(term.node);
|
|
459
|
+
next = term.next;
|
|
460
|
+
}
|
|
461
|
+
return { node: terms.length === 1 ? terms[0] : { kind: 'call', name: '||', args: terms }, next };
|
|
462
|
+
}
|
|
463
|
+
/** A term, or a minus or plus before one: a minus before a number is the negative number. */
|
|
464
|
+
function readUnary(tokens, index) {
|
|
465
|
+
const token = tokens[index];
|
|
466
|
+
if (!isPunct(token, '-') && !isPunct(token, '+'))
|
|
467
|
+
return readIndexed(tokens, index);
|
|
468
|
+
const operand = readUnary(tokens, index + 1);
|
|
469
|
+
if (!operand || isPunct(token, '+'))
|
|
470
|
+
return operand;
|
|
471
|
+
const { node } = operand;
|
|
472
|
+
return { node: node.kind === 'number' ? { kind: 'number', value: -node.value } : { kind: 'negate', arg: node }, next: operand.next };
|
|
473
|
+
}
|
|
474
|
+
/** A term and any `[index]` after it, read as `array_get`, as QGIS's index operator works on an array. */
|
|
475
|
+
function readIndexed(tokens, index) {
|
|
476
|
+
let read = readTerm(tokens, index);
|
|
477
|
+
while (read && isPunct(tokens[read.next], '[')) {
|
|
478
|
+
const position = readOr(tokens, read.next + 1);
|
|
479
|
+
if (!position || !isPunct(tokens[position.next], ']'))
|
|
480
|
+
return null;
|
|
481
|
+
read = { node: { kind: 'call', name: 'array_get', args: [read.node, position.node] }, next: position.next + 1 };
|
|
482
|
+
}
|
|
483
|
+
return read;
|
|
484
|
+
}
|
|
485
|
+
/**
|
|
486
|
+
* A field reference, quoted or bare, a string literal, a number,
|
|
487
|
+
* `TRUE`, `FALSE`, `NULL`, a bracketed expression, a `CASE`,
|
|
488
|
+
* or a call to one of the label functions, from `index`.
|
|
489
|
+
*/
|
|
490
|
+
function readTerm(tokens, index) {
|
|
491
|
+
const token = tokens[index];
|
|
492
|
+
const after = tokens[index + 1];
|
|
493
|
+
if (token?.type === 'field')
|
|
494
|
+
return { node: { kind: 'field', name: token.value }, next: index + 1 };
|
|
495
|
+
if (token?.type === 'text')
|
|
496
|
+
return { node: { kind: 'text', value: token.value }, next: index + 1 };
|
|
497
|
+
if (token?.type === 'number')
|
|
498
|
+
return { node: { kind: 'number', value: token.value }, next: index + 1 };
|
|
499
|
+
if (isPunct(token, '(')) {
|
|
500
|
+
const inner = readOr(tokens, index + 1);
|
|
501
|
+
return inner && isPunct(tokens[inner.next], ')') ? { node: inner.node, next: inner.next + 1 } : null;
|
|
502
|
+
}
|
|
503
|
+
if (token?.type !== 'name')
|
|
504
|
+
return null;
|
|
505
|
+
if (!isPunct(after, '(')) {
|
|
506
|
+
const word = token.value.toLowerCase();
|
|
507
|
+
if (word.startsWith('$')) {
|
|
508
|
+
if (word === '$now')
|
|
509
|
+
return { node: { kind: 'call', name: 'now', args: [] }, next: index + 1 };
|
|
510
|
+
return Object.hasOwn(GEOMETRY_VALUES, word) || word === '$geometry' ? { node: { kind: 'call', name: word, args: [] }, next: index + 1 } : null;
|
|
511
|
+
}
|
|
512
|
+
// A variable: `@parent` alone is read, inside an `aggregate` filter.
|
|
513
|
+
if (word.startsWith('@'))
|
|
514
|
+
return word === '@parent' ? { node: { kind: 'call', name: word, args: [] }, next: index + 1 } : null;
|
|
515
|
+
if (word === 'true' || word === 'false')
|
|
516
|
+
return { node: { kind: 'boolean', value: word === 'true' }, next: index + 1 };
|
|
517
|
+
if (word === 'null')
|
|
518
|
+
return { node: { kind: 'null' }, next: index + 1 };
|
|
519
|
+
if (word === 'case')
|
|
520
|
+
return readCase(tokens, index + 1);
|
|
521
|
+
if (QGIS_KEYWORDS.has(word))
|
|
522
|
+
return null;
|
|
523
|
+
return { node: { kind: 'field', name: token.value }, next: index + 1 };
|
|
524
|
+
}
|
|
525
|
+
const name = token.value.toLowerCase();
|
|
526
|
+
const argumentRange = argumentRangeOf(name);
|
|
527
|
+
if (!argumentRange)
|
|
528
|
+
return null;
|
|
529
|
+
const list = readArguments(tokens, index + 1, NAMED_PARAMETERS[name]);
|
|
530
|
+
if (!list || list.nodes.length < argumentRange[0] || list.nodes.length > argumentRange[1])
|
|
531
|
+
return null;
|
|
532
|
+
return { node: { kind: 'call', name, args: list.nodes }, next: list.next };
|
|
533
|
+
}
|
|
534
|
+
/**
|
|
535
|
+
* A call's arguments from its opening bracket, each by position or as
|
|
536
|
+
* `name:=value`, in the order of the function's `parameters`, with a left-out
|
|
537
|
+
* one given its `omitted` value. As in QGIS, a value by position cannot follow
|
|
538
|
+
* a named one, and a name must be the function's and given once
|
|
539
|
+
* (`exp_list` in `src/core/qgsexpressionparser.yy` and
|
|
540
|
+
* `QgsExpressionNodeFunction::validateParams` in `qgsexpressionnodeimpl.cpp`,
|
|
541
|
+
* `qgis/QGIS@master`, read 2026-10-27). Null for a named value where the
|
|
542
|
+
* function has no `parameters`.
|
|
543
|
+
*/
|
|
544
|
+
function readArguments(tokens, index, parameters) {
|
|
545
|
+
if (!isPunct(tokens[index], '('))
|
|
546
|
+
return null;
|
|
547
|
+
const positional = [];
|
|
548
|
+
const named = new Map();
|
|
549
|
+
let next = index + 1;
|
|
550
|
+
while (!isPunct(tokens[next], ')')) {
|
|
551
|
+
if (next >= tokens.length)
|
|
552
|
+
return null;
|
|
553
|
+
if (positional.length + named.size > 0) {
|
|
554
|
+
if (!isPunct(tokens[next], ','))
|
|
555
|
+
return null;
|
|
556
|
+
next += 1;
|
|
557
|
+
}
|
|
558
|
+
const nameToken = tokens[next];
|
|
559
|
+
const isNamed = nameToken?.type === 'name' && isPunct(tokens[next + 1], ':=');
|
|
560
|
+
const item = readOr(tokens, isNamed ? next + 2 : next);
|
|
561
|
+
if (!item)
|
|
562
|
+
return null;
|
|
563
|
+
if (isNamed) {
|
|
564
|
+
const position = parameters?.names.indexOf(nameToken.value.toLowerCase()) ?? -1;
|
|
565
|
+
if (position < 0 || position < positional.length || named.has(parameters.names[position]))
|
|
566
|
+
return null;
|
|
567
|
+
named.set(parameters.names[position], item.node);
|
|
568
|
+
}
|
|
569
|
+
else {
|
|
570
|
+
if (named.size > 0)
|
|
571
|
+
return null;
|
|
572
|
+
positional.push(item.node);
|
|
573
|
+
}
|
|
574
|
+
next = item.next;
|
|
575
|
+
}
|
|
576
|
+
if (named.size === 0)
|
|
577
|
+
return { nodes: positional, next: next + 1 };
|
|
578
|
+
const nodes = parameters.names.map((parameter, position) => positional[position] ?? named.get(parameter) ?? parameters.omitted);
|
|
579
|
+
return { nodes, next: next + 1 };
|
|
580
|
+
}
|
|
581
|
+
/** `WHEN … THEN …`, one or more, an optional `ELSE …`, and `END`, from just after `CASE`. */
|
|
582
|
+
function readCase(tokens, index) {
|
|
583
|
+
const whens = [];
|
|
584
|
+
let next = index;
|
|
585
|
+
while (isWord(tokens[next], 'when')) {
|
|
586
|
+
const when = readOr(tokens, next + 1);
|
|
587
|
+
if (!when || !isWord(tokens[when.next], 'then'))
|
|
588
|
+
return null;
|
|
589
|
+
const then = readOr(tokens, when.next + 1);
|
|
590
|
+
if (!then)
|
|
591
|
+
return null;
|
|
592
|
+
whens.push({ when: when.node, then: then.node });
|
|
593
|
+
next = then.next;
|
|
594
|
+
}
|
|
595
|
+
if (whens.length === 0)
|
|
596
|
+
return null;
|
|
597
|
+
let otherwise = null;
|
|
598
|
+
if (isWord(tokens[next], 'else')) {
|
|
599
|
+
const read = readOr(tokens, next + 1);
|
|
600
|
+
if (!read)
|
|
601
|
+
return null;
|
|
602
|
+
otherwise = read.node;
|
|
603
|
+
next = read.next;
|
|
604
|
+
}
|
|
605
|
+
return isWord(tokens[next], 'end') ? { node: { kind: 'case', whens, otherwise }, next: next + 1 } : null;
|
|
606
|
+
}
|
|
607
|
+
/** The fewest and most arguments a label function takes, or null for a function Groundwork does not read. */
|
|
608
|
+
function argumentRangeOf(name) {
|
|
609
|
+
if (name === 'concat')
|
|
610
|
+
return [1, Infinity];
|
|
611
|
+
if (Object.hasOwn(ONE_ARGUMENT_FUNCTIONS, name))
|
|
612
|
+
return [1, 1];
|
|
613
|
+
return Object.hasOwn(QGIS_ONLY_FUNCTIONS, name) ? QGIS_ONLY_FUNCTIONS[name] : null;
|
|
614
|
+
}
|
|
615
|
+
/** The nodes directly under a node. */
|
|
616
|
+
function childNodes(node) {
|
|
617
|
+
switch (node.kind) {
|
|
618
|
+
case 'call':
|
|
619
|
+
case 'predicate':
|
|
620
|
+
case 'logic':
|
|
621
|
+
return node.args;
|
|
622
|
+
case 'arithmetic':
|
|
623
|
+
case 'compare':
|
|
624
|
+
return [node.left, node.right];
|
|
625
|
+
case 'negate':
|
|
626
|
+
case 'not':
|
|
627
|
+
return [node.arg];
|
|
628
|
+
case 'case':
|
|
629
|
+
return [...node.whens.flatMap(({ when, then }) => [when, then]), ...(node.otherwise ? [node.otherwise] : [])];
|
|
630
|
+
default:
|
|
631
|
+
return [];
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
/** The `make_interval` calls that are the right side of a `+` or `-` at or under a node. */
|
|
635
|
+
function findAddedIntervals(node) {
|
|
636
|
+
const own = node.kind === 'arithmetic' && (node.op === '+' || node.op === '-') && node.right.kind === 'call' && node.right.name === 'make_interval'
|
|
637
|
+
? [node.right]
|
|
638
|
+
: [];
|
|
639
|
+
return [...own, ...childNodes(node).flatMap(findAddedIntervals)];
|
|
640
|
+
}
|
|
641
|
+
/**
|
|
642
|
+
* Whether an expression's reads of other layers are ones Groundwork answers.
|
|
643
|
+
* With `allowed` false, it holds none. Otherwise each `overlay_within` or
|
|
644
|
+
* `overlay_intersects` gives an expression and is the array that
|
|
645
|
+
* `array_first` or a whole-number `[index]` reads; each `aggregate` is a
|
|
646
|
+
* `count`, `min`, `max` or `concatenate` of the features its filter
|
|
647
|
+
* `intersects($geometry, geometry(@parent))` keeps, with no `order_by`; the
|
|
648
|
+
* layer is named in text; the target's expression and filter read only the
|
|
649
|
+
* target's fields; and `array_first`, `array_get` and the calls of that filter
|
|
650
|
+
* stand nowhere else.
|
|
651
|
+
*/
|
|
652
|
+
function areLookupsReadable(root, allowed) {
|
|
653
|
+
const lookups = findCalls(root, ...LOOKUPS);
|
|
654
|
+
if (lookups.length === 0) {
|
|
655
|
+
return findCalls(root, 'array_first', 'array_get', ...PARENT_GEOMETRY_CALLS).length === 0;
|
|
656
|
+
}
|
|
657
|
+
if (!allowed || !lookups.every(isReadableLookup))
|
|
658
|
+
return false;
|
|
659
|
+
const overlays = lookups.filter((call) => call.name !== 'aggregate');
|
|
660
|
+
const readers = findCalls(root, 'array_first', 'array_get');
|
|
661
|
+
if (readers.length !== overlays.length)
|
|
662
|
+
return false;
|
|
663
|
+
if (!readers.every((reader) => overlays.includes(reader.args[0])))
|
|
664
|
+
return false;
|
|
665
|
+
if (readers.some((reader) => reader.name === 'array_get' && !isWholeNumber(reader.args[1])))
|
|
666
|
+
return false;
|
|
667
|
+
const filterCalls = lookups.filter((call) => call.name === 'aggregate').length * PARENT_GEOMETRY_CALLS.length;
|
|
668
|
+
return findCalls(root, ...PARENT_GEOMETRY_CALLS).length === filterCalls;
|
|
669
|
+
}
|
|
670
|
+
function isWholeNumber(node) {
|
|
671
|
+
return node?.kind === 'number' && Number.isInteger(node.value);
|
|
672
|
+
}
|
|
673
|
+
/** The arguments a lookup was given, by name, a left-out one read as NULL. */
|
|
674
|
+
function lookupArguments(call) {
|
|
675
|
+
const names = NAMED_PARAMETERS[call.name].names;
|
|
676
|
+
return Object.fromEntries(names.map((name, position) => [name, call.args[position] ?? { kind: 'null' }]));
|
|
677
|
+
}
|
|
678
|
+
/** An expression over the target's own fields: no read of another layer, form or shape. */
|
|
679
|
+
function readsTargetFieldsOnly(node) {
|
|
680
|
+
return findCalls(node, ...LOOKUPS, ...PARENT_GEOMETRY_CALLS, 'array_first', 'array_get', 'current_value', ...AGGREGATES, ...Object.keys(GEOMETRY_VALUES)).length === 0;
|
|
681
|
+
}
|
|
682
|
+
function isParentIntersectsFilter(node) {
|
|
683
|
+
if (node.kind !== 'call' || node.name !== 'intersects')
|
|
684
|
+
return false;
|
|
685
|
+
const isRecordShape = (arg) => arg?.kind === 'call' && arg.name === '$geometry';
|
|
686
|
+
const isParentShape = (arg) => arg?.kind === 'call' && arg.name === 'geometry'
|
|
687
|
+
&& arg.args[0]?.kind === 'call' && arg.args[0].name === '@parent';
|
|
688
|
+
const [first, second] = node.args;
|
|
689
|
+
return (isRecordShape(first) && isParentShape(second)) || (isParentShape(first) && isRecordShape(second));
|
|
690
|
+
}
|
|
691
|
+
function isReadableLookup(call) {
|
|
692
|
+
const args = lookupArguments(call);
|
|
693
|
+
const layer = args.layer;
|
|
694
|
+
const expression = args.expression;
|
|
695
|
+
if (layer.kind !== 'text' || !layer.value.trim())
|
|
696
|
+
return false;
|
|
697
|
+
if (expression.kind === 'null' || !readsTargetFieldsOnly(expression))
|
|
698
|
+
return false;
|
|
699
|
+
if (call.name === 'aggregate') {
|
|
700
|
+
const aggregate = args.aggregate;
|
|
701
|
+
return aggregate.kind === 'text'
|
|
702
|
+
&& LOOKUP_AGGREGATES.includes(aggregate.value.toLowerCase())
|
|
703
|
+
&& isParentIntersectsFilter(args.filter)
|
|
704
|
+
&& (args.concatenator.kind === 'null' || args.concatenator.kind === 'text')
|
|
705
|
+
&& args.order_by.kind === 'null';
|
|
706
|
+
}
|
|
707
|
+
return (args.filter.kind === 'null' || readsTargetFieldsOnly(args.filter))
|
|
708
|
+
&& (args.limit.kind === 'null' || isWholeNumber(args.limit))
|
|
709
|
+
&& (args.cache.kind === 'null' || args.cache.kind === 'boolean');
|
|
710
|
+
}
|
|
711
|
+
/** The `LayerLookup` a readable lookup call makes. */
|
|
712
|
+
function toLayerLookup(call) {
|
|
713
|
+
const args = lookupArguments(call);
|
|
714
|
+
const isAggregate = call.name === 'aggregate';
|
|
715
|
+
const layer = args.layer;
|
|
716
|
+
const limit = args.limit;
|
|
717
|
+
const lookup = {
|
|
718
|
+
layer: layer.kind === 'text' ? layer.value.trim() : '',
|
|
719
|
+
predicate: call.name === 'overlay_within' ? 'within' : 'intersects',
|
|
720
|
+
expression: args.expression,
|
|
721
|
+
filter: isAggregate || args.filter.kind === 'null' ? null : args.filter,
|
|
722
|
+
limit: !isAggregate && limit?.kind === 'number' && limit.value >= 0 ? limit.value : null
|
|
723
|
+
};
|
|
724
|
+
return { key: JSON.stringify(lookup), ...lookup };
|
|
725
|
+
}
|
|
726
|
+
/**
|
|
727
|
+
* The reads of other layers in an expression `readQgisExpression` read with
|
|
728
|
+
* `lookups`, one per distinct read.
|
|
729
|
+
*/
|
|
730
|
+
export function readLayerLookups(node) {
|
|
731
|
+
const lookups = new Map();
|
|
732
|
+
for (const call of findCalls(node, ...LOOKUPS)) {
|
|
733
|
+
const lookup = toLayerLookup(call);
|
|
734
|
+
lookups.set(lookup.key, lookup);
|
|
735
|
+
}
|
|
736
|
+
return [...lookups.values()];
|
|
737
|
+
}
|
|
738
|
+
/** The calls to any of the named functions at or under a node. */
|
|
739
|
+
function findCalls(node, ...names) {
|
|
740
|
+
const own = node.kind === 'call' && names.includes(node.name) ? [node] : [];
|
|
741
|
+
return [...own, ...childNodes(node).flatMap((child) => findCalls(child, ...names))];
|
|
742
|
+
}
|
|
743
|
+
/**
|
|
744
|
+
* The form fields an expression reads through `current_value`, or null where
|
|
745
|
+
* QGIS could not tell. QGIS names them by evaluating each call's argument with
|
|
746
|
+
* no feature (`expressionFormAttributes`,
|
|
747
|
+
* `src/core/fieldformatter/qgsvaluerelationfieldformatter.cpp`,
|
|
748
|
+
* `qgis/QGIS@master`, read 2026-10-24), so an argument that reads a field
|
|
749
|
+
* gives a blank name and the filter is not used. Groundwork takes a text
|
|
750
|
+
* literal as the one constant argument. An empty list is an expression that
|
|
751
|
+
* reads no form value.
|
|
752
|
+
*/
|
|
753
|
+
export function readCurrentValueFields(node) {
|
|
754
|
+
const fields = [];
|
|
755
|
+
for (const call of findCalls(node, 'current_value')) {
|
|
756
|
+
const arg = call.args[0];
|
|
757
|
+
if (arg?.kind !== 'text' || !arg.value)
|
|
758
|
+
return null;
|
|
759
|
+
fields.push(arg.value);
|
|
760
|
+
}
|
|
761
|
+
return fields;
|
|
762
|
+
}
|
|
763
|
+
/**
|
|
764
|
+
* The `{{field}}` template of a label made only of fields, text and joins, or
|
|
765
|
+
* null where it holds anything else. A template draws a field with no value as
|
|
766
|
+
* blank, which is what `concat` does, so a `||` join is a template only when
|
|
767
|
+
* none of its parts can be NULL.
|
|
768
|
+
*/
|
|
769
|
+
export function expressionToTemplate(node) {
|
|
770
|
+
switch (node.kind) {
|
|
771
|
+
case 'field':
|
|
772
|
+
return `{{${node.name}}}`;
|
|
773
|
+
case 'text':
|
|
774
|
+
return node.value;
|
|
775
|
+
case 'number':
|
|
776
|
+
case 'boolean':
|
|
777
|
+
return String(node.value);
|
|
778
|
+
case 'null':
|
|
779
|
+
return '';
|
|
780
|
+
case 'call': {
|
|
781
|
+
if (node.name === '||' && !node.args.every((arg) => arg.kind === 'text' || arg.kind === 'number' || arg.kind === 'boolean'))
|
|
782
|
+
return null;
|
|
783
|
+
if (node.name !== 'concat' && node.name !== '||')
|
|
784
|
+
return null;
|
|
785
|
+
const parts = node.args.map(expressionToTemplate);
|
|
786
|
+
return parts.every((part) => part !== null) ? parts.join('') : null;
|
|
787
|
+
}
|
|
788
|
+
default:
|
|
789
|
+
return null;
|
|
790
|
+
}
|
|
791
|
+
}
|
|
792
|
+
/**
|
|
793
|
+
* The GeoStyler function for a label. A comparison, `AND`, `OR`, `NOT`, `IN`,
|
|
794
|
+
* `BETWEEN`, `CASE` and the four arithmetic operators take GeoStyler's own
|
|
795
|
+
* names (`add`, `sub`, `mul`, `div`); `IS`, `LIKE`, `ILIKE` and
|
|
796
|
+
* `QGIS_ONLY_FUNCTIONS` are `custom` functions with the QGIS name.
|
|
797
|
+
*/
|
|
798
|
+
export function expressionToGeoStylerFunction(node) {
|
|
799
|
+
const negated = (fn, negate) => (negate ? { name: 'not', args: [fn] } : fn);
|
|
800
|
+
switch (node.kind) {
|
|
801
|
+
case 'field':
|
|
802
|
+
return { name: 'property', args: [node.name] };
|
|
803
|
+
case 'null':
|
|
804
|
+
return null;
|
|
805
|
+
case 'text':
|
|
806
|
+
case 'number':
|
|
807
|
+
case 'boolean':
|
|
808
|
+
return node.value;
|
|
809
|
+
case 'compare':
|
|
810
|
+
return { name: COMPARISON_FUNCTIONS[node.op], args: [expressionToGeoStylerFunction(node.left), expressionToGeoStylerFunction(node.right)] };
|
|
811
|
+
case 'logic':
|
|
812
|
+
return { name: node.op === 'and' ? 'all' : 'any', args: node.args.map(expressionToGeoStylerFunction) };
|
|
813
|
+
case 'not':
|
|
814
|
+
return { name: 'not', args: [expressionToGeoStylerFunction(node.arg)] };
|
|
815
|
+
case 'arithmetic':
|
|
816
|
+
return { name: ARITHMETIC_FUNCTIONS[node.op], args: [expressionToGeoStylerFunction(node.left), expressionToGeoStylerFunction(node.right)] };
|
|
817
|
+
case 'negate':
|
|
818
|
+
return { name: 'custom', fnName: 'negate', args: [expressionToGeoStylerFunction(node.arg)] };
|
|
819
|
+
case 'predicate': {
|
|
820
|
+
const args = node.args.map(expressionToGeoStylerFunction);
|
|
821
|
+
const fn = node.op === 'in' || node.op === 'between' ? { name: node.op, args } : { name: 'custom', fnName: node.op, args };
|
|
822
|
+
return negated(fn, node.negate);
|
|
823
|
+
}
|
|
824
|
+
case 'case':
|
|
825
|
+
return {
|
|
826
|
+
name: 'case',
|
|
827
|
+
args: [
|
|
828
|
+
node.otherwise ? expressionToGeoStylerFunction(node.otherwise) : null,
|
|
829
|
+
...node.whens.map(({ when, then }) => ({ case: expressionToGeoStylerFunction(when), value: expressionToGeoStylerFunction(then) }))
|
|
830
|
+
]
|
|
831
|
+
};
|
|
832
|
+
case 'call': {
|
|
833
|
+
const args = node.args.map(expressionToGeoStylerFunction);
|
|
834
|
+
if (node.name === 'concat')
|
|
835
|
+
return { name: 'strConcat', args };
|
|
836
|
+
if (node.name === '||')
|
|
837
|
+
return { name: 'custom', fnName: '||', args };
|
|
838
|
+
if (Object.hasOwn(GEOMETRY_VALUES, node.name))
|
|
839
|
+
return { name: 'custom', fnName: node.name, args: [node.factor ?? null] };
|
|
840
|
+
if (node.name === 'overlay_within' || node.name === 'overlay_intersects')
|
|
841
|
+
return { name: 'custom', fnName: LAYER_LOOKUP, args: [toLayerLookup(node).key] };
|
|
842
|
+
if (node.name === 'aggregate') {
|
|
843
|
+
const { aggregate, concatenator } = lookupArguments(node);
|
|
844
|
+
return {
|
|
845
|
+
name: 'custom',
|
|
846
|
+
fnName: 'aggregate',
|
|
847
|
+
args: [
|
|
848
|
+
aggregate.kind === 'text' ? aggregate.value.toLowerCase() : null,
|
|
849
|
+
concatenator.kind === 'text' ? concatenator.value : '',
|
|
850
|
+
{ name: 'custom', fnName: LAYER_LOOKUP, args: [toLayerLookup(node).key] }
|
|
851
|
+
]
|
|
852
|
+
};
|
|
853
|
+
}
|
|
854
|
+
if (Object.hasOwn(QGIS_ONLY_FUNCTIONS, node.name))
|
|
855
|
+
return { name: 'custom', fnName: node.name, args };
|
|
856
|
+
return { name: ONE_ARGUMENT_FUNCTIONS[node.name], args };
|
|
857
|
+
}
|
|
858
|
+
}
|
|
859
|
+
}
|