4

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.

1
  • Please provide a link to the previous question that you mention at the start of yours.
    – PolyGeo
    Aug 14, 2021 at 5:06

2 Answers 2

5

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

1
  • Thanks for the help. It resolved the above errors, but the exported data unfortunately don't look correct. I don't see any of the LST values (i.e., no column titled "mean") or a single value per city. I noticed that the above link to the Shapefile of all the cities was broken. It's included below if that helps - note that it's a huge file. code.earthengine.google.com/?asset=users/catngo/…
    – Cat C
    Aug 14, 2021 at 22:35
1

Thank you user2856 for helping me understand the nuances between image and image collection. As mentioned in my comment, I unfortunately still struggled after implementing the recommendations. The exported data didn't have any mean LSTs. However, I think I resolved my issue based on this posting and just wanted to share. (There's one wrinkle in that each city has an LST for each day within my time period. I can live with that and will move the rest of my analysis to R). I'm also open to critique if anyone has any!

//Import cities with population >= 500,000
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');
print('winter_day', winter_day)

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

//Convert image collection to multiband image because reduceRegions only works on image, not image collection
var multiband = winter_day_C.toBands();
print(multiband)

//Get mean LST per city
var Winter_Day_LST = multiband.reduceRegions({
  collection: cities,
  reducer: ee.Reducer.mean(),
  scale: 100,
});

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

Your Answer

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

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