0

I want to add the layer to map,

followed this solution,

but encountering the following error,

however I have converted the variable value to string, still, it is showing the error:

layer error: Element.get, argument 'property': Invalid type.
Expected type: String.
Actual type: Integer.
Actual value: 7

The code is at this link

var shapefile_to_select = Arvind_Punjab

// Function to cloud mask Landsat 8.
var CloudMaskOfLANDSAT8 = function(image) {
  var cloudShadowBitMask = ee.Number(2).pow(3).int();
  var cloudsBitMask = ee.Number(2).pow(4).int();
  var qa = image.select('QA_PIXEL');
  var mask = qa.bitwiseAnd(cloudShadowBitMask).eq(0).and(
            qa.bitwiseAnd(cloudsBitMask).eq(0));
  return image
      .select('SR_B.').multiply(0.0000275).add(-0.2)
      .updateMask(mask)
      .set('system:time_start', image.get('system:time_start'));
};

var startDate = '2021-01-01'
var endDate = '2021-01-31'

var raw_dataset = ee.ImageCollection('LANDSAT/LC08/C02/T1_L2')

var cloudmasked_dataset = raw_dataset
                          .filterDate(startDate, endDate)
                          .filterBounds(shapefile_to_select)  
                          .map(CloudMaskOfLANDSAT8)
                          .map(function(image){return image.clip(shapefile_to_select)});

var colour_Palette = ['#a50026','#d73027','#f46d43','#fdae61',
  '#fee08b','#d9ef8b','#a6d96a','#66bd63','#1a9850','#006837'];


var colList = cloudmasked_dataset.toList(cloudmasked_dataset.size());
var n = colList.size().getInfo();

for (var i = 0; i < n; i++) {
      
      var img = ee.Image(colList.get(i));
      var id = img.id().getInfo();
      var listOfImages = cloudmasked_dataset.toList(cloudmasked_dataset.size());
      var Tile = listOfImages.get(i);
      var allRasters = ee.ImageCollection.fromImages([Tile]);
      print("Raster no " + i, allRasters)
      
      var console_image = ee.Image(allRasters.get(i));
      Map.addLayer(console_image.select('SR_B2')
                             .clip(shapefile_to_select), 
                            {min:0, max:1,  
                            'palette': colour_Palette}, 
                            i.toString())
}

1 Answer 1

1

If your only intention is to add every Image to the map, these lines are not needed:

var allRasters = ee.ImageCollection.fromImages([Tile]);
print("Raster no " + i, allRasters)
var console_image = ee.Image(allRasters.get(i));

and you can just do:

Map.addLayer(Tile.select('SR_B2').clip(shapefile_to_select), 
             {min:0, max:1,'palette': colour_Palette}, 
             i.toString())

https://code.earthengine.google.com/a950b6832b27384e9cb88dd1cb3f4e25

If you need to keep the collection for another purpose you can filter the image using the id you get a few lines back, by doing:

var allRasters = ee.ImageCollection.fromImages([Tile]);
print("Raster no " + i, allRasters)
var console_image = ee.Image(allRasters.filter(ee.Filter.eq('system:index', id)).first());

https://code.earthengine.google.com/64d2047ea2eb2c9417cb9f60377e0810

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.