Google Earth Engine – How to Fix reduceRegions Is Not a Function Error

google-earth-engine

Note that this is a follow-up question to a previous one that was answered. I have a Shapefile with polygons for 1000ish cities of interest. I'm trying to retrieve the land surface temp (LST) for those cities; the ultimate goal is to export a .csv file that'll show the LST for each of those cities for my selected time frame.

I'm trying to aggregate/dissolve my pixels based on each city's boundaries. GEE tells me that reduceRegions is not a function.

//Import cities with population >= 500,000. [Link to asset][1] 
var cities = ee.FeatureCollection('filepath');

// Import day and night time land surface temperature (LST) by season from MODIS
var modis = ee.ImageCollection("MODIS/006/MOD11A1");

// WINTER.  
var winter_day = modis.filter(ee.Filter.date('2009-12-01', '2010-03-01')).select('LST_Day_1km');

// Convert to Celsius. 
var winter_day_C = winter_day.map(function(image) {
  return image
    .multiply(0.02)
    .subtract(273.15)
    .copyProperties(image, ['system:time_start']);
});

// Reduce LST data to Shapefile with cities. GEE says this is not a function.
var Winter_Day_LST = winter_day_C.reduceRegions({
  collection: cities,
  reducer: ee.Reducer.mean(),
  scale: 100,
});

My first attempt at troubleshooting involved rewriting the reduceRegions function. That appeared to resolve the "not a function" issue, but I encountered an error when exporting. "Error: Unable to use a collection in an algorithm that requires a feature or image. This may happen when trying to use a collection of collections where a collection of features is expected; use flatten, or map a function to convert inner collections to features. Use clipToCollection (instead of clip) to clip an image to a collection."

//Rewriting reduceRegions code
var Winter_Day_LST = winter_day_C.map(function(image){
  return image.reduceRegions({
  collection: cities,
  reducer: ee.Reducer.mean(),
  scale: 100,
  });
});

 // Export 
    Export.table.toDrive({
      collection: Winter_Day_LST,
      description: '2010_WinterDayLST_export',
      fileFormat: 'CSV'
    });

I tried to clip, and now the error with exporting is "collection must be a FeatureCollection."

var clippedLST = winter_day_C.mean().clip(cities);

// Export 
Export.table.toDrive({
  collection: clippedLST,
  description: '2010_WinterDayLST_export',
  fileFormat: 'CSV'
});

Please let me know what I'm doing wrong.

Best Answer

reduceRegions is a method of an Image not an ImageCollection and winter_day_C is an ImageCollection, not an Image.

If you want to reduce spatially to create a single mean value per city by image (date), then map reduceRegions to each image in the ImageCollection.

// Import day and night time land surface temperature (LST) by season from MODIS
var modis = ee.ImageCollection("MODIS/006/MOD11A1");

// WINTER.  
var winter_day = modis.filter(ee.Filter.date('2009-12-01', '2010-03-01')).select('LST_Day_1km');

// Convert to Celsius. 
var winter_day_C = winter_day.map(function(image) {
  return image
    .multiply(0.02)
    .subtract(273.15)
    .reduceRegions({  // <---- reduceRegions on the Image object
      collection: cities,
      reducer: ee.Reducer.mean(),
      scale: 100,
    })
    .copyProperties(image, ['system:time_start']);
});

To avoid the "Error: Unable to use a collection in an algorithm that requires a feature or image. This may happen when trying to use a collection of collections where a collection of features is expected;" do what the error tells you to and "use flatten, or map a function to convert inner collections to features."

// Export 
Export.table.toDrive({
  collection: winter_day_C.flatten(),  // <----- flatten()
  description: '2010_WinterDayLST_export',
  fileFormat: 'CSV'
});

enter image description here