Python – Fixing Google Earth Engine EEException for Type Mismatch

geemapgoogle-earth-enginepython

This code selects Sentinel-2 data and de-clouds it using the S2_CLOUD_PROBABILITY data set. Then, cloud-free Sentinel-2 images and NDVI were synthesized.The problem is that the images cannot be displayed and I get the next error message:

EEException: Filter.eq, argument 'leftField': Invalid type.
Expected type: String.
Actual type: Dictionary<String>.
Actual value: {rightField=system:index, leftField=system:index}

I do all this in Python Jupyter notebook and using the package 'geemap'. This is my code:

river_shp='D:/paper1 folder/data/shp/Changjiang/CJ.shp'

river = geemap.shp_to_ee(river_shp)

S2_Cloud = ee.ImageCollection("COPERNICUS/S2_CLOUD_PROBABILITY")

S2 =ee.ImageCollection("COPERNICUS/S2")

roi = river

styling = {'color':'black', 'fillColor':'00000000'}

Map=geemap.Map()

Map.centerObject(roi,6)


def CloudProbability(img,thread):
  prob = img.select('probability')
  return img.updateMask(prob.lte(thread))

def MergeImages(primary, secondary):
  join = ee.Join.inner()
  filter = ee.Filter.equals({
    'leftField': 'system:index',
    'rightField': 'system:index'
  })
  Col = join.apply(primary, secondary, filter)
  def func_ach(image):
    img1 = ee.Image(image.get("primary"))
    img2 = ee.Image(image.get("secondary"))
    return ee.Image.cat(img1,img2)
  Col = Col.map(func_ach)
  return ee.ImageCollection(Col)

def main():
  startDate = "2021-5-1"
  endDate = "2021-10-1"
  S2Img1 = S2.filterDate(startDate, endDate) \
                  .filterBounds(roi)
  S2Img2 = S2_Cloud.filterDate(startDate, endDate) \
                        .filterBounds(roi)
  S2Imgs = MergeImages(S2Img1, S2Img2)
  def func_irn(image):
    return CloudProbability(image, 30)
  S2Imgs = S2Imgs.map(func_irn)

  def func_tjh (img):
    return img.normalizedDifference(['B8','B4'])
  ndvi_max = S2Imgs.map(func_tjh).max().clip(roi)
  Map.addLayer(S2Imgs,{'bands':['B8', 'B4', 'B3'], 'min':0, 'max':3000},'S2Imgs')
  Map.addLayer(ndvi_max, {'min':0, 'max':0.9}, "ndvi")
main()
Map.addLayer(roi.style(styling),{},'CJ')

Best Answer

This is how to use ee.Filter.equals in python: filter = ee.Filter.equals(leftField= 'system:index',rightField= 'system:index')

Related Question