Skip to content

Commit

Permalink
Make processes more GEE-native
Browse files Browse the repository at this point in the history
  • Loading branch information
m-mohr committed May 17, 2024
1 parent 60f7c75 commit b5380f2
Show file tree
Hide file tree
Showing 11 changed files with 145 additions and 39 deletions.
3 changes: 1 addition & 2 deletions src/processes/array_concat.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ export default class array_concat extends GeeProcess {
if (array2 instanceof ee.List) {
const keyStart = array1.size();
const keyEnd = array1.size().add(array2.length()).subtract(1);
const keys = ee.List.sequence(keyStart, keyEnd)
.map(x => ee.String(ee.Number(x).int()));
const keys = ee.List.sequence(keyStart, keyEnd).map(x => ee.String(ee.Number(x).int()));
array2 = ee.Dictionary.fromLists(keys, array2);
}
if (array2 instanceof ee.Dictionary) {
Expand Down
11 changes: 3 additions & 8 deletions src/processes/array_create.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,8 @@ export default class array_create extends GeeProcess {

executeSync(node) {
const ee = node.ee;
const data = node.getArgument("data", []);
const repeat = node.getArgument("repeat", 1);

let list = ee.List(data);
for (let i = 1; i < repeat; i++) {
list = list.cat(data);
}
return list;
const data = node.getArgumentAsListEE("data", []);
const repeat = node.getArgumentAsNumberEE("repeat", 1);
return ee.List.sequence(1, repeat).iterate((i, newList) => ee.List(newList).cat(data), ee.List([]));
}
}
6 changes: 3 additions & 3 deletions src/processes/array_create_labeled.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ export default class array_create_labeled extends GeeProcess {

executeSync(node) {
const ee = node.ee;
const values = node.getArgument("data");
const keys = node.getArgument("labels");
const eeKeys = ee.List(keys).map(x => GeeTypes.toString(x));
const values = node.getArgumentAsListEE("data");
const keys = node.getArgumentAsListEE("labels");
const eeKeys = keys.map(x => GeeTypes.toString(x));
return ee.Dictionary.fromLists(eeKeys, values);
}
}
9 changes: 7 additions & 2 deletions src/processes/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default class inspect extends GeeProcess {

executeSync(node) {
let data = node.getArgument('data');
const message = node.getArgument('message', '');
let message = node.getArgument('message', '');
const code = node.getArgument('code', 'User');
const level = node.getArgument('level', 'info');

Expand All @@ -14,10 +14,15 @@ export default class inspect extends GeeProcess {
data = data.toJSON();
}
else if (data instanceof ee.ComputedObject) {
node.warn('Inspecting GEE objects via getInfo() is slow. Do not use this in production.');
node.warn('Inspecting GEE objects (for `data`) via getInfo() is slow. Do not use this in production.');
data = data.getInfo();
}

if (message instanceof ee.ComputedObject) {
node.warn('Inspecting GEE objects (for `message`) via getInfo() is slow. Do not use this in production.');
message = String(ee.String(message).getInfo());
}

const logger = node.getLogger();
logger.add(message, level, data, node.getLoggerPath(), code)

Expand Down
2 changes: 2 additions & 0 deletions src/processes/load_collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ export default class load_collection extends GeeProcess {
dc = GeeFilters.filterBands(node, "bands", dc);
}

// Todo: Support property filter

return dc;
}

Expand Down
13 changes: 5 additions & 8 deletions src/processes/text_begins.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import GeeProcess from '../processgraph/process.js';
import text_position from './text_position.js';

export default class text_begins extends GeeProcess {

executeSync(node) {
let data = node.getArgumentAsStringEE('data');
let pattern = node.getArgumentAsStringEE('pattern');
const case_sensitive = node.getArgument('case_sensitive');
if (!case_sensitive) {
data = data.toLowerCase();
pattern = pattern.toLowerCase();
}
return data.index(pattern).eq(0);
const data = node.getArgumentAsStringEE('data');
const pattern = node.getArgumentAsStringEE('pattern');
const case_sensitive = node.getArgumentAsNumberEE('case_sensitive', true);
return text_position.process(data, pattern, case_sensitive).eq(0);
}

}
13 changes: 5 additions & 8 deletions src/processes/text_contains.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import GeeProcess from '../processgraph/process.js';
import text_position from './text_position.js';

export default class text_contains extends GeeProcess {

executeSync(node) {
let data = node.getArgumentAsStringEE('data');
let pattern = node.getArgumentAsStringEE('pattern');
const case_sensitive = node.getArgument('case_sensitive');
if (!case_sensitive) {
data = data.toLowerCase();
pattern = pattern.toLowerCase();
}
return data.index(pattern).gte(0);
const data = node.getArgumentAsStringEE('data');
const pattern = node.getArgumentAsStringEE('pattern');
const case_sensitive = node.getArgumentAsNumberEE('case_sensitive', true);
return text_position.process(data, pattern, case_sensitive).gte(0);
}

}
13 changes: 5 additions & 8 deletions src/processes/text_ends.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import GeeProcess from '../processgraph/process.js';
import text_position from './text_position.js';

export default class text_ends extends GeeProcess {

executeSync(node) {
let data = node.getArgumentAsStringEE('data');
let pattern = node.getArgumentAsStringEE('pattern');
const case_sensitive = node.getArgument('case_sensitive');
if (!case_sensitive) {
data = data.toLowerCase();
pattern = pattern.toLowerCase();
}
const data = node.getArgumentAsStringEE('data');
const pattern = node.getArgumentAsStringEE('pattern');
const case_sensitive = node.getArgumentAsNumberEE('case_sensitive', true);
const expectedPos = data.length().subtract(pattern.length());
return data.index(pattern).eq(expectedPos);
return text_position.process(data, pattern, case_sensitive).eq(expectedPos);
}

}
19 changes: 19 additions & 0 deletions src/processes/text_position.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import GeeProcess from '../processgraph/process.js';
import If from './if.js';

export default class text_position extends GeeProcess {

process(data, pattern, case_sensitive) {
data = If.process(case_sensitive.eq(0), data.toLowerCase(), data);
pattern = If.process(case_sensitive.eq(0), pattern.toLowerCase(), pattern);
return data.index(pattern);
}

executeSync(node) {
const data = node.getArgumentAsStringEE('data');
const pattern = node.getArgumentAsStringEE('pattern');
const case_sensitive = node.getArgumentAsNumberEE('case_sensitive', true);
return this.process(data, pattern, case_sensitive);
}

}
92 changes: 92 additions & 0 deletions src/processes/text_position.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
{
"id": "text_position",
"summary": "First position of a text in another text",
"description": "Checks where the text (also known as *string*) specified for `pattern` is positioned in the text specified for `data` for the first time. No-data values are passed through.",
"categories": [
"texts"
],
"parameters": [
{
"name": "data",
"description": "Text in which to find something in.",
"schema": {
"type": [
"string",
"null"
]
}
},
{
"name": "pattern",
"description": "Text to find in `data`. Regular expressions are not supported.",
"schema": {
"type": "string"
}
},
{
"name": "case_sensitive",
"description": "Case sensitive comparison can be disabled by setting this parameter to `false`.",
"schema": {
"type": "boolean"
},
"default": true,
"optional": true
}
],
"returns": {
"description": "A value >= 0 that indicates the position of the text, `-1` if the text was not found.",
"schema": {
"type": [
"integer",
"null"
],
"minimum": -1
}
},
"examples": [
{
"arguments": {
"data": "Lorem ipsum dolor sit amet",
"pattern": "openEO"
},
"returns": -1
},
{
"arguments": {
"data": "Lorem ipsum dolor sit amet",
"pattern": "ipsum dolor"
},
"returns": 6
},
{
"arguments": {
"data": "Lorem ipsum dolor sit amet",
"pattern": "Ipsum Dolor"
},
"returns": -1
},
{
"arguments": {
"data": "Lorem ipsum dolor sit amet",
"pattern": "SIT",
"case_sensitive": false
},
"returns": 18
},
{
"arguments": {
"data": "ÄÖÜ",
"pattern": "ö",
"case_sensitive": false
},
"returns": 1
},
{
"arguments": {
"data": null,
"pattern": "null"
},
"returns": null
}
]
}
3 changes: 3 additions & 0 deletions src/processes/utils/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const GeeFilters = {

filterTemporal(node, extentParam, dataParam = "data") {
const ee = node.ee;
// Todo: Support GEE type inputs
let [min, max] = node.getArgument(extentParam);
try {
const minDate = DateTime.fromISO(min, {zone: "utc"});
Expand Down Expand Up @@ -70,6 +71,7 @@ const GeeFilters = {

filterSpatial(node, extentParam, dataParam = "data") {
const ee = node.ee;
// Todo: Support GEE type inputs
const extent = node.getArgument(extentParam);
let dc;
if (dataParam instanceof DataCube) {
Expand Down Expand Up @@ -141,6 +143,7 @@ const GeeFilters = {
if (!Array.isArray(availableBands)) {
throw node.invalidArgument(bandsParam, `Data cube does not contain bands.`);
}
// Todo: Support GEE type inputs
const requestedBands = node.getArgument(bandsParam);
for(const band of requestedBands) {
if (!availableBands.includes(band)) {
Expand Down

0 comments on commit b5380f2

Please sign in to comment.