Python GEE 从 Landsat 图像中提取特征收集时间序列
Python GEE to extract featurecollection timeseries from Landsat imageries
我有一个特征集,我需要从 Landsat 图像中提取 NDWI 的时间序列。
这是我用来提取时间序列的 javascript 功能代码:https://code.earthengine.google.com/5992f0f029b10a1c57c8ed34e73a368f
现在我正尝试在 python 中复制相同的脚本,但它在包含 image.mask().and(cloud01.not()) 的行中显示语法错误.我想知道如何在 python.
中指定相同的条件
如有任何帮助,我们将不胜感激。
提前致谢。
我的python代码如下:
import datetime
import ee
ee.Initialize()
table = ee.FeatureCollection("users/nbiswas/Buffered_reservoirs_seasia")
L8 = ee.ImageCollection("LANDSAT/LC08/C01/T2"),
table2 = ee.FeatureCollection("users/nbiswas/Madushan"),
table3 = ee.FeatureCollection("users/nbiswas/india_reservoirs_buffer_3km");
class ReservoirExtent():
def __init__(self):
self.table = table2
self.geometry = table2.geometry();
self.Date_Start = ee.Date('2008-01-01');
self.Date_End = ee.Date('2018-07-31');
self.cloud_thresh = 20;
def clipimage(self, img):
return img.clip(self.geometry);
def cloudfunction(self, image):
#use add the cloud likelihood band to the image
CloudScore = ee.Algorithms.Landsat.simpleCloudScore(image);
#isolate the cloud likelihood band
quality = CloudScore.select('cloud');
#get pixels above the threshold
cloud01 = quality.gt(self.cloud_thresh);
#create a mask from high likelihood pixels
cloudmask = image.mask().and(cloud01.not());
#mask those pixels from the image
return image.updateMask(cloudmask);
def l8Ndwi(self, img):
ndwi = img.normalizedDifference(['B3', 'B5']).rename('NDWI');
return img.addBands(ndwi);
def areadate(self, img):
area = img.gt(0).multiply(ee.Image.pixelArea()).divide(1000000).reduceRegion(ee.Reducer.sum(), self.geometry, 30).get('NDWI');
return img.set('area', area).set('date', img.get('system:time_start'));
def extentseries(self, reservoir):
l8images = L8.filterDate(self.Date_Start,self.Date_End).filterBounds(self.geometry);
print(l8images);
l8images = l8images.map(self.clipimages);
l8images = l8images.map(self.cloudfunction);
l8images = l8images.select(["B3","B5"]);
# calculate ndwi for each image in imagecollection
l8ndwi = l8images.map(self.l8Ndwi);
mosaics = l8ndwi.map(self.areadate)
mosaics = ee.ImageCollection(mosaics);
mosaic = mosaics.sort('system:time_start', false);
list = mosaic.reduceColumns(ee.Reducer.toList(2), ['date', 'area']).get('list');
print(list);
#return reservoir.set(ee.Dictionary(ee.List(list).flatten()));
if __name__ == '__main__':
forecast = ReservoirExtent()
ppp = forecast.extentseries(table2);
print(ppp.getInfo());
尝试 And
而不是 and
:
image.mask().<b>A</b>nd(cloud01.not())
这是为了区分 Python and
运算符和来自 Google Earth Engine 的 And
函数的语法更改。
希望对您有所帮助!
我有一个特征集,我需要从 Landsat 图像中提取 NDWI 的时间序列。
这是我用来提取时间序列的 javascript 功能代码:https://code.earthengine.google.com/5992f0f029b10a1c57c8ed34e73a368f
现在我正尝试在 python 中复制相同的脚本,但它在包含 image.mask().and(cloud01.not()) 的行中显示语法错误.我想知道如何在 python.
中指定相同的条件如有任何帮助,我们将不胜感激。
提前致谢。
我的python代码如下:
import datetime
import ee
ee.Initialize()
table = ee.FeatureCollection("users/nbiswas/Buffered_reservoirs_seasia")
L8 = ee.ImageCollection("LANDSAT/LC08/C01/T2"),
table2 = ee.FeatureCollection("users/nbiswas/Madushan"),
table3 = ee.FeatureCollection("users/nbiswas/india_reservoirs_buffer_3km");
class ReservoirExtent():
def __init__(self):
self.table = table2
self.geometry = table2.geometry();
self.Date_Start = ee.Date('2008-01-01');
self.Date_End = ee.Date('2018-07-31');
self.cloud_thresh = 20;
def clipimage(self, img):
return img.clip(self.geometry);
def cloudfunction(self, image):
#use add the cloud likelihood band to the image
CloudScore = ee.Algorithms.Landsat.simpleCloudScore(image);
#isolate the cloud likelihood band
quality = CloudScore.select('cloud');
#get pixels above the threshold
cloud01 = quality.gt(self.cloud_thresh);
#create a mask from high likelihood pixels
cloudmask = image.mask().and(cloud01.not());
#mask those pixels from the image
return image.updateMask(cloudmask);
def l8Ndwi(self, img):
ndwi = img.normalizedDifference(['B3', 'B5']).rename('NDWI');
return img.addBands(ndwi);
def areadate(self, img):
area = img.gt(0).multiply(ee.Image.pixelArea()).divide(1000000).reduceRegion(ee.Reducer.sum(), self.geometry, 30).get('NDWI');
return img.set('area', area).set('date', img.get('system:time_start'));
def extentseries(self, reservoir):
l8images = L8.filterDate(self.Date_Start,self.Date_End).filterBounds(self.geometry);
print(l8images);
l8images = l8images.map(self.clipimages);
l8images = l8images.map(self.cloudfunction);
l8images = l8images.select(["B3","B5"]);
# calculate ndwi for each image in imagecollection
l8ndwi = l8images.map(self.l8Ndwi);
mosaics = l8ndwi.map(self.areadate)
mosaics = ee.ImageCollection(mosaics);
mosaic = mosaics.sort('system:time_start', false);
list = mosaic.reduceColumns(ee.Reducer.toList(2), ['date', 'area']).get('list');
print(list);
#return reservoir.set(ee.Dictionary(ee.List(list).flatten()));
if __name__ == '__main__':
forecast = ReservoirExtent()
ppp = forecast.extentseries(table2);
print(ppp.getInfo());
尝试 And
而不是 and
:
image.mask().<b>A</b>nd(cloud01.not())
这是为了区分 Python and
运算符和来自 Google Earth Engine 的 And
函数的语法更改。
希望对您有所帮助!