Skip to content

Commit ee4a77c

Browse files
committed
Formulas can reference more then just the first parameter by adding additional $ at the beginning, v2.0.1, Open-EO/openeo-web-editor#166
1 parent 85671b3 commit ee4a77c

File tree

10 files changed

+70
-36
lines changed

10 files changed

+70
-36
lines changed

CHANGELOG.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
## [2.0.1] - 2021-07-14
10+
11+
### Fixed
12+
13+
- `Formula`: Formulas can reference more then just the first parameter by adding additional `$` at the beginning, e.g. `$$0` to access the first element of an array in the second parameter.
14+
915
## [2.0.0] - 2021-07-06
1016

1117
### Added
@@ -105,7 +111,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
105111

106112
All prior releases have been documented in the [GitHub Releases](https://github.com/Open-EO/openeo-js-client/releases).
107113

108-
[Unreleased]: https://github.com/Open-EO/openeo-js-client/compare/v2.0.0...HEAD
114+
[Unreleased]: https://github.com/Open-EO/openeo-js-client/compare/v2.0.1...HEAD
115+
[2.0.1]: https://github.com/Open-EO/openeo-js-client/compare/v2.0.0...v2.0.1
109116
[2.0.0]: https://github.com/Open-EO/openeo-js-client/compare/v1.3.2...v2.0.0
110117
[1.3.2]: https://github.com/Open-EO/openeo-js-client/compare/v1.3.1...v1.3.2
111118
[1.3.1]: https://github.com/Open-EO/openeo-js-client/compare/v1.3.0...v1.3.1

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
JavaScript/TypeScript client for the openEO API.
44

5-
* [Documentation](https://open-eo.github.io/openeo-js-client/2.0.0/).
5+
* [Documentation](https://open-eo.github.io/openeo-js-client/2.0.1/).
66

7-
The version of this client is **2.0.0** and supports **openEO API versions 1.x.x**.
7+
The version of this client is **2.0.1** and supports **openEO API versions 1.x.x**.
88
Legacy versions are available as releases.
99
See the [CHANGELOG](CHANGELOG.md) for recent changes.
1010

@@ -53,7 +53,7 @@ In Node.js:
5353
In Typescript:
5454
* [Basic Discovery (promises)](examples/typescript/discovery.ts)
5555

56-
More information can be found in the [documentation](https://open-eo.github.io/openeo-js-client/2.0.0/).
56+
More information can be found in the [documentation](https://open-eo.github.io/openeo-js-client/2.0.1/).
5757

5858
## Development
5959

openeo.d.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1351,8 +1351,9 @@ declare module OpenEO {
13511351
* You can refer to output from processes with a leading `#`, e.g. `#loadco1` if the node to refer to has the key `loadco1`.
13521352
*
13531353
* Only available if a parent node is set via `setNode()`:
1354-
* Parameters can be accessed simply by name.
1355-
* If the first parameter is a (labeled) array, the value for a specific index or label can be accessed by typing the numeric index or textual label with a $ in front, for example $B1 for the label B1 or $0 for the first element in the array. Numeric labels are not supported.
1354+
* Parameters can be accessed simply by name.
1355+
* If the first parameter is a (labeled) array, the value for a specific index or label can be accessed by typing the numeric index or textual label with a `$` in front, for example `$B1` for the label `B1` or `$0` for the first element in the array. Numeric labels are not supported.
1356+
* You can access subsequent parameters by adding additional `$` at the beginning, e.g. `$$0` to access the first element of an array in the second parameter, `$$$0` for the same in the third parameter etc.
13561357
*
13571358
* An example that computes an EVI (assuming the labels for the bands are `NIR`, `RED` and `BLUE`): `2.5 * ($NIR - $RED) / (1 + $NIR + 6 * $RED + (-7.5 * $BLUE))`
13581359
*/

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@openeo/js-client",
3-
"version": "2.0.0",
3+
"version": "2.0.1",
44
"author": "openEO Consortium",
55
"contributors": [
66
{

src/builder/formula.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ const BuilderNode = require('./node');
1414
*
1515
* Only available if a parent node is set via `setNode()`:
1616
* Parameters can be accessed simply by name.
17-
* If the first parameter is a (labeled) array, the value for a specific index or label can be accessed by typing the numeric index or textual label with a $ in front, for example $B1 for the label B1 or $0 for the first element in the array. Numeric labels are not supported.
17+
* If the first parameter is a (labeled) array, the value for a specific index or label can be accessed by typing the numeric index or textual label with a `$` in front, for example `$B1` for the label `B1` or `$0` for the first element in the array. Numeric labels are not supported.
18+
* You can access subsequent parameters by adding additional `$` at the beginning, e.g. `$$0` to access the first element of an array in the second parameter, `$$$0` for the same in the third parameter etc.
1819
*
1920
* An example that computes an EVI (assuming the labels for the bands are `NIR`, `RED` and `BLUE`): `2.5 * ($NIR - $RED) / (1 + $NIR + 6 * $RED + (-7.5 * $BLUE))`
2021
*/
@@ -145,18 +146,20 @@ class Formula {
145146

146147
let callbackParams = this.builder.getParentCallbackParameters();
147148
// Array labels / indices
148-
if (typeof value === 'string' && value.startsWith('$') && callbackParams.length > 0) {
149-
let ref = value.substring(1);
150-
// Array access always refers to the first parameter passed
151-
return callbackParams[0][ref];
149+
if (typeof value === 'string' && callbackParams.length > 0) {
150+
let prefix = value.match(/^\$+/);
151+
let count = prefix ? prefix[0].length : 0;
152+
if (count > 0 && callbackParams.length >= count) {
153+
let ref = value.substring(count);
154+
return callbackParams[count-1][ref];
155+
}
152156
}
157+
153158
// Everything else is a parameter
154-
else {
155-
let parameter = new Parameter(value);
156-
// Add new parameter if it doesn't exist
157-
this.builder.addParameter(parameter);
158-
return parameter;
159-
}
159+
let parameter = new Parameter(value);
160+
// Add new parameter if it doesn't exist
161+
this.builder.addParameter(parameter);
162+
return parameter;
160163
}
161164

162165
/**

src/builder/tapdigit.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,18 +124,21 @@ TapDigit.Lexer = function () {
124124
}
125125

126126
function scanIdentifier() {
127-
let ch
128-
let id;
129-
130-
ch = peekNextChar();
131-
if (!isIdentifierStart(ch)) {
127+
let startCh = peekNextChar();
128+
if (!isIdentifierStart(startCh)) {
132129
return undefined;
133130
}
134131

135-
id = getNextChar();
132+
let id = getNextChar();
136133
while (true) {
137-
ch = peekNextChar();
138-
if (!isIdentifierPart(ch)) {
134+
let ch = peekNextChar();
135+
// If the first character is a $, it is allowed that more $ follow directly after
136+
if (startCh === '$') {
137+
if (ch !== '$') {
138+
startCh = ''; // Stop allowing $ once the first non-$ has been found
139+
} // else: allowed
140+
}
141+
else if (!isIdentifierPart(ch)) {
139142
break;
140143
}
141144
id += getNextChar();

src/openeo.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ class OpenEO {
104104
* @returns {string} Version number (according to SemVer).
105105
*/
106106
static clientVersion() {
107-
return "2.0.0";
107+
return "2.0.1";
108108
}
109109

110110
}

tests/builder.evi.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,11 @@ describe('Process Graph Builder (EVI)', () => {
6868
var expectedProcess;
6969
if (math) {
7070
expectedProcess = require('./data/builder.math.evi.example.json');
71-
eviAlgorithm = new Formula('2.5 * (($B08 - $B04) / (1 + $B08 + 6 * $B04 + -7.5 * $B02))');
71+
eviAlgorithm = new Formula('2.5 * (($B08 - $B04) / ($$offset + $B08 + 6 * $B04 + -7.5 * $B02))');
7272
}
7373
else {
7474
expectedProcess = expectedProcessEvi;
75-
eviAlgorithm = function(data) {
75+
eviAlgorithm = function(data, context) {
7676
var nir = data["B08"];
7777
var red = data["B04"];
7878
var blue = data["B02"];
@@ -82,7 +82,7 @@ describe('Process Graph Builder (EVI)', () => {
8282
this.divide(
8383
this.subtract(nir, red),
8484
this.sum([
85-
1,
85+
context["offset"],
8686
nir,
8787
this.multiply(6, red),
8888
this.multiply(-7.5, blue)
@@ -91,7 +91,7 @@ describe('Process Graph Builder (EVI)', () => {
9191
);
9292
};
9393
}
94-
var evi = builder.reduce_dimension(datacube, eviAlgorithm, "bands").description("Compute the EVI. Formula: 2.5 * (NIR - RED) / (1 + NIR + 6*RED + -7.5*BLUE)");
94+
var evi = builder.reduce_dimension(datacube, eviAlgorithm, "bands", {offset: 1}).description("Compute the EVI. Formula: 2.5 * (NIR - RED) / ($offset + NIR + 6*RED + -7.5*BLUE)");
9595

9696
var minTime = builder.reduce_dimension(
9797
evi,

tests/data/builder.evi.example.json

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
},
2727
"reduce1": {
2828
"process_id": "reduce_dimension",
29-
"description": "Compute the EVI. Formula: 2.5 * (NIR - RED) / (1 + NIR + 6*RED + -7.5*BLUE)",
29+
"description": "Compute the EVI. Formula: 2.5 * (NIR - RED) / ($offset + NIR + 6*RED + -7.5*BLUE)",
3030
"arguments": {
3131
"data": {"from_node": "loadco1"},
3232
"dimension": "bands",
@@ -53,6 +53,13 @@
5353
"label": "B02"
5454
}
5555
},
56+
"arraye4": {
57+
"process_id": "array_element",
58+
"arguments": {
59+
"data": {"from_parameter": "context"},
60+
"label": "offset"
61+
}
62+
},
5663
"subtra1": {
5764
"process_id": "subtract",
5865
"arguments": {
@@ -77,7 +84,7 @@
7784
"sum1": {
7885
"process_id": "sum",
7986
"arguments": {
80-
"data": [1, {"from_node": "arraye1"}, {"from_node": "multip1"}, {"from_node": "multip2"}]
87+
"data": [{"from_node": "arraye4"}, {"from_node": "arraye1"}, {"from_node": "multip1"}, {"from_node": "multip2"}]
8188
}
8289
},
8390
"divide1": {
@@ -96,6 +103,9 @@
96103
"result": true
97104
}
98105
}
106+
},
107+
"context": {
108+
"offset": 1
99109
}
100110
}
101111
},

tests/data/builder.math.evi.example.json

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
},
2727
"reduce1": {
2828
"process_id": "reduce_dimension",
29-
"description": "Compute the EVI. Formula: 2.5 * (NIR - RED) / (1 + NIR + 6*RED + -7.5*BLUE)",
29+
"description": "Compute the EVI. Formula: 2.5 * (NIR - RED) / ($offset + NIR + 6*RED + -7.5*BLUE)",
3030
"arguments": {
3131
"data": {"from_node": "loadco1"},
3232
"dimension": "bands",
@@ -47,6 +47,13 @@
4747
}
4848
},
4949
"arraye3": {
50+
"process_id": "array_element",
51+
"arguments": {
52+
"data": {"from_parameter": "context"},
53+
"label": "offset"
54+
}
55+
},
56+
"arraye4": {
5057
"process_id": "array_element",
5158
"arguments": {
5259
"data": {"from_parameter": "data"},
@@ -71,13 +78,13 @@
7178
"process_id": "multiply",
7279
"arguments": {
7380
"x": -7.5,
74-
"y": {"from_node": "arraye3"}
81+
"y": {"from_node": "arraye4"}
7582
}
7683
},
7784
"add1": {
7885
"process_id": "add",
7986
"arguments": {
80-
"x": 1,
87+
"x": {"from_node": "arraye3"},
8188
"y": {"from_node": "arraye1"}
8289
}
8390
},
@@ -111,6 +118,9 @@
111118
"result": true
112119
}
113120
}
121+
},
122+
"context": {
123+
"offset": 1
114124
}
115125
}
116126
},

0 commit comments

Comments
 (0)