0

I am trying to compute the NDVI index using the MODIS Landset in Python and to visualize it in a time series chart, but I am facing an error and I cannot understand where is the problem. This is my code:

campo_1= ee.Geometry.Polygon(
 [[10.6552805, 45.0993966], 
 [10.6584562, 45.0978214], 
 [10.6608595, 45.1003054], 
 [10.6576837, 45.1019109],
 [10.6552805, 45.0993966]]])`

campo1_points= ee.FeatureCollection.randomPoints(campo_1, 8).geometry()

def create_reduce_region_function(geometry,
                        reducer=ee.Reducer.mean(),
                        scale=1000,
                        crs='EPSG:4326',
                        bestEffort=True,
                        maxPixels=1e13,
                        tileScale=4):
        
    def reduce_region_function(img):
        stat = img.reduceRegion(
                reducer=reducer,
                geometry=geometry,
                scale=scale,
                crs=crs,
                bestEffort=bestEffort,
                maxPixels=maxPixels,
                tileScale=tileScale)
        
        return ee.FeatureCollection(geometry, stat).set({'millis': img.date().millis()})
    return reduce_region_function

def fc_to_dict(fc):
    prop_names = fc.first().propertyNames()
    prop_lists = fc.reduceColumns(
      reducer=ee.Reducer.toList().repeat(prop_names.size()),
      selectors=prop_names).get('list')

    return ee.Dictionary.fromLists(prop_names, prop_lists)

date_range= ee.DateRange('2023-07-24','2023-07-31')
ndvi = ee.ImageCollection('MODIS/006/MOD13A2').filterDate(date_range).select('NDVI')
reduce_ndvi = create_reduce_region_function(
    geometry=campo1_points, reducer=ee.Reducer.mean(), scale=1000, crs='EPSG:3310')
ndvi_stat_fc = ee.FeatureCollection(ndvi.map(reduce_ndvi)).filter(
    ee.Filter.notNull(ndvi.first().bandNames()))

fc_to_dict(ndvi_stat_fc).getInfo()

When trying to get the features of the variable I got the following error:

EEException: Image.bandNames: Parameter 'image' is required.

Can someone explain to me why and how to fix this error?

1 Answer 1

1

The reduce_region_function() needs a parameter img. The function is declared within the create_reduce_region_function() function and the img parameter is not mentioned there. Consequently, calling the function throws an error for the reduce_region_function().

What you can do is add the parameter img to the main create_reduce_region_function() and reformat the map function as follows.

def create_reduce_region_function(img, geometry,
                        reducer=ee.Reducer.mean(),
                        scale=1000,
                        crs='EPSG:4326',
                        bestEffort=True,
                        maxPixels=1e13,
                        tileScale=4):
                      # Rest similar to your code
ndvi_stat_fc=ee.FeatureCollection(ndvi.map(lambda i:create_reduce_region_function(img=i,geometry=campo1_points))).filter(
    ee.Filter.notNull(ndvi.first().bandNames()))

NB: You can change other parameters according to your requirements.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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