Google Earth Engine – Troubleshooting reduceRegion Not Working

google-earth-engine

I want to get the NDVI value for each point of my featureCollection vector. I had used reduceRegion for that in codeeditor javascript of GEE. I when i using the same function in python api of GEE, it is showing 'Unrecognized argument type to convert to a FeatureCollection'. May be some thing need to change in python api for the reducer function. Please help me in this regards.

import ee

import time
import datetime
import json
from ee.batch import Export

ee.Initialize()

startyear=2018
startmonth=01
startday=01

endyear=2018
endmonth=02
endday=01
def maskS2clouds(image):
    qa = image.select('QA60');
    cloudBitMask = ee.Number(2).pow(10).int();
    cirrusBitMask = ee.Number(2).pow(11).int();
    mask = qa.bitwiseAnd(cloudBitMask).eq(0) and(
             qa.bitwiseAnd(cirrusBitMask).eq(0));
    return image.updateMask(mask).divide(10000);

print("test")

boundary = ee.FeatureCollection('ft:1T-XrxMki2k49fhQdhMxesWKBoyyUJiKN70pErh9z')
#print(boundary)
gtpoints = ee.FeatureCollection('ft:1ZvFeFO5jHGo3VExlKNc-kdL-3hmJSeyfLzqLvHhy')
print (gtpoints)
image = (ee.ImageCollection('COPERNICUS/S2').
         filterBounds(boundary).
         filterDate(datetime.datetime(startyear,startmonth,startday),
                     datetime.datetime(endyear,endmonth,endday)).
         sort("CLOUD_COVERAGE_ASSESMENT").
         map(maskS2clouds). 
         limit(20))

mosaic = image.mosaic();
    #median = mosaic.median();
palette = ['FFFFFF', 'CE7E45', 'DF923D', 'F1B555', 'FCD163', '99B718',
               '74A901', '66A000', '529400', '3E8601', '207401', '056201',
               '004C00', '023B01', '012E01', '011D01', '011301'];
clip = mosaic.clip(boundary)
#median = clip.median()
#print(clip)
ndvi = clip.normalizedDifference(['B8', 'B4'])
NDVI=ndvi.multiply(100).uint8()
#print(clip)
palette = ','.join(palette)
vis = {'min': 0, 'max': 300, 'bands':'nd', 'palette': palette}
toexport = NDVI.visualize(**vis).addBands(NDVI);
#print(toexport,'toexport')
points_ndvi = ndvi.reduceRegions({
  'collection':gtpoints,
  'reducer':ee.Reducer.first(),
  'scale':10
})
properties = ee.Feature(points_ndvi.first()).propertyNames()
new_names = properties.replace('first', 'ndvi')
points_ndvi = points_ndvi.select(properties, new_names)
print(points_ndvi,"test")

Best Answer

You can't use dictionaries of arguments in python; python has it's own way of doing keyword arguments for that:

points_ndvi = ndvi.reduceRegions(
  collection=gtpoints,
  reducer=ee.Reducer.first(),
  scale=10
)

(also, you don't need to use datetime):

.filterDate(ee.Date.fromYMD(startyear,startmonth,startday),
            ee.Date.fromYMD(endyear,endmonth,endday))