ee.FeatureCollection flatten() computation timed out Google Earth Engine

1.9k views Asked by At

I am trying to extract water mask over a large collection of rapideye images using Google Earth Engine. I developed this script in the code editor of Google Earth Engine, which works perfectly for a small set of images. But when I try to increase the number of images to over 100, I get a message saying

Computation timed out when processing the ee.FeatureCollection.flatten() function, line 84 of the script.

Does anyone have any tip or solution to this problem?

Javascript Code:

//Convert image into ImageCollection
function loadImageCollection(folderPath){
var t= ee.data.getInfo(folderPath);
var d=ee.data.getList(t);
var inglst=[];
for (var index = 0; index < d.length; ++index) {
inglst.push(ee.Image(d[index].id));}
return ee.ImageCollection.fromImages(inglst);}

var rapideyetoa= loadImageCollection('users/danielassumpcaoferreira/Rapideye2');
print(rapideyetoa);

//This function calculates Indices
var allbynary = function(image) {
var NIR = image.select('b5').rename ("nir");
var SR1 = image.select('b5').divide(image.select('b2')).rename("sr1");
var SR2 = image.select('b5').divide(image.select('b3')).rename("sr2");
var NGI = image.select('b2').divide(image.select('b1').add(image.select('b3')).add(image.select('b4')).add(image.select('b5'))).rename("ngi");
var NDWI = image.select('b5').subtract(image.select('b2')).divide(image.select('b5').add(image.select('b2'))).rename("ndwi");

// Classify based on threshold
var region = image.geometry();
var classNIR = ee.Image(0).clip(region);
var binNIR = classNIR.where(NIR.lte(0.3),1).rename("nir");
var classSR1 = ee.Image(0).clip(region);
var binSR1 = classSR1.where(SR1.lte(1),1).rename("sr1");
var classSR2 = ee.Image(0).clip(region);
var binSR2 = classSR2.where(SR2.lte(1),1).rename("sr2");
var classNGI = ee.Image(0).clip(region);
var binNGI = classNGI.where(NGI.gte(0.24),1).rename("ngi");
var classNdwi = ee.Image(0).clip(region);
var binNDWI = classNdwi.where(NDWI.lte(0),1).rename("ndwi");
var myimage = binNIR.addBands(binSR1).addBands(binSR2).addBands(binNGI).addBands(binNDWI);
var wproduct = myimage.expression('(b("nir") * b("sr1") * b("sr2") * b("ngi") * b("ndwi") )').rename("wmask");
wproduct = wproduct.clip(region);
return wproduct};

//Morphological filter, opening followed by closing
var filtered = function(image) {
return image.focal_min(1,'square')
.focal_max(1,'square').focal_max(1,'square').focal_min(1,'square')};

//Map the functions (Indice calculation and Morphological filter) over the collection.
var allbinCollection = rapideyetoa.map(allbynary);
var filterCollection = allbinCollection.map(filtered);

//Define the visualization parameters for rapideye.
var vizParams = {bands: ['b3', 'b2', 'b1'],min: 0.0, max: 0.3,gamma: [1.5, 1, 1]};

//Images Visualization
Map.addLayer(rapideyetoa,vizParams, "Rapideye");
Map.addLayer(allbinCollection, {min:0, max:1}, "binary");
Map.addLayer(filterCollection, {min:0, max:1}, "filter");

print(allbinCollection, "Binary Collection");

//Reproject Images to an Equivalent Projection -
var reproject = function(image) { return image.reproject('PROJCS["South_America_Albers_Equal_Area_Conic",GEOGCS["GCS_South_American_1969",DATUM["South_American_Datum_1969",SPHEROID["GRS_1967_Truncated",6378160,298.25]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]],PROJECTION["Albers_Conic_Equal_Area"],PARAMETER["False_Easting",0],PARAMETER["False_Northing",0],PARAMETER["longitude_of_center",-60],PARAMETER["Standard_Parallel_1",-5],PARAMETER["Standard_Parallel_2",-42],PARAMETER["latitude_of_center",-32],UNIT["Meter",1],AUTHORITY["EPSG","102033"]]',null,5)};
var ReprjCollectionAlbers = filterCollection.map(reproject);
print(ReprjCollectionAlbers, "Filter Collection");

//Convert Raster to vector functions and Map over collection.
var transforvector = function(image) {
var rest = image.reduceToVectors(
{ geometry: image.geometry(),
scale : image.projection().nominalScale(),
crs : image.projection(),
geometryType: 'polygon',
maxPixels:1000000000,
bestEffort:true,
eightConnected: false,
});
return rest};

var watermask = ReprjCollectionAlbers.map(transforvector);

print(watermask, "watermask" );

//Flatten Image Collection
var FlattenCollection = watermask.flatten();

print(FlattenCollection, "watermask2" );

//Add area(ha) attribute to features
var addArea = function(feature) {return feature.set({areaHa: feature.geometry().area(ee.ErrorMargin(1)).divide(100 * 100)});};
var areaAdded = FlattenCollection.map(addArea);

//Exclude boundingBox and areas lower than 5 ha from feature
var Wmaskboundboxoff = areaAdded.filter(ee.Filter.eq('label',1));
var Wmask5ha = Wmaskboundboxoff.filter(ee.Filter.gte('areaHa',5));

print (Wmask5ha, "Wmask5ha");

Map.addLayer(Wmask5ha, {color:'#00BFFF'}, 'WaterMask');

//Convert vector do Geojason and export to googledrive
Export.table.toDrive ({collection: Wmask5ha,
description:'watermask',
fileFormat: 'GeoJSON'});
0

There are 0 answers