0

What is this error? It clearly says we have a FeatureCollection then it says Cannot flatten an object that is not a collection.

FeatureCollection (Error)
Cannot flatten an object that is not a collection.

Reproducible code:

///////////////////////////////////////////////
///
///                           functions definitions start
///
/// Function to mask clouds using the Sentinel-2 QA band.
function maskS2clouds(image) {
    var qa = image.select('QA60');

    // Bits 10 and 11 are clouds and cirrus, respectively.
    var cloudBitMask = 1 << 10;
    var cirrusBitMask = 1 << 11;

    // Both flags should be set to zero, indicating clear conditions.
    var mask = qa.bitwiseAnd(cloudBitMask).eq(0).and(
                        qa.bitwiseAnd(cirrusBitMask).eq(0));

    // Return the masked and scaled data, without the QA bands.
    return image.updateMask(mask).divide(10000)
                          .select("B.*")
                          .copyProperties(image, ["system:time_start"]);
}

////////////////////////////////////////////////
///
/// add Day of Year to an image
///

var addDate_to_image = function(image){
  var doy = image.date().getRelative('day', 'year');
  var doyBand = ee.Image.constant(doy).uint16().rename('doy');
  doyBand = doyBand.updateMask(image.select('B8').mask());

  return image.addBands(doyBand);
};

////////////////////////
///
/// add Day of Year to an imageCollection
///

var addDate_to_collection = function(collec){
  var C = collec.map(addDate_to_image);
  return C;
};

////////////////////////////////////////////////
///
///
/// add NDVI to an image

var addNDVI_to_image = function(image) {
  var ndvi = image.normalizedDifference(['B8', 'B4']).rename('NDVI');
  return image.addBands(ndvi);
};

////////////////////////////////////////////////
///
///
/// add NDVI to an imageCollection

var add_NDVI_collection = function(image_IC){
  var NDVI_IC = image_IC.map(addNDVI_to_image);
  return NDVI_IC;
};

var extract_sentinel_IC = function(a_feature){
    // var start_date = 
    // var end_date = 
    // var cloud_percentage = 
    // var geom = ee.Feature(feature_col).geometry();
    var geom = a_feature.geometry();
    var imageC = ee.ImageCollection('COPERNICUS/S2')
                .filterDate('2012-01-01', '2019-12-31')
                .filterBounds(geom)
                //.filterMetadata('CLOUDY_PIXEL_PERCENTAGE', "less_than", 10)
                .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 10));

    imageC = imageC.map(maskS2clouds);
    imageC = imageC.select(['B8', 'B4', 'B3', 'B2']);

    imageC = addDate_to_collection(imageC);
    imageC = add_NDVI_collection(imageC);
    //imageC = imageC.map(add_NDVI_collection)
  return imageC;
};

///
///                         functions definitions end
///
///////////////////////////////////////////////

var city = ee.Feature(ee.Geometry.Rectangle(-122.42, 37.78, -122.4, 37.8), 
                      {label: 'City'});

var city_IC = extract_sentinel_IC(city);
print (city_IC)
var city_IC_flatten = city_IC.flatten()
print (city_IC_flatten)
2
  • Can you please provide your script or an example that reproduces the error so that we can investigate why it is triggered. Commented Feb 12, 2020 at 21:10
  • will do in a minute (no one in the world has not seen this before?) Commented Feb 12, 2020 at 22:01

1 Answer 1

2

In the error message

FeatureCollection (Error)
Cannot flatten an object that is not a collection.

the first word being FeatureCollection does not mean that the object was a FeatureCollection. Rather, it means that as far as the Earth Engine API running in your web browser knows, it asked the Earth Engine servers to compute something that will be a FeatureCollection if the computation succeeds (because the type of the result of flatten() is FeatureCollection). But there was an error instead, and when the Earth Engine Code Editor gets an error from the server, it always displays in this format:

<Type of expected result> (Error)
<Error message>

This type is intended to help remind you what it was you were trying to compute, since there isn't much other information available.


Now I will discuss the specific error “Cannot flatten an object that is not a collection.”

(As of February 28, 2020, this error message has been revised; the new error message is “The input collection must be a collection of collections, but the element with id … was …, which is not a collection.”)

The error is not about the collection, but its contents. The purpose of flatten is to convert a collection containing collections into a collection of the features in those collections. In your case, as demonstrated by the print (city_IC) immediately above, you have an ImageCollection, whose contents are always images. Images are not collections — they do not contain features. So, you cannot apply flatten to an ImageCollection (except in the edge case where the collection is empty).

I don't know exactly what you are trying to achieve, but flatten is not the right tool to do it.

3
  • Thanks a lot Kevin. appreciate it. What I was trying to do, was to export the imageCollection as a matrix/dataframe so I can do further analysis on it. But I got the result asked in this question gis.stackexchange.com/questions/350517/… Commented Feb 13, 2020 at 18:39
  • ... comment [2] I would like to be able to do [as an example] {var series2 = ui.Chart.image.doySeriesByYear( collection, 'NDVI', forest, ee.Reducer.mean(), 500);} in my computer. So, I need to export all information of image collection like bands time series as a CSV file lets say Commented Feb 13, 2020 at 18:48
  • @NoLie Ah, I understand now. I don't have a good answer for your other question, so it's good that you posted it separately so other people will see it.
    – Kevin Reid
    Commented Feb 13, 2020 at 21:49

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.