2

I'm trying to extract a historical series of ndvi by land cover class (in particular Trees, Shrubland and Grassland), using the ESA collection (ESA/WORLDCOVER/200V) and clipping on my study area (geometry: Palo) I followed several suggestions already given among the available questions, but I'll stop at this point (I attach script). it shows the following error:

Collection.geometry: 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.'

if I try to run on a single class (ex. Trees) it runs but the chart shows as many lines as the polygons referred to the class. I therefore need to find a command (the equivalent of "dissolve" on QGIS) that merges all polygons of a given class together. I've already tried the various .dissolve commands but they don't work.

(this is a temporary script)

var ESA = ee.ImageCollection("ESA/WorldCover/v100").first();
var cover = ESA.select('Map').clip(Palo);

var class10 = cover.eq(10); 
var class20 = cover.eq(20);
var class30 = cover.eq(20);

var Trees = cover.updateMask(class10)
.reduceToVectors({ geometry: Palo,
 scale: 10,
 geometryType:'polygon',
 labelProperty: 'Trees',
});


var Shrub = cover.updateMask(class20)
.reduceToVectors({ geometry: Palo,
 scale: 10,
 geometryType:'polygon',
 labelProperty: 'Shrubland'
});

var Grass = cover.updateMask(class30)
.reduceToVectors({ geometry: Palo,
 scale: 10,
 geometryType:'polygon',
 labelProperty: 'Shrubland'
});

var regions = ee.FeatureCollection([
  ee.Feature(Trees,{label : 'Trees'}),
  ee.Feature(Shrub,{label : 'Shrubland'}),
  ee.Feature(Grass,{label : 'Grassland'})]
  );
  
var S2 = ee.ImageCollection('COPERNICUS/S2')
.filterBounds(Palo)
.filterDate('2020-01-01','2020-01-31');

var addNDVI = function(image) {
return image.addBands(image.normalizedDifference(['B8', 'B4']));
};
var S2 = S2.map(addNDVI);

var NDVI = S2.select(['nd']);
var NDVImed = NDVI.median(); 
//print('S2',S2);
var visParams = {
  min: -0.1,
  max: 1.0,
  palette: [
    'FFBB22', 'FFFF4C', '648C00', 
    ],
};

var plotNDVI = ui.Chart.image.seriesByRegion(
  S2, 
  regions,
  ee.Reducer.mean(),
  'nd',10)
  .setChartType('LineChart')
  .setSeriesNames(['Trees', 'Shrubland','Grassland'])
  .setOptions({
    interpolateNulls: true,
    lineWidth: 1,
    pointSize: 3,
    title: 'NDVI annual evolution',
    hAxis: {title: 'Date'},
    vAxis: {title: 'NDVI'},
    series: {0:{color: 'red'}
    }
  });
  
print(plotNDVI);

1 Answer 1

1

Issues are produced because you are trying to use a Feature Collection of Feature Collections as regions. It must be a Feature Collection of MultiPolygons that it must be flatten as in following code (I used an arbitrary area in USA for layer named "Palo" and it includes "Trees", "Shrub" and "Grass" classes):

var Palo = ee.Geometry.Polygon(
        [[[-98.01058032937533, 27.921349480149573],
          [-98.01058032937533, 27.917254083529787],
          [-97.99895027108676, 27.917254083529787],
          [-97.99895027108676, 27.921349480149573]]], null, false);

var ESA = ee.ImageCollection("ESA/WorldCover/v100").first();

print(ESA);

Map.centerObject(Palo);
//Map.addLayer(Palo);
var cover = ESA.clip(Palo);

Map.addLayer(cover, {}, "cover");

var class10 = cover.eq(10);
var class20 = cover.eq(20);
var class30 = cover.eq(30);

var Trees = cover.updateMask(class10)
                 .reduceToVectors({geometry: Palo, scale: 30});

var Shrub = cover.updateMask(class20)
                 .reduceToVectors({ geometry: Palo, scale: 30});

var Grass = cover.updateMask(class30)
  .reduceToVectors({ geometry: Palo, scale: 30});

Map.addLayer(Trees, {}, 'Trees');
print("Trees", Trees);

var Trees_list = Trees.toList(Trees.size());

var geomTrees = Trees_list.map(function (ele) {
  
  return ee.Feature(ele).geometry();
  
});

Trees = ee.FeatureCollection(ee.Feature(ee.Geometry.MultiPolygon(geomTrees)));

var Shrub_list = Shrub.toList(Shrub.size());

var geomShrub = Shrub_list.map(function (ele) {
  
  return ee.Feature(ele).geometry();
  
});

Shrub = ee.FeatureCollection(ee.Feature(ee.Geometry.MultiPolygon(geomShrub)));

Map.addLayer(Shrub, {}, 'Shrub');
print("Shrub", Shrub);

var Grass_list = Grass.toList(Grass.size());

var geomGrass = Grass_list.map(function (ele) {
  
  return ee.Feature(ele).geometry();
  
});

Grass = ee.FeatureCollection(ee.Feature(ee.Geometry.MultiPolygon(geomGrass)));

Map.addLayer(Grass, {}, 'Grass');
print("Grass", Grass);

var regions = ee.FeatureCollection([Trees, Shrub, Grass]).flatten();

print("regions", regions);

var S2 = ee.ImageCollection('COPERNICUS/S2')
  .filterBounds(Palo)
  .filterDate('2020-01-01','2020-01-31');

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

};

var S2 = S2.map(addNDVI);

print(S2);

var plotNDVI = ui.Chart.image.seriesByRegion(
  S2, 
  regions,
  ee.Reducer.mean(), 'ndvi', 30)
  .setChartType('LineChart')
  .setSeriesNames(['Trees', 'Shrubland','Grassland'])
  .setOptions({
    interpolateNulls: true,
    lineWidth: 1,
    pointSize: 3,
    title: 'NDVI annual evolution',
    hAxis: {title: 'Date'},
    vAxis: {title: 'NDVI'}
  });
  
print(plotNDVI);

After running above code in GEE code editor, I got result of following picture:

enter image description here

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.