forked from fitoprincipe/geetools-code-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmutables
57 lines (50 loc) · 1.69 KB
/
mutables
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
/****************
* Mutable Objects
* (despite https://groups.google.com/d/msg/google-earth-engine-developers/ZWxQsCiJhQY/x4b-7vKYAwAJ)
*
* Author: Rodrigo E. Principe
* email: [email protected]
* licence: MIT
****************/
var tools = require('users/fitoprincipe/geetools:tools')
var Feature = function(feature) {
this.feature = feature
this.set = function(key, value) {
this.feature = this.feature.set(key, value)
}
this.get = function(property) {
return this.feature.get(property)
}
this.id = this.feature.id()
}
exports.Feature = Feature
var FeatureCollection = function(collection) {
this.collection = collection
this.setPosition = function(position, property, value) {
var featlist = this.collection.toList(this.collection.size())
var feat = ee.Feature(featlist.get(position))
this.set(feat, property, value)
}
this.getPosition = function(position, property) {
var featlist = this.collection.toList(this.collection.size())
var feat = ee.Feature(featlist.get(position))
return this.get(feat, property)
}
this.set = function(feature, property, value) {
var fid = feature.id() // string
var newft = this.collection.map(function(feat) {
feat = ee.Feature(feat)
var featid = feat.id()
var cond = fid.compareTo(featid).eq(0)
return ee.Feature(ee.Algorithms.If(cond,
feat.set(property, value), feat))
})
this.collection = newft
}
this.get = function(feature, property) {
var fid = feature.id() // string
var feat = ee.Feature(this.collection.filterMetadata('system:index', 'equals', fid).first())
return feat.get(property)
}
}
exports.FeatureCollection = FeatureCollection