The VectorExpression operator performs a feature-wise expression function on a feature collection of a vector source. The expression is specified as a user-defined script in a very simple language. The output is a feature collection with the result of the expression and with time intervals that are the same as for the inputs. Users can either add a new column or replace the geometry column with the outputs of the expression. Internally, the expression is evaluated using floating-point numbers.
VectorExpression
An example usage scenario is to calculate a population density from an area and a population_size column. The expression uses a feature collection with two columns, referred to with their column names area and a population_size, and calculates the formula area / population_size. The output feature collection contains the result of the density expression in a new column.
area
population_size
area / population_size
Another example is to calculate the centroid of a polygon geometry. The expression uses a feature collection with a geometry column and calculates the formula centroid(geom). The output feature collection contains the result of the centroid expression replacing the original geometries.
centroid(geom)
expression
Expression
inputColumns
Vec<String>
[area, population_size]
outputColumn
OutputColumn
{"type": "column", "value": "density"} {"type": "geometry", "value": "MultiPoint"}
{"type": "column", "value": "density"}
{"type": "geometry", "value": "MultiPoint"}
geometryColumnName
String
geom
outputMeasurement
Measurement
{ "type": "continuous", "measurement": "Density"}
Note: If a name in inputColumns contains any characters other than letters, numbers, and underscores, a canonical variable name has so be used in the expression. For example, the column name population size has to be referred to as population_size in the expression.
population size
The following describes the types used in the parameters.
Expressions are simple scripts to perform feature-wise computations. One can refer to the columns with their name, e.g., area and a population_size. Furthermore, expressions can check with A IS NODATA, B IS NODATA, etc. for empty or NO DATA values. Finally, the value NODATA can be used to output empty or NO DATA.
A IS NODATA
B IS NODATA
NODATA
Users can think of this implicit function signature for, e.g., two inputs:
fn (A: f64, B: f64) -> f64
As a start, expressions contain algebraic operations and mathematical functions.
(A + B) / 2
In addition, branches can be used to check for conditions.
if A IS NODATA { B } else { A }
To generate more complex expressions, it is possible to have variable assignments.
let mean = (A + B) / 2; let coefficient = 0.357; mean * coefficient
Note, that all assignments are separated by semicolons. However, the last expression must be without a semicolon.
Function calls can be used to access utility functions.
max(A, 0)
Currently, the following functions are available:
abs(a)
min(a, b)
min(a, b, c)
max(a, b)
max(a, b, c)
sqrt(a)
ln(a)
log10(a)
cos(a)
sin(a)
tan(a)
acos(a)
asin(a)
atan(a)
pi()
e()
round(a)
ceil(a)
floor(a)
mod(a, b)
to_degrees(a)
to_radians(a)
Geometries can be referred to using the geometryColumnName, which is geom by default. There are several functions to work with geometries:
area(geom)
An example expression to calculate the centroid of a geometry is:
The VectorExpression operator expects one rater input with at most 8 bands.
vector
SingleVectorSource
The parsing of the expression can fail if there are, e.g., syntax errors.
{ "type": "VectorExpression", "params": { "inputColumns": ["area", "population_size"], "outputColumn": { "type": "column", "value": "density" }, "expression": "area / population_size", "outputMeasurement": { "type": "unitless" } }, "sources": { "vector": { "type": "OgrSource", "params": { "data": "areas" } } } }
{ "type": "VectorExpression", "params": { "inputColumns": [], "outputColumn": { "type": "geometry", "value": "MultiPoint" }, "expression": "centroid(geom)", "geometryColumnName": "geom" }, "sources": { "vector": { "type": "OgrSource", "params": { "data": "areas" } } } }