1

I had already asked this question for Earth Engine Javascript.The link is given below I have NDVI layer in Earth Engine. I want to assign and calculate area of good and bad ndvi area I wamted to do the same thing in Python API for earth engine. But it is showing the following error- EEException: Invalid argument for ee.Reducer(): ({'geometry': [[74.022029, 20.103245], [74.022029, 20.116564], [74.038048, 20.116564], [74.038048, 20.103245]], 'reducer': , 'scale': 10},). Must be a ComputedObject. I am also sharing a snippet of my code in Python-

    ndvi = clip.normalizedDifference(['B8', 'B4'])
    #NDVI=ndvi.multiply(100).uint8()
    bad=ndvi.gte(0)and(ndvi.lte(0.2)).rename('bad')
    avg=ndvi.gt(0.2) and(ndvi.lte(0.35)).rename('avg')
    good=ndvi.gt(0.35)and(ndvi.lte(1)).rename('good')
    ndvi.addBands(['bad', 'avg', 'good'])
    print (ndvi)
    areas = ndvi.select(['bad', 'avg','good']).multiply(ee.Image.pixelArea()).reduceRegion({'reducer':ee.Reducer 
    .sum(),'geometry':geometry1,'scale':10});
    print (areas)
2

You can't specify arguments with a dictionary in Python; Python has it's own method for specifying keyword arguments:

areas = (ndvi.select(['bad', 'avg','good']).multiply(ee.Image.pixelArea())
    .reduceRegion(reducer=ee.Reducer.sum(), geometry=geometry1, scale=10))
  • Hi Noel. Thanks for the reply but it always returning same values for bad, avg and good which should not be and ndvi color inference is also not matching with the values provided as good, avg or bad – Avik Betal Jul 30 at 5:51
  • Your "and" syntax is broken; you're missing an important '.'. It should be: bad=ndvi.gte(0).and(ndvi.lte(0.2)).rename('bad') – Noel Gorelick Jul 31 at 8:10
  • Not working :( bad=ndvi.gte(-0.99).and((ndvi.lte(0.4))).rename('bad') ^ SyntaxError: invalid syntax – Avik Betal Aug 1 at 9:14
  • Oh, right. "and" is a reserved keyword in Python. You have to use "And()" instead (capital letter). It's one of the few places where we couldn't make the python library match the javascript. – Noel Gorelick Aug 2 at 10:27
  • Hi!, Getting another issue : 'Required argument (%s) missing to function: %s' % (name, self)) ee.ee_exception.EEException: Required argument (image2) missing to function: Returns 1 iff both values are non-zero for each matched pair of bands in image1 and image2. If either image1 or image2 has only 1 band, then it is used against all the bands in the other image. If the images have the same number of bands, but not the same names, they're used pairwise in the natural order. The output bands are named for the longer of the two inputs, or if they're equal in length, in image1's order. The type – Avik Betal Aug 13 at 8:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.