I have a code that calculates NDVI for an imageCollection and then is supposed to display those rasters. The rasters are filtered by their size so only rasters that have more than a certain amount of pixels are being added to a new imageCollection and then displayed.
The problem is that the images cannot be displayed and I get the next error message:
Layer error: Element.get, argument 'property': Invalid type. Expected:
String. Actual: Integer
The graphs are displayed but the images are not and I don't understand why would the images need to be string, when did they changed and how to change them back to integer.
This is my code:
* Function to mask clouds based on the pixel_qa band of Landsat SR data.
* @param {ee.Image} image Input Landsat SR image
* @return {ee.Image} Cloudmasked Landsat image
*/
var cloudMaskL457 = function(image) {
var qa = image.select('pixel_qa');
// If the cloud bit (5) is set and the cloud confidence (7) is high
// or the cloud shadow bit is set (3), then it's a bad pixel.
var cloud = qa.bitwiseAnd(1 << 5)
.and(qa.bitwiseAnd(1 << 7))
.or(qa.bitwiseAnd(1 << 3));
// Remove edge pixels that don't occur in all bands
var mask2 = image.mask().reduce(ee.Reducer.min());
return image.updateMask(cloud.not()).updateMask(mask2).divide(10000)
.copyProperties(image, ['system:time_start']);
};
//Create LANDSAT7 dataset
var dataset = ee.ImageCollection('LANDSAT/LE07/C01/T1_SR')
.filterDate('1999-01-01', '2013-04-29')
.select('B1','B2','B3','B4','pixel_qa')
.filterBounds(geometry)
.map(cloudMaskL457);
//RGB visualization
var visParams = {
bands: ['B3', 'B2', 'B1'],
min: 0,
max: 1,
gamma: 1.4,
};
//clip the dataset according to the geometry
var clippedCol=dataset.map(function(im){
return im.clip(geometry);
});
// Get the number of images.
var count = dataset.size();
print('Count: ',count);
//print(clippedCol);
print(dataset,'dataset');
//function to calculate NDWI in LANDSAT7
var addNDVI = function(image) {
var NDVI = image.normalizedDifference(['B4', 'B3'])
.rename('NDVI')
.copyProperties(image,['system:time_start']);
return image.addBands(NDVI);
};
//NDWI to the clipped image collection
var withNDVI = clippedCol.map(addNDVI).select('NDVI');
var NDVIcolor = {
min: -1,
max:1,
palette: ['FFFFFF', 'CE7E45', 'DF923D', 'F1B555', 'FCD163', '99B718', '74A901',
'66A000', '529400', '3E8601', '207401', '056201', '004C00', '023B01',
'012E01', '011D01', '011301'],
};
var ndviWithCount = withNDVI.map(function(image){
var countpixels = ee.Number(image.reduceRegion({
reducer: ee.Reducer.count(),
geometry: geometry,
crs: 'EPSG:4326',
scale: 30,
}).get('NDVI'));
return image.set('count', countpixels);
});
print(ndviWithCount, 'ndviWithCount');
//filter between a range
var filter = ndviWithCount.filter(ee.Filter.rangeContains(
'count', 220000, 446224))
print(filter, 'filtered');
var max = ndviWithCount.reduceColumns(ee.Reducer.max(), ["count"])
print(max.get('max'));
var OrderList=[0,1,2,3,4,5,6,7,8,9,10]
for (var i in OrderList) {
var image = ee.Image(filter.get(OrderList[i]));
var toexport=image.visualize(NDVIcolor).addBands(image);
// do what ever you need with image
Map.addLayer(image, NDVIcolor, i);
}
print(ui.Chart.image.series(filter, geometry, ee.Reducer.mean(), 30));
print(ui.Chart.image.series(withNDVI, geometry, ee.Reducer.max(), 30));
print(ui.Chart.image.series(withNDVI, geometry, ee.Reducer.min(), 30));
print(ui.Chart.image.series(withNDVI, geometry, ee.Reducer.stdDev(), 30));
Best Answer
Convert the
filter
ImageCollection to a List of images.When the
get
method is applied to an ImageCollection it expects the name (string) of a property to get; when it is applied to a List, it expects an integer index value and returns the respective image from the List.filter = filter.toList(filter.size()); // convert ImageCollection to List
See line 97: https://code.earthengine.google.com/e4f2efb32d16a45e918a5ff60ab78b03
Note, however, that converting collections to lists can negatively affect performance. Avoid using
.toList()
if you can use alternative methods. For instance, use.filter()
to select and image of interest.