-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
83 lines (52 loc) · 1.88 KB
/
utils.py
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
# gee helpers
import ee
import math
UPPER_LEFT = 0
LOWER_LEFT = 1
LOWER_RIGHT = 2
UPPER_RIGHT = 3
PI = lambda: ee.Number(math.pi)
MAX_SATELLITE_ZENITH = 7.5
def line_from_coords(coordinates, fromIndex, toIndex):
return ee.Geometry.LineString(ee.List([
coordinates.get(fromIndex),
coordinates.get(toIndex)]))
def line(start, end):
return ee.Geometry.LineString(ee.List([start, end]))
def degToRad(deg):
return deg.multiply(PI().divide(180))
def value(list, index):
return ee.Number(list.get(index))
def radToDeg(rad):
return rad.multiply(180).divide(PI())
def where(condition, trueValue, falseValue):
trueMasked = trueValue.mask(condition)
falseMasked = falseValue.mask(invertMask(condition))
return trueMasked.unmask(falseMasked)
def invertMask(mask):
return mask.multiply(-1).add(1)
def x(point):
return ee.Number(ee.List(point).get(0))
def y(point):
return ee.Number(ee.List(point).get(1))
def determine_footprint(image):
footprint = ee.Geometry(image.get('system:footprint'))
bounds = ee.List(footprint.bounds().coordinates().get(0))
coords = footprint.coordinates()
xs = coords.map(lambda item: x(item))
ys = coords.map(lambda item: y(item))
def findCorner(targetValue, values):
diff = values.map(lambda value: ee.Number(value).subtract(targetValue).abs())
minValue = diff.reduce(ee.Reducer.min())
idx = diff.indexOf(minValue)
return coords.get(idx)
lowerLeft = findCorner(x(bounds.get(0)), xs)
lowerRight = findCorner(y(bounds.get(1)), ys)
upperRight = findCorner(x(bounds.get(2)), xs)
upperLeft = findCorner(y(bounds.get(3)), ys)
return ee.List([upperLeft, lowerLeft, lowerRight, upperRight, upperLeft])
def replace_bands(image, bands):
result = image
for band in bands:
result = result.addBands(band, overwrite=True)
return result