@vectoriahq/quac 0.5.0 → 0.6.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.
@@ -78,18 +78,28 @@ export declare const EXPRESSION_VARIABLE = "expression_variable";
78
78
  export declare const LOOKUP_AGGREGATES: readonly ["count", "min", "max", "concatenate"];
79
79
  /**
80
80
  * One read of another layer of the project: the features of `layer`, named by
81
- * its name or its QGIS id, that the record's shape lies within or meets, kept
82
- * by `filter` and cut to `limit`, each giving the value of `expression`. The
83
- * answer is the list of those values, in the order the source gave the
84
- * features. `key` tells one read from another.
81
+ * its name or its QGIS id, that the record's shape lies within or meets, or
82
+ * that are nearest to it (`predicate`), kept by `filter` and cut to `limit`,
83
+ * each giving the value of `expression`. The answer is the list of those
84
+ * values, in the order the caller works out: nearest first for `nearest`;
85
+ * otherwise by `sortByOverlap` where it is given, and else as the caller
86
+ * chooses. `key` tells one read from another.
87
+ *
88
+ * `sortByOverlap` is QGIS's `sort_by_intersection_size` of `overlay_intersects`:
89
+ * the features ordered by the size of the intersection, the largest first for
90
+ * `descending`. `maxDistance` is `overlay_nearest`'s `max_distance`. Each is
91
+ * left out where the expression does not give it. A `limit` of null keeps
92
+ * every feature; `overlay_nearest` keeps one, as QGIS does, where none is given.
85
93
  */
86
94
  export interface LayerLookup {
87
95
  key: string;
88
96
  layer: string;
89
- predicate: 'within' | 'intersects';
97
+ predicate: 'within' | 'intersects' | 'nearest';
90
98
  expression: ExpressionNode;
91
99
  filter: ExpressionNode | null;
92
100
  limit: number | null;
101
+ sortByOverlap?: 'descending' | 'ascending';
102
+ maxDistance?: number;
93
103
  }
94
104
  /**
95
105
  * The `fnName` of the GeoStyler `custom` function that stands for a
@@ -130,7 +140,7 @@ export declare function isQgisConditionFilter(filter: unknown): filter is {
130
140
  * evaluator adds it to a date or takes it away. QGIS reads an interval
131
141
  * elsewhere too, which the evaluator does not.
132
142
  *
133
- * `overlay_within`, `overlay_intersects` and `aggregate` read another layer,
143
+ * `overlay_within`, `overlay_intersects`, `overlay_nearest` and `aggregate` read another layer,
134
144
  * and are read only where the caller says `lookups`: the caller then answers
135
145
  * each `LayerLookup` before it evaluates (a default value). See
136
146
  * `areLookupsReadable` for the forms read.
@@ -59,7 +59,8 @@ const QGIS_ONLY_FUNCTIONS = {
59
59
  array_first: [1, 1],
60
60
  array_get: [2, 2],
61
61
  overlay_within: [1, 5],
62
- overlay_intersects: [1, 5],
62
+ overlay_intersects: [1, 9],
63
+ overlay_nearest: [1, 6],
63
64
  aggregate: [3, 6],
64
65
  geometry: [1, 1],
65
66
  intersects: [2, 2],
@@ -199,14 +200,19 @@ const REGEXP_FUNCTIONS = ['regexp_match', 'regexp_replace', 'regexp_substr'];
199
200
  */
200
201
  const NAMED_PARAMETERS = {
201
202
  make_interval: { names: ['years', 'months', 'weeks', 'days', 'hours', 'minutes', 'seconds'], omitted: { kind: 'number', value: 0 } },
202
- // QGIS's own lists go on (`min_overlap` and others); a value for any of
203
- // those leaves the expression unread.
203
+ // QGIS's own lists go on past the ones that are read (`min_overlap`,
204
+ // `min_inscribed_circle_radius` and `return_details` of `overlay_intersects`);
205
+ // a value for any of those leaves the expression unread.
204
206
  overlay_within: { names: ['layer', 'expression', 'filter', 'limit', 'cache'], omitted: { kind: 'null' } },
205
- overlay_intersects: { names: ['layer', 'expression', 'filter', 'limit', 'cache'], omitted: { kind: 'null' } },
207
+ overlay_intersects: {
208
+ names: ['layer', 'expression', 'filter', 'limit', 'cache', 'min_overlap', 'min_inscribed_circle_radius', 'return_details', 'sort_by_intersection_size'],
209
+ omitted: { kind: 'null' }
210
+ },
211
+ overlay_nearest: { names: ['layer', 'expression', 'filter', 'limit', 'max_distance', 'cache'], omitted: { kind: 'null' } },
206
212
  aggregate: { names: ['layer', 'aggregate', 'expression', 'filter', 'concatenator', 'order_by'], omitted: { kind: 'null' } }
207
213
  };
208
214
  /** The calls that read another layer of the project: `LayerLookup`. */
209
- const LOOKUPS = ['overlay_within', 'overlay_intersects', 'aggregate'];
215
+ const LOOKUPS = ['overlay_within', 'overlay_intersects', 'overlay_nearest', 'aggregate'];
210
216
  /**
211
217
  * What `aggregate` reads the filter `intersects($geometry, geometry(@parent))`
212
218
  * through, which stand nowhere else.
@@ -280,7 +286,7 @@ export function isQgisConditionFilter(filter) {
280
286
  * evaluator adds it to a date or takes it away. QGIS reads an interval
281
287
  * elsewhere too, which the evaluator does not.
282
288
  *
283
- * `overlay_within`, `overlay_intersects` and `aggregate` read another layer,
289
+ * `overlay_within`, `overlay_intersects`, `overlay_nearest` and `aggregate` read another layer,
284
290
  * and are read only where the caller says `lookups`: the caller then answers
285
291
  * each `LayerLookup` before it evaluates (a default value). See
286
292
  * `areLookupsReadable` for the forms read.
@@ -798,8 +804,8 @@ function findAddedIntervals(node) {
798
804
  }
799
805
  /**
800
806
  * Whether an expression's reads of other layers are ones the caller can answer.
801
- * With `allowed` false, it holds none. Otherwise each `overlay_within` or
802
- * `overlay_intersects` gives an expression and is the array that
807
+ * With `allowed` false, it holds none. Otherwise each `overlay_within`,
808
+ * `overlay_intersects` or `overlay_nearest` gives an expression and is the array that
803
809
  * `array_first` or a whole-number `[index]` reads; each `aggregate` is a
804
810
  * `count`, `min`, `max` or `concatenate` of the features its filter
805
811
  * `intersects($geometry, geometry(@parent))` keeps, with no `order_by`; the
@@ -863,23 +869,45 @@ function isReadableLookup(call) {
863
869
  && (args.concatenator.kind === 'null' || args.concatenator.kind === 'text')
864
870
  && args.order_by.kind === 'null';
865
871
  }
866
- return (args.filter.kind === 'null' || readsTargetFieldsOnly(args.filter))
872
+ const isReadableOverlay = (args.filter.kind === 'null' || readsTargetFieldsOnly(args.filter))
867
873
  && (args.limit.kind === 'null' || isWholeNumber(args.limit))
868
874
  && (args.cache.kind === 'null' || args.cache.kind === 'boolean');
875
+ if (call.name === 'overlay_nearest') {
876
+ return isReadableOverlay && (args.max_distance.kind === 'null' || (args.max_distance.kind === 'number' && args.max_distance.value >= 0));
877
+ }
878
+ if (call.name === 'overlay_intersects') {
879
+ const sort = args.sort_by_intersection_size;
880
+ return isReadableOverlay
881
+ && args.min_overlap.kind === 'null'
882
+ && args.min_inscribed_circle_radius.kind === 'null'
883
+ && args.return_details.kind === 'null'
884
+ && (sort.kind === 'null' || (sort.kind === 'text' && OVERLAP_SORTS.has(sort.value.trim().toLowerCase())));
885
+ }
886
+ return isReadableOverlay;
869
887
  }
888
+ /** The values of `sort_by_intersection_size` that QGIS takes: `des` for the largest overlap first, `asc` for the smallest. */
889
+ const OVERLAP_SORTS = new Map([['des', 'descending'], ['asc', 'ascending']]);
870
890
  /** The `LayerLookup` a readable lookup call makes. */
871
891
  function toLayerLookup(call) {
872
892
  const args = lookupArguments(call);
873
893
  const isAggregate = call.name === 'aggregate';
874
894
  const layer = args.layer;
875
895
  const limit = args.limit;
896
+ const isNearest = call.name === 'overlay_nearest';
897
+ const sort = args.sort_by_intersection_size;
898
+ const maxDistance = args.max_distance;
876
899
  const lookup = {
877
900
  layer: layer.kind === 'text' ? layer.value.trim() : '',
878
- predicate: call.name === 'overlay_within' ? 'within' : 'intersects',
901
+ predicate: call.name === 'overlay_within' ? 'within' : isNearest ? 'nearest' : 'intersects',
879
902
  expression: args.expression,
880
903
  filter: isAggregate || args.filter.kind === 'null' ? null : args.filter,
881
- limit: !isAggregate && limit?.kind === 'number' && limit.value >= 0 ? limit.value : null
904
+ // `overlay_nearest` gives the one nearest feature unless it is given a limit, and every one for a limit of -1.
905
+ limit: isAggregate ? null : limit?.kind === 'number' ? (limit.value >= 0 ? limit.value : null) : isNearest ? 1 : null
882
906
  };
907
+ if (sort?.kind === 'text')
908
+ lookup.sortByOverlap = OVERLAP_SORTS.get(sort.value.trim().toLowerCase());
909
+ if (maxDistance?.kind === 'number')
910
+ lookup.maxDistance = maxDistance.value;
883
911
  return { key: JSON.stringify(lookup), ...lookup };
884
912
  }
885
913
  /**
@@ -1004,7 +1032,7 @@ export function expressionToGeoStylerFunction(node) {
1004
1032
  return { name: 'custom', fnName: '||', args };
1005
1033
  if (Object.hasOwn(GEOMETRY_VALUES, node.name))
1006
1034
  return { name: 'custom', fnName: node.name, args: [node.factor ?? null] };
1007
- if (node.name === 'overlay_within' || node.name === 'overlay_intersects')
1035
+ if (node.name === 'overlay_within' || node.name === 'overlay_intersects' || node.name === 'overlay_nearest')
1008
1036
  return { name: 'custom', fnName: LAYER_LOOKUP, args: [toLayerLookup(node).key] };
1009
1037
  if (node.name === 'aggregate') {
1010
1038
  const { aggregate, concatenator } = lookupArguments(node);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vectoriahq/quac",
3
- "version": "0.5.0",
3
+ "version": "0.6.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.",