@vectoriahq/quac 0.3.0-next.1 → 0.4.0-next.1

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.
@@ -1,6 +1,7 @@
1
1
  import { readHttpsAddress } from "../model/fieldIconAddress.js";
2
2
  import { declareQuacExtension } from "../model/quacExtension.js";
3
3
  import { STAC_VERSION } from "../model/Stac.js";
4
+ import { PROJ_CODE, PROJECTION_EXTENSION } from "../model/stacProjection.js";
4
5
  import { collectCatalogueCollections } from "../model/stacTreeWalk.js";
5
6
  import { readNonBlank } from "../util/nonBlankText.js";
6
7
  /**
@@ -193,6 +194,8 @@ function buildCollection(layer, defaultProviders) {
193
194
  const vectorGeometryTypes = geoJsonGeometryTypes(layer.wkbTypes ?? []);
194
195
  if (vectorGeometryTypes)
195
196
  stacExtensions.push(VECTOR_EXTENSION);
197
+ if (Object.values(layer.assets).some((asset) => typeof asset[PROJ_CODE] === 'string'))
198
+ stacExtensions.push(PROJECTION_EXTENSION);
196
199
  const summaries = {};
197
200
  if (layer.geometryType)
198
201
  summaries['quac:geometry_type'] = [layer.geometryType];
@@ -4,6 +4,7 @@ import { generateStacCatalogue } from "./stacCatalogue.js";
4
4
  import { wfsGetFeatureAsset } from "./wfsStacCatalogue.js";
5
5
  import { mapQtTypeToStacTableType } from "./wmsStacCatalogue.js";
6
6
  import { declareQuacExtension, QUAC_SCHEMA_URL } from "../model/quacExtension.js";
7
+ import { PROJECTION_EXTENSION } from "../model/stacProjection.js";
7
8
  import { chooseWmsCrs, WEB_MAP_LINKS_EXTENSION, WMS_REQUEST_PARAMETERS } from "../model/stacWms.js";
8
9
  /**
9
10
  * The catalogue a QGIS Server would serve for a project: the QUAC plugin's
@@ -200,7 +201,8 @@ function serveCollection(source, context) {
200
201
  delete collection.assets;
201
202
  collection['quac:editable'] = false;
202
203
  }
203
- const extensions = (source.stac_extensions ?? []).filter((url) => url !== QUAC_SCHEMA_URL && url !== WEB_MAP_LINKS_EXTENSION);
204
+ // The served Collection gives the data asset afresh, without `proj:code`.
205
+ const extensions = (source.stac_extensions ?? []).filter((url) => url !== QUAC_SCHEMA_URL && url !== WEB_MAP_LINKS_EXTENSION && url !== PROJECTION_EXTENSION);
204
206
  extensions.push(WEB_MAP_LINKS_EXTENSION);
205
207
  if (extensions.length > 0)
206
208
  collection.stac_extensions = extensions;
@@ -5,6 +5,7 @@ import { delimitedTextField, QUAC_DELIMITED_TEXT } from "../model/delimitedText.
5
5
  import { featureFilterField, QUAC_FEATURE_FILTER } from "../model/featureFilter.js";
6
6
  import { readDataAsset } from "../model/stacAssetRoles.js";
7
7
  import { decodeAssetPath, encodeAssetPath } from "../model/stacHref.js";
8
+ import { PROJ_CODE } from "../model/stacProjection.js";
8
9
  import { DEFAULT_STYLE_ROLE, styleAssetKey } from "../model/stacStyles.js";
9
10
  import { buildWmsLink, WEB_MAP_LINKS_EXTENSION } from "../model/stacWms.js";
10
11
  import { buildWmtsLink } from "../model/stacWmts.js";
@@ -498,6 +499,15 @@ function buildLayerAssets(layer, projectPath, geometryFilter) {
498
499
  const dataAssets = buildAssets(layer, geometryFilter);
499
500
  return { ...dataAssets, ...buildStyleAssets(layer, projectPath, dataAssets) };
500
501
  }
502
+ /**
503
+ * The layer's coordinate system as QGIS holds it (`<srs><spatialrefsys><authid>`),
504
+ * which `$x`, `$y` and the coordinates of `$geometry` are given in, as
505
+ * `proj:code`. Nothing where the project names none, or names one with no
506
+ * authority code, such as a user-defined system.
507
+ */
508
+ function layerCrsField(layer) {
509
+ return layer.crs && /^[A-Za-z]+:\S+$/.test(layer.crs) ? { [PROJ_CODE]: layer.crs } : {};
510
+ }
501
511
  function buildAssets(layer, geometryFilter) {
502
512
  const ds = layer.dataSource;
503
513
  if (ds.kind === 'wms' || ds.kind === 'wmts')
@@ -533,18 +543,20 @@ function buildAssets(layer, geometryFilter) {
533
543
  // file, which holds one layer.
534
544
  ...(ds.layerName && isLayerFileOfFormat(ds.filename, 'geopackage') ? { [QUAC_LAYER_NAME]: ds.layerName } : {}),
535
545
  ...(ds.delimitedText ? { [QUAC_DELIMITED_TEXT]: delimitedTextField(ds.delimitedText) } : {}),
536
- ...(ds.featureFilter ? { [QUAC_FEATURE_FILTER]: featureFilterField(ds.featureFilter) } : {})
546
+ ...(ds.featureFilter ? { [QUAC_FEATURE_FILTER]: featureFilterField(ds.featureFilter) } : {}),
547
+ ...layerCrsField(layer)
537
548
  }
538
549
  };
539
550
  }
540
551
  if (ds.kind === 'arcgis') {
541
- return { data: arcgisQueryAsset({ layerUrl: ds.url, where: ds.where }, layer.name) };
552
+ return { data: { ...arcgisQueryAsset({ layerUrl: ds.url, where: ds.where }, layer.name), ...layerCrsField(layer) } };
542
553
  }
543
554
  if (ds.kind === 'wfs') {
544
555
  return {
545
556
  data: {
546
557
  ...wfsGetFeatureAsset(ds.url, ds.typeName, ds.typeName),
547
- ...(ds.featureFilter ? { [QUAC_FEATURE_FILTER]: featureFilterField(ds.featureFilter) } : {})
558
+ ...(ds.featureFilter ? { [QUAC_FEATURE_FILTER]: featureFilterField(ds.featureFilter) } : {}),
559
+ ...layerCrsField(layer)
548
560
  }
549
561
  };
550
562
  }
@@ -41,10 +41,10 @@ function walkWmsNodes(nodes, context) {
41
41
  const out = [];
42
42
  for (const node of nodes) {
43
43
  if (node.kind === 'layer') {
44
- const wmsLink = context.wmsEndpoint && !context.wfsPublished.has(node.name)
45
- ? buildQgisServerWmsLink(context.wmsEndpoint, node)
44
+ const wmsLinks = context.wmsEndpoint && !context.wfsPublished.has(node.name)
45
+ ? buildQgisServerWmsLinks(context.wmsEndpoint, node)
46
46
  : null;
47
- out.push(wmsLink ? wmsOnlyLayerToNormalized(node, wmsLink) : wmsLayerToNormalized(node, context.resolveWfsSource(node)));
47
+ out.push(wmsLinks ? wmsOnlyLayerToNormalized(node, wmsLinks) : wmsLayerToNormalized(node, context.resolveWfsSource(node)));
48
48
  }
49
49
  else {
50
50
  const subId = slugify(node.title || node.name || 'group') || 'group';
@@ -84,27 +84,38 @@ function wmsLayerToNormalized(layer, wfsSource) {
84
84
  };
85
85
  }
86
86
  /**
87
- * A GetMap link for a layer QGIS Server publishes only over WMS. The style is
88
- * left empty, which asks the server for the layer's default style. No version
89
- * is stated, so an application reading the catalogue asks the server for one
87
+ * The GetMap links for a layer QGIS Server publishes only over WMS. The first
88
+ * leaves the style empty, which draws the layer's current style
89
+ * (`QgsRenderer::setLayerStyle`, `src/server/services/wms/qgswmsrenderer.cpp`,
90
+ * `qgis/QGIS@master`, read 2026-11-15). Where the layer has more than one
91
+ * named style, one link follows for each, titled with the style's title.
92
+ * GetProjectSettings lists the styles in name order and does not say which is
93
+ * current (`QgsMapLayerStyleManager::styles`, `src/core/qgsmaplayerstylemanager.cpp`,
94
+ * and `appendLayerStyles`, `src/server/services/wms/qgswmsgetcapabilities.cpp`,
95
+ * same revision), so the current one is also among them. No version is
96
+ * stated, so an application reading the catalogue asks the server for one
90
97
  * when the layer is first shown.
91
98
  */
92
- function buildQgisServerWmsLink(endpoint, layer) {
93
- return {
99
+ function buildQgisServerWmsLinks(endpoint, layer) {
100
+ const link = (style) => ({
94
101
  rel: 'wms',
95
102
  href: endpoint,
96
103
  type: 'image/png',
97
104
  'wms:layers': [layer.name],
98
- 'wms:styles': [''],
105
+ 'wms:styles': [style],
99
106
  'wms:transparent': true,
100
107
  'quac:wms_crs': chooseWmsCrs(layer.crsList)
101
- };
108
+ });
109
+ const named = layer.styles.length > 1
110
+ ? layer.styles.map((style) => ({ ...link(style.name), title: style.title || style.name }))
111
+ : [];
112
+ return [link(''), ...named];
102
113
  }
103
114
  /**
104
115
  * A layer QGIS Server publishes only over WMS: an image layer, so it has no data
105
116
  * asset, fields or editing, as a QGIS project's WMS layer has none.
106
117
  */
107
- function wmsOnlyLayerToNormalized(layer, wmsLink) {
118
+ function wmsOnlyLayerToNormalized(layer, wmsLinks) {
108
119
  return {
109
120
  kind: 'layer',
110
121
  id: layer.name,
@@ -123,7 +134,7 @@ function wmsOnlyLayerToNormalized(layer, wmsLink) {
123
134
  scaleRange: layer.scaleRange,
124
135
  opacity: layer.opacity,
125
136
  vendorFields: {},
126
- links: [wmsLink],
137
+ links: wmsLinks,
127
138
  extensions: [WEB_MAP_LINKS_EXTENSION]
128
139
  };
129
140
  }
package/dist/index.d.ts CHANGED
@@ -59,7 +59,7 @@ export type { StacWmtsSource } from './model/stacWmts.ts';
59
59
  export { VECTOR_TILE_MEDIA_TYPE, buildXyzLink, readCollectionXyzLink, readCollectionXyzTemplate } from './model/stacXyz.ts';
60
60
  export type { ActiveWfsLayer, WfsCapabilities, WfsDescribeFeatureTypeSchema, WfsFeatureTypeOperationsSupport, WfsFeatureTypeSummary, WfsFieldKind, WfsFieldSchema, WfsOperationName, WfsTransactionOperation, WfsTransactionResult, WfsValidationError, WfsWgs84BoundingBox } from './model/wfs.ts';
61
61
  export { parseProjectMeasurement } from './parse/parseMeasurement.ts';
62
- export { EXPRESSION_VARIABLE, GEOMETRY_VALUES, LAYER_LOOKUP, QGIS_CONDITION, expressionToGeoStylerFunction, expressionToTemplate, isQgisConditionFilter, qgisConditionToFilter, readCurrentValueFields, readLayerLookups, readQgisExpression } from './parse/qgisExpression.ts';
62
+ export { EXPRESSION_VARIABLE, GEOMETRY_VALUES, LAYER_CRS, LAYER_LOOKUP, QGIS_CONDITION, SHAPE_COORDINATE, expressionToGeoStylerFunction, expressionToTemplate, isQgisConditionFilter, qgisConditionToFilter, readCurrentValueFields, readLayerLookups, readQgisExpression } from './parse/qgisExpression.ts';
63
63
  export type { ExpressionNode, LayerLookup } from './parse/qgisExpression.ts';
64
64
  export { formatQtDateTime, qtDatePart, readQtDateTime, splitQtDatePattern } from './parse/qtDateFormat.ts';
65
65
  export { compileQtRegex, replaceQtRegex } from './parse/qtRegex.ts';
@@ -75,3 +75,4 @@ export { QUAC_SCHEMA_URL, declareQuacExtension, isNewerQuacMajor, listQuacExtens
75
75
  export { generateServerStacCatalogue } from './generate/serverStacCatalogue.ts';
76
76
  export { serverLayerTree } from './generate/serverLayerTree.ts';
77
77
  export type { ServerLayerTree, ServerLayerTreeGroup, ServerLayerTreeLayer, ServerLayerTreeNode } from './generate/serverLayerTree.ts';
78
+ export { PROJ_CODE, PROJECTION_EXTENSION, readCollectionCrs } from './model/stacProjection.ts';
package/dist/index.js CHANGED
@@ -38,7 +38,7 @@ export { WEB_MAP_LINKS_EXTENSION, buildWmsLink, readCollectionWms, wmsEndpoint }
38
38
  export { readCollectionWmts, wmtsCapabilitiesUrl } from "./model/stacWmts.js";
39
39
  export { VECTOR_TILE_MEDIA_TYPE, buildXyzLink, readCollectionXyzLink, readCollectionXyzTemplate } from "./model/stacXyz.js";
40
40
  export { parseProjectMeasurement } from "./parse/parseMeasurement.js";
41
- export { EXPRESSION_VARIABLE, GEOMETRY_VALUES, LAYER_LOOKUP, QGIS_CONDITION, expressionToGeoStylerFunction, expressionToTemplate, isQgisConditionFilter, qgisConditionToFilter, readCurrentValueFields, readLayerLookups, readQgisExpression } from "./parse/qgisExpression.js";
41
+ export { EXPRESSION_VARIABLE, GEOMETRY_VALUES, LAYER_CRS, LAYER_LOOKUP, QGIS_CONDITION, SHAPE_COORDINATE, expressionToGeoStylerFunction, expressionToTemplate, isQgisConditionFilter, qgisConditionToFilter, readCurrentValueFields, readLayerLookups, readQgisExpression } from "./parse/qgisExpression.js";
42
42
  export { formatQtDateTime, qtDatePart, readQtDateTime, splitQtDatePattern } from "./parse/qtDateFormat.js";
43
43
  export { compileQtRegex, replaceQtRegex } from "./parse/qtRegex.js";
44
44
  export { cloneCatalogShallow, collectAllCollections, resolveDocumentHrefs, withSelfLink } from "./sources/stacDocuments.js";
@@ -50,3 +50,4 @@ export { DEFAULT_WFS_VERSION, SPOKEN_WFS_VERSIONS, WFS_CAPABILITIES_VERSION, WFS
50
50
  export { QUAC_SCHEMA_URL, declareQuacExtension, isNewerQuacMajor, listQuacExtension, readQuacVersion } from "./model/quacExtension.js";
51
51
  export { generateServerStacCatalogue } from "./generate/serverStacCatalogue.js";
52
52
  export { serverLayerTree } from "./generate/serverLayerTree.js";
53
+ export { PROJ_CODE, PROJECTION_EXTENSION, readCollectionCrs } from "./model/stacProjection.js";
@@ -0,0 +1,11 @@
1
+ import type { StacCollection } from './Stac.ts';
2
+ /** STAC extension URL for the Projection extension, whose `proj:code` a layer's data asset carries. */
3
+ export declare const PROJECTION_EXTENSION = "https://stac-extensions.github.io/projection/v2.0.0/schema.json";
4
+ /** The Projection extension's key for the coordinate system an asset's data is in, such as `EPSG:28355`. */
5
+ export declare const PROJ_CODE = "proj:code";
6
+ /**
7
+ * The coordinate system a layer's data is in, from its data asset's
8
+ * `proj:code`: the one QGIS gives `$x`, `$y` and `$geometry` in. Null where the
9
+ * Collection names none.
10
+ */
11
+ export declare function readCollectionCrs(collection: StacCollection): string | null;
@@ -0,0 +1,14 @@
1
+ import { readDataAsset } from "./stacAssetRoles.js";
2
+ /** STAC extension URL for the Projection extension, whose `proj:code` a layer's data asset carries. */
3
+ export const PROJECTION_EXTENSION = 'https://stac-extensions.github.io/projection/v2.0.0/schema.json';
4
+ /** The Projection extension's key for the coordinate system an asset's data is in, such as `EPSG:28355`. */
5
+ export const PROJ_CODE = 'proj:code';
6
+ /**
7
+ * The coordinate system a layer's data is in, from its data asset's
8
+ * `proj:code`: the one QGIS gives `$x`, `$y` and `$geometry` in. Null where the
9
+ * Collection names none.
10
+ */
11
+ export function readCollectionCrs(collection) {
12
+ const code = readDataAsset(collection)?.[PROJ_CODE];
13
+ return typeof code === 'string' && code.trim() ? code.trim() : null;
14
+ }
@@ -515,7 +515,7 @@ function parseFieldEditRules(maplayer, note) {
515
515
  const defaultValue = parseDefaultExpression(expression, onUpdate);
516
516
  if (defaultValue)
517
517
  ruleFor(name).defaultValue = defaultValue;
518
- else if (readQgisExpression(expression, { geometry: true, aggregates: true, lookups: true, variables: true }))
518
+ else if (readQgisExpression(expression, { geometry: true, aggregates: true, lookups: true, variables: true, coordinates: true }))
519
519
  ruleFor(name).defaultExpression = { expression: expression.trim(), onUpdate };
520
520
  else
521
521
  note('default', name, expression.trim());
@@ -54,6 +54,18 @@ export type ExpressionNode = {
54
54
  }>;
55
55
  otherwise: ExpressionNode | null;
56
56
  };
57
+ /**
58
+ * The `fnName` of the GeoStyler `custom` function that stands for a coordinate
59
+ * of the record's shape. Its arguments are the coordinate, `x`, `y` or `z`; how
60
+ * it is taken, `feature` for `$x` and `$y` or `shape` for `x()`, `y()` and
61
+ * `z()` of `$geometry`; and the coordinate systems a `transform()` takes the
62
+ * shape from and to, each a code such as `EPSG:28355` or `LAYER_CRS`, or both
63
+ * null where there is no `transform()`. The evaluator works it out from the
64
+ * shape and the layer's coordinate system before it starts.
65
+ */
66
+ export declare const SHAPE_COORDINATE = "shape_coordinate";
67
+ /** Stands for the layer's own coordinate system in a `SHAPE_COORDINATE`, written `@layer_crs` or `layer_property(@layer, 'crs')`. */
68
+ export declare const LAYER_CRS = "@layer_crs";
57
69
  /**
58
70
  * The `fnName` of the GeoStyler `custom` function that stands for a variable,
59
71
  * such as `@cloud_useremail`. Its first argument is the variable's name with
@@ -153,6 +165,14 @@ export declare function isQgisConditionFilter(filter: unknown): filter is {
153
165
  * (`EXPRESSION_VARIABLE`) as a record is created or saved (a default value).
154
166
  * `x`, `y` and `z` are read only with a variable as their point, as in
155
167
  * `x(@position_coordinate)`. A read of another layer cannot use a variable.
168
+ *
169
+ * The coordinates of the record's shape are read only where the caller says
170
+ * `coordinates`: the caller then works each one out from the shape in the
171
+ * layer's coordinate system (`SHAPE_COORDINATE`), as a record is created or
172
+ * saved (a default value). They are `$x` and `$y`, and `x()`, `y()` and `z()`
173
+ * of `$geometry` or of `transform($geometry, from, to)`, where each
174
+ * coordinate system is a code written as text, `@layer_crs` or
175
+ * `layer_property(@layer, 'crs')`. A read of another layer cannot use one.
156
176
  */
157
177
  export declare function readQgisExpression(expression: string, options?: {
158
178
  currentValue?: boolean;
@@ -160,6 +180,7 @@ export declare function readQgisExpression(expression: string, options?: {
160
180
  aggregates?: boolean;
161
181
  lookups?: boolean;
162
182
  variables?: boolean;
183
+ coordinates?: boolean;
163
184
  }): ExpressionNode | null;
164
185
  /**
165
186
  * A QGIS rule's condition as a GeoStyler filter: the condition's GeoStyler
@@ -65,10 +65,29 @@ const QGIS_ONLY_FUNCTIONS = {
65
65
  intersects: [2, 2],
66
66
  x: [1, 1],
67
67
  y: [1, 1],
68
- z: [1, 1]
68
+ z: [1, 1],
69
+ transform: [3, 3],
70
+ layer_property: [2, 2]
69
71
  };
70
- /** The functions that take a coordinate of a point, read only with a variable as their point (`EXPRESSION_VARIABLE`). */
72
+ /**
73
+ * The functions that take a coordinate of a point: of the point a variable
74
+ * holds (`EXPRESSION_VARIABLE`), or of the record's shape (`SHAPE_COORDINATE`).
75
+ */
71
76
  const POINT_PARTS = ['x', 'y', 'z'];
77
+ /** The coordinates of the record's own point, written with no brackets. */
78
+ const FEATURE_COORDINATES = ['$x', '$y'];
79
+ /**
80
+ * The `fnName` of the GeoStyler `custom` function that stands for a coordinate
81
+ * of the record's shape. Its arguments are the coordinate, `x`, `y` or `z`; how
82
+ * it is taken, `feature` for `$x` and `$y` or `shape` for `x()`, `y()` and
83
+ * `z()` of `$geometry`; and the coordinate systems a `transform()` takes the
84
+ * shape from and to, each a code such as `EPSG:28355` or `LAYER_CRS`, or both
85
+ * null where there is no `transform()`. The evaluator works it out from the
86
+ * shape and the layer's coordinate system before it starts.
87
+ */
88
+ export const SHAPE_COORDINATE = 'shape_coordinate';
89
+ /** Stands for the layer's own coordinate system in a `SHAPE_COORDINATE`, written `@layer_crs` or `layer_property(@layer, 'crs')`. */
90
+ export const LAYER_CRS = '@layer_crs';
72
91
  /**
73
92
  * The `fnName` of the GeoStyler `custom` function that stands for a variable,
74
93
  * such as `@cloud_useremail`. Its first argument is the variable's name with
@@ -88,6 +107,87 @@ function isVariable(node) {
88
107
  function findVariables(node) {
89
108
  return [...(isVariable(node) ? [node] : []), ...childNodes(node).flatMap(findVariables)];
90
109
  }
110
+ /** Whether a node is a coordinate of the record's shape: `$x`, `$y`, or `x()`, `y()` or `z()` of anything but a variable. */
111
+ function isCoordinateCall(node) {
112
+ if (node.kind !== 'call')
113
+ return false;
114
+ return FEATURE_COORDINATES.includes(node.name) || (POINT_PARTS.includes(node.name) && !isVariable(node.args[0]));
115
+ }
116
+ /** The coordinates of the record's shape at or under a node, not looking inside one. */
117
+ function findCoordinateCalls(node) {
118
+ return isCoordinateCall(node) ? [node] : childNodes(node).flatMap(findCoordinateCalls);
119
+ }
120
+ function isRecordShape(node) {
121
+ return node?.kind === 'call' && node.name === '$geometry';
122
+ }
123
+ /** A coordinate system written as text, as `@layer_crs`, or as `layer_property(@layer, 'crs')`. */
124
+ function isReadableCrs(node) {
125
+ if (node?.kind === 'text')
126
+ return node.value.trim() !== '';
127
+ if (variableName(node) === 'layer_crs')
128
+ return true;
129
+ if (node?.kind !== 'call' || node.name !== 'layer_property')
130
+ return false;
131
+ const [layer, property] = node.args;
132
+ return variableName(layer) === 'layer' && property?.kind === 'text' && property.value.toLowerCase() === 'crs';
133
+ }
134
+ /** The code a readable coordinate system names, or `LAYER_CRS` for the layer's own. */
135
+ function crsOf(node) {
136
+ return node.kind === 'text' ? node.value.trim() : LAYER_CRS;
137
+ }
138
+ /** `$x`, `$y`, or `x()`, `y()` or `z()` of `$geometry` or of `transform($geometry, from, to)`. */
139
+ function isReadableCoordinate(call) {
140
+ if (FEATURE_COORDINATES.includes(call.name))
141
+ return true;
142
+ const shape = call.args[0];
143
+ if (isRecordShape(shape))
144
+ return true;
145
+ return shape?.kind === 'call' && shape.name === 'transform' && isRecordShape(shape.args[0]) && isReadableCrs(shape.args[1]) && isReadableCrs(shape.args[2]);
146
+ }
147
+ /** The `SHAPE_COORDINATE` a readable coordinate becomes. */
148
+ function coordinateFunction(call) {
149
+ if (FEATURE_COORDINATES.includes(call.name))
150
+ return { name: 'custom', fnName: SHAPE_COORDINATE, args: [call.name.slice(1), 'feature', null, null] };
151
+ const shape = call.args[0];
152
+ const transformed = shape.kind === 'call' && shape.name === 'transform';
153
+ return {
154
+ name: 'custom',
155
+ fnName: SHAPE_COORDINATE,
156
+ args: [call.name, 'shape', transformed ? crsOf(shape.args[1]) : null, transformed ? crsOf(shape.args[2]) : null]
157
+ };
158
+ }
159
+ /**
160
+ * A node with each coordinate of the record's shape replaced by NULL, for the
161
+ * checks that do not look inside one. Nodes holding none are kept as they are,
162
+ * so a `$area` found in the result is the one in the expression.
163
+ */
164
+ function withoutCoordinates(node) {
165
+ if (isCoordinateCall(node))
166
+ return { kind: 'null' };
167
+ const children = childNodes(node);
168
+ const replaced = children.map(withoutCoordinates);
169
+ if (replaced.every((child, index) => child === children[index]))
170
+ return node;
171
+ const next = (() => {
172
+ let index = 0;
173
+ return () => replaced[index++];
174
+ })();
175
+ switch (node.kind) {
176
+ case 'call': return { ...node, args: node.args.map(next) };
177
+ case 'predicate':
178
+ case 'logic': return { ...node, args: node.args.map(next) };
179
+ case 'arithmetic':
180
+ case 'compare': return { ...node, left: next(), right: next() };
181
+ case 'negate':
182
+ case 'not': return { ...node, arg: next() };
183
+ case 'case': return {
184
+ ...node,
185
+ whens: node.whens.map(() => ({ when: next(), then: next() })),
186
+ otherwise: node.otherwise ? next() : null
187
+ };
188
+ default: return node;
189
+ }
190
+ }
91
191
  /** The functions whose second argument is a regular expression, which QGIS compiles with Unicode properties. */
92
192
  const REGEXP_FUNCTIONS = ['regexp_match', 'regexp_replace', 'regexp_substr'];
93
193
  /**
@@ -215,13 +315,31 @@ export function isQgisConditionFilter(filter) {
215
315
  * (`EXPRESSION_VARIABLE`) as a record is created or saved (a default value).
216
316
  * `x`, `y` and `z` are read only with a variable as their point, as in
217
317
  * `x(@position_coordinate)`. A read of another layer cannot use a variable.
318
+ *
319
+ * The coordinates of the record's shape are read only where the caller says
320
+ * `coordinates`: the caller then works each one out from the shape in the
321
+ * layer's coordinate system (`SHAPE_COORDINATE`), as a record is created or
322
+ * saved (a default value). They are `$x` and `$y`, and `x()`, `y()` and `z()`
323
+ * of `$geometry` or of `transform($geometry, from, to)`, where each
324
+ * coordinate system is a code written as text, `@layer_crs` or
325
+ * `layer_property(@layer, 'crs')`. A read of another layer cannot use one.
218
326
  */
219
327
  export function readQgisExpression(expression, options = {}) {
220
328
  const tokens = tokenizeExpression(expression);
221
329
  if (!tokens || tokens.length === 0)
222
330
  return null;
223
- const parsed = readOr(tokens, 0);
224
- if (!parsed || parsed.next !== tokens.length)
331
+ const read = readOr(tokens, 0);
332
+ if (!read || read.next !== tokens.length)
333
+ return null;
334
+ const coordinates = findCoordinateCalls(read.node);
335
+ if (coordinates.length > 0 && (!options.coordinates || !coordinates.every(isReadableCoordinate)))
336
+ return null;
337
+ if (findCalls(read.node, ...LOOKUPS).some((call) => findCoordinateCalls(call).length > 0))
338
+ return null;
339
+ // The rest is checked with each coordinate set aside: its shape, its
340
+ // coordinate systems and `@layer` are read above.
341
+ const parsed = { node: withoutCoordinates(read.node) };
342
+ if (findCalls(parsed.node, ...FEATURE_COORDINATES, 'transform', 'layer_property').length > 0)
225
343
  return null;
226
344
  if (!options.currentValue && findCalls(parsed.node, 'current_value').length > 0)
227
345
  return null;
@@ -251,18 +369,18 @@ export function readQgisExpression(expression, options = {}) {
251
369
  return null;
252
370
  const geometryCalls = findCalls(parsed.node, ...Object.keys(GEOMETRY_VALUES));
253
371
  if (geometryCalls.length === 0)
254
- return parsed.node;
372
+ return read.node;
255
373
  if (!options.geometry)
256
374
  return null;
257
375
  if (options.geometry === true)
258
- return parsed.node;
376
+ return read.node;
259
377
  for (const call of geometryCalls) {
260
378
  const factor = options.geometry[GEOMETRY_VALUES[call.name]];
261
379
  if (factor === null)
262
380
  return null;
263
381
  call.factor = factor;
264
382
  }
265
- return parsed.node;
383
+ return read.node;
266
384
  }
267
385
  /** A regular expression written into the expression as text, which `compileQtRegex` translates. */
268
386
  function isReadableRegex(pattern, unicode) {
@@ -541,7 +659,9 @@ function readTerm(tokens, index) {
541
659
  if (word.startsWith('$')) {
542
660
  if (word === '$now')
543
661
  return { node: { kind: 'call', name: 'now', args: [] }, next: index + 1 };
544
- return Object.hasOwn(GEOMETRY_VALUES, word) || word === '$geometry' ? { node: { kind: 'call', name: word, args: [] }, next: index + 1 } : null;
662
+ return Object.hasOwn(GEOMETRY_VALUES, word) || word === '$geometry' || FEATURE_COORDINATES.includes(word)
663
+ ? { node: { kind: 'call', name: word, args: [] }, next: index + 1 }
664
+ : null;
545
665
  }
546
666
  // `@parent` is read inside an `aggregate` filter, and any other variable
547
667
  // where the caller says `variables`. A variable keeps its spelling, since
@@ -869,6 +989,8 @@ export function expressionToGeoStylerFunction(node) {
869
989
  ]
870
990
  };
871
991
  case 'call': {
992
+ if (isCoordinateCall(node))
993
+ return coordinateFunction(node);
872
994
  const variable = variableName(node);
873
995
  if (variable !== null)
874
996
  return { name: 'custom', fnName: EXPRESSION_VARIABLE, args: [variable] };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vectoriahq/quac",
3
- "version": "0.3.0-next.1",
3
+ "version": "0.4.0-next.1",
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.",