-
Notifications
You must be signed in to change notification settings - Fork 110
/
composite
69 lines (54 loc) · 1.75 KB
/
composite
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
/*
* Author: Rodrigo E. Principe
* License: Apache 2.0
* email: [email protected]
Composite Methods
*/
var tools = require('users/fitoprincipe/geetools:tools')
var euclidean_distance = function(i, i2) {
var bandsi = i.bandNames()
var bandsi2 = i2.bandNames()
var proxy = tools.image.fromList(bandsi, 0)
i = proxy.where(i.gt(0), i)
i2 = proxy.where(i2.gt(0), i2)
var a = i.subtract(i2)
var b = a.pow(2)
var c = b.reduce('sum')
var d = c.sqrt()
return d.rename('distance')
}
var sum_distance = function(i, rest) {
var accum = ee.Image(0).rename('sumdist')
return ee.Image(rest.iterate(function(im, ini) {
ini = ee.Image(ini)
im = ee.Image(im)
var dist = ee.Image(euclidean_distance(i, im)).rename('sumdist')
return ini.add(dist)
}, accum))
}
var medoid = function(collection, bands) {
//collection = collection.map(function(im){return im.unmask()})
var first_image = ee.Image(collection.first())
var ibands = first_image.bandNames()
bands = bands || ibands
//collection = collection.select(bands)
var enumerated = tools.imageCollection.enumerateProperty(collection)
var collist = enumerated.toList(enumerated.size())
var imlist = ee.List(collist.map(function(im){
im = ee.Image(im)
var n = ee.Number(im.get('enumeration'))
var filtered = tools.list.removeIndex(collist, n)
.map(function(im){
im = ee.Image(im)
return im.select(bands)
})
var dist = sum_distance(
im.select(bands),
filtered
).multiply(-1)
return im.addBands(dist)
}))
var medcol = ee.ImageCollection.fromImages(imlist)
return medcol.qualityMosaic('sumdist')
}
exports.medoid = medoid