-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsentinel.py
43 lines (32 loc) · 1.31 KB
/
sentinel.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
import ee
def mask2clouds(image):
'''Mask the clouds from Sentinel 2 Imagery'''
qa = image.select('QA60')
# Bits 10 and 11 are clouds and cirrus, respectively.
cloudBitMask = 1 << 10
cirrusBitMask = 1 << 11
# Both flags should be set to zero, indicating clear conditions.
mask = qa.bitwiseAnd(cloudBitMask).eq(0) and (qa.bitwiseAnd(cirrusBitMask).eq(0))
return image.updateMask(mask).divide(10000)
def get_sentinel_image(geometry, start_date='2015-06-01', end_date='2016-12-31', top=5):
return ee.ImageCollection('COPERNICUS/S2')\
.filterDate(start_date, end_date)\
.filterBounds(geometry)\
.sort('CLOUDY_PIXEL_PERCENTAGE').limit(top)\
.map(mask2clouds)\
.select(['B4', 'B3', 'B2', 'B8', 'B11', 'B12'])\
.median()\
.clip(geometry)
def ndvi(sat_img):
return sat_img.normalizedDifference(['B8', 'B4']) # NDVI: RED - NIR
def ndwi(sat_img):
return sat_img.normalizedDifference(['B3', 'B8']) # NDVI: NIR - GREEN
def ndbi(sat_img):
return sat_img.normalizedDifference(['B11', 'B8']) # NDBI: SWIR1 - NIR
def bare(ndvi, ndwi):
return ndvi.lt(0.2).And(ndwi.lt(0))
def nd_mask(nd, threshold=None):
if threshold:
return nd.updateMask(nd.gte(threshold))
else:
return nd.updateMask(nd)