I have retrieved both Landsat (4–9) and Sentinel 2 data. Right away I assign to all the images a property called source. This source states LANDSAT_X (with X being 4, 5, 7,8 or 9) or SENTINEL_2.
After some processing I select the least cloudy image that meets the set criteria. This image is called firstImage. As you can see I added quite a few print statements in the function to assess whether the source is detected right. Every time the source is printed as should.
However, when calling the function the image is not actually added to the map. It seems that the if statements are not responding. I need the if statement because the LANDSAT and SENTINEL images have different bands and need different min/max values for their visualization.
The code should be able to work for both image, either coming from LANDSAT or Sentinel, as we do not know which one is providing the best image beforehand.
The if statement is not reading the source.. How to overcome? I have stated the code below.
// Function to add image to map for Landsat and Sentinel images
var addImageToMap = function(image) {
// Get the 'source' from the image to determine if it's Landsat or Sentinel
var source = image.get('source');
print('source value:', source);
// Function to calculate the min and max for the bands
var getMinMax = function(image, bands) {
return image.select(bands).reduceRegion({
reducer: ee.Reducer.minMax(),
geometry: image.geometry(),
scale: 30, // Set appropriate scale depending on dataset
maxPixels: 1e8
});
};
// For Landsat
if (source.getInfo().slice(0,6) === "LANDSAT") {
print('Landsat image detected');
var bands = ['SR_B4', 'SR_B3', 'SR_B2']; // True color bands for Landsat
var stats = getMinMax(image, bands);
// Retrieve the min and max values for the SR_B4 band (red band)
var minVal = stats.get('SR_B4_min');
var maxVal = stats.get('SR_B4_max');
// Check if the min/max values are valid, otherwise set defaults
minVal = minVal !== null ? minVal : 4000;
maxVal = maxVal !== null ? maxVal : 20000;
// Add the image to the map if the min/max values are valid
Map.addLayer(image, {bands: bands, min: minVal, max: maxVal}, 'Landsat True Color');
}
// For Sentinel-2
else if (source.getInfo().slice(0,7) === 'SENTINEL') {
var bands = ['B4', 'B3', 'B2']; // True color bands for Sentinel
var stats = getMinMax(image, bands);
// Retrieve the min and max values for the B4 band (red band)
var minVal = stats.get('B4_min');
var maxVal = stats.get('B4_max');
// Check if the min/max values are valid, otherwise set defaults
minVal = minVal !== null ? minVal : 0;
maxVal = maxVal !== null ? maxVal : 3000;
// Add the image to the map if the min/max values are valid
Map.addLayer(image, {bands: bands, min: minVal, max: maxVal}, 'Sentinel True Color');
}
};
// Example image: firstImage is already defined, assuming it is a valid image object
print('Properties of the first image (Low Clouds):');
print('Cloud Cover [%]: ', firstImage.get('cloud_cover'));
// Call addImageToMap function to actually add the image to the map
addImageToMap(firstImage);
current output:
source value: LANDSAT_5 and no image
I have tried many types of if statements but they did not seem to work