forked from fitoprincipe/geetools-code-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist_algorithms
86 lines (72 loc) · 2.59 KB
/
list_algorithms
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/***
* List algorithms for enhacing List objects
*
* Author: Rodrigo E. Principe
* email: [email protected]
* License: MIT
*/
var help = {};
var remove_duplicates = function(list) {
var newlist = ee.List([]);
list = ee.List(list)
var wrap = function(element, init) {
init = ee.List(init);
var contained = init.contains(element);
return ee.Algorithms.If(contained, init, init.add(element));
};
return ee.List(list.iterate(wrap, newlist));
};
var get_from_dict = function(list, dict) {
var empty = ee.List([])
list = ee.List(list)
dict = ee.Dictionary(dict)
var values = ee.List(list.iterate(function(el, first) {
var f = ee.List(first)
var cond = dict.contains(el);
return ee.Algorithms.If(cond, f.add(dict.get(el)), f)
}, empty))
return values
}
var intersect = function(list1, list2) {
var newlist = ee.List([])
var wrap = function(element, first) {
first = ee.List(first)
return ee.Algorithms.If(list2.contains(element), first.add(element), first)
}
return ee.List(list1.iterate(wrap, newlist))
}
// FILTERS
var regex = function(list, regex, flags) {
list = ee.List(list)
var fl = flags || null
var i = ee.List([])
var f = list.iterate(function(el, first){
var f = ee.List(first)
var e = ee.String(el);
var matched = ee.List(e.match(regex, fl))
return ee.Algorithms.If(matched.size().gt(0), f.add(el), f)
}, i)
return ee.List(f)
}
var filters = {regex: regex}
help['remove_duplicates'] = 'remove_duplicates(list)\n'+
'Remove duplicated values from a list object'
help['get_from_dict'] = "get_from_dict(list, dict)\n"+
"Get a list of Dict's values from a list object. Keys must be unique"+
"list: list of keys\n"+
"dict: dict to get the values for list's keys\n"+
'return: ee.List'
help['itersection'] = "intersection(list1, list2)\n"+
"Return the intersection between two lists (common elements)"
help['filters'] = {'regex': 'filters.regex(list, regex, flags)\n'+
'Filter a list using a Regular Expression\n'+
'list: the list to filter\n'+
'regex: regular expression\n'+
'flags: flags for the RegEx'
}
exports.help = help
exports.remove_duplicates = remove_duplicates
exports.get_from_dict = get_from_dict
exports.filters = filters
exports.intersect = intersect
exports.options = ee.Dictionary(help).keys()