简体   繁体   English

使用 Python API 对 GEE 图像集合进行阈值处理

[英]Thresholding a GEE Image Collection with Python API

I am looking for a way to thresholding image collection by a given with Python API, in this script i want to calculate images per year and make classification With the python API I get the error: 'ImageCollection' object has no attribute 'gte' I am looking for a way to thresholding image collection by a given with Python API, in this script i want to calculate images per year and make classification With the python API I get the error: 'ImageCollection' object has no attribute 'gte'

collection = ee.ImageCollection('MODIS/006/MCD43A4')     .filterBounds(study_area)
    
yearlist = range(start_year, end_year) \
#ee.List([2013,2014,2015, 2016, 2017, 2018,2019,2020])

#CLOUD MASKING
def mask_clouds(image):
  # Select the QA band.
  QA = image.select('BRDF_Albedo_Band_Mandatory_Quality_Band1')
  # Make a mask to get bit 10, the internal_cloud_algorithm_flag bit.
  bitMask = 1 << 10
  # Return an image masking out cloudy areas.
  return image.multiply(0.0001).updateMask(QA.bitwiseAnd(bitMask).eq(0))

#PERHITUNGAN MNDVI

def calculate_mndvi (year):
    image = collection      .filter(ee.Filter.calendarRange(year, year, 'year'))       .map(mask_clouds)     .median()
    NDVI = image    .normalizedDifference(['Nadir_Reflectance_Band2', 'Nadir_Reflectance_Band1'])      .rename('NDVI')

    H1 = image.expression(
              '((C11*RED)-BLUE+C12)/((NIR*NIR)-(RED*RED))', {
              'C11': 0.55,
              'C12': 0.12,
              'RED': image.select('Nadir_Reflectance_Band1'),
              'BLUE': image.select('Nadir_Reflectance_Band3'),
              'NIR': image.select('Nadir_Reflectance_Band2')
              }).rename('H1')

    H2 = image.expression(
              '1/((C11*RED)-BLUE+C12)', {
              'C11': 0.55,
              'C12': 0.12,
              'RED': image.select('Nadir_Reflectance_Band1'),
              'BLUE': image.select('Nadir_Reflectance_Band3')
              }).rename('H2')

    mndvi = image.expression(
              '((1+(C2*H2))*NDVI)/(1+(C1*H1))', {
              'C1': 0.6,
              'C2': 0.03,
              'H1': H1,
              'H2': H2,
              'NDVI' : NDVI
              }).rename('MNDVI')

    return mndvi    .set('year', year)     .set('month', 1)     .set('date', ee.Date.fromYMD(year,1,1))     .set('system:time_start',ee.Date.fromYMD(year,1,1))

mndvi_images = ee.ImageCollection.fromImages([
    calculate_mndvi(year) for year in yearlist
])

print(mndvi_images.getInfo())

threshold1 = mndvi_images.select('MNDVI').gte(-1).And(mndvi_images.select('MNDVI').lte(0.2)).selfMask()
threshold2 = mndvi_images.select('MNDVI').gte(0.2).And(mndvi_images.select('MNDVI').lte(0.4)).selfMask().multiply(2)
threshold3 = mndvi_images.select('MNDVI').gte(0.4).And(mndvi_images.select('MNDVI').lte(0.6)).selfMask().multiply(3)
threshold4 = mndvi_images.select('MNDVI').gte(0.6).And(mndvi_images.select('MNDVI').lte(1)).selfMask().multiply(4)

threshold1 = threshold1.toShort().select(0).rename('MNDVI')
threshold2 = threshold2.toShort().select(0).rename('MNDVI')
threshold3 = threshold3.toShort().select(0).rename('MNDVI')
threshold4 = threshold4.toShort().select(0).rename('MNDVI')

# STACKING LAYER

stacking_layer = ee.ImageCollection.fromImages([threshold1, threshold2, threshold3, threshold4])
print(stacking_layer,'staking'+i)

# Mosaic the ImageCollection.
stacking = stacking_layer.mosaic()
Map.addLayer(stacking, {'palette': ['e81410',  'f0fc0a',  '30bf21', '198f0d'], 'min':1, 'max':4},'Vegetation'+i)

Every little help, I really appreciate it.每一个小小的帮助,我真的很感激。 Thank you谢谢

.gte , .lte , etc. operate on images, not image collections. .gte.lte等对图像进行操作,而不是图像 collections。 You need to create a function to operate on an image and use .map to apply it to each image in the collection.您需要创建一个 function 来对图像进行操作,并使用.map将其应用于集合中的每个图像。 Like this:像这样:

def threshold1_image(image):
    return image.gte(-1).And(image.lte(0.2)).selfMask()


threshold1 = mndvi_images.select("MNDVI").map(threshold1_image)

Or you could use a lambda function:或者您可以使用 lambda function:

threshold1 = mndvi_images.select("MNDVI").map(lambda x: x.gte(-1).And(x.lte(0.2)).selfMask())         

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM