-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathweighted_reductions.py
38 lines (29 loc) · 1.01 KB
/
weighted_reductions.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
import ee
import geemap
# Create a map centered at (lat, lon).
Map = geemap.Map(center=[40, -100], zoom=4)
# Load a Landsat 8 input image.
image = ee.Image('LANDSAT/LC08/C01/T1/LC08_044034_20140318')
# Creat an arbitrary region.
geometry = ee.Geometry.Rectangle(-122.496, 37.532, -121.554, 37.538)
# Make an NDWI image. It will have one band named 'nd'.
ndwi = image.normalizedDifference(['B3', 'B5'])
# Compute the weighted mean of the NDWI image clipped to the region.
weighted = ndwi.clip(geometry) \
.reduceRegion(**{
'reducer': ee.Reducer.sum(),
'geometry': geometry,
'scale': 30}) \
.get('nd')
# Compute the UN-weighted mean of the NDWI image clipped to the region.
unweighted = ndwi.clip(geometry) \
.reduceRegion(**{
'reducer': ee.Reducer.sum().unweighted(),
'geometry': geometry,
'scale': 30}) \
.get('nd')
# Observe the difference between weighted and unweighted reductions.
print('weighted:', weighted.getInfo())
print('unweighted', unweighted.getInfo())
# Display the map.
Map