mercoledì 11 febbraio 2026

Mappa NDVI medio annuale con Google Earth Engine

Al contrario del precedente post https://debiaonoldcomputers.blogspot.com/2026/02/analisi-multitemporale-ndvi-con-google.html qui l'idea e' di creare una mappa di NDVI al posto della serie tempo per vedere anomalie spaziali

 


 

Landsat 8 

 // 1. Define the Study Area

var areaOfInterest = 
    /* color: #d63000 */
    /* displayProperties: [
      {
        "type": "rectangle"
      }
    ] */
    ee.Geometry.Polygon(
        [[[10.904865172222156, 43.01615135702198],
          [10.904865172222156, 43.00648615894694],
          [10.925807860210437, 43.00648615894694],
          [10.925807860210437, 43.01615135702198]]], null, false);

// 2. Pre-processing Function for Landsat 8
var processL8 = function(image) {
  var opticalBands = image.select('SR_B.').multiply(0.0000275).add(-0.2);
  var ndvi = opticalBands.normalizedDifference(['SR_B5', 'SR_B4']).rename('NDVI');
  return image.addBands(ndvi).select('NDVI');
};

// 3. Load 2025 Median NDVI
var ndvi2025 = ee.ImageCollection("LANDSAT/LC08/C02/T1_L2")
  .filterBounds(areaOfInterest)
  .filterDate('2025-01-01', '2025-12-31')
  .map(processL8)
  .median()
  .clip(areaOfInterest);

// 4. Visualization Parameters
var palette = [
  '#FFFFFF', '#CE7E45', '#FCD163', '#66A000', '#207401', '#056201', '#011301'
];
var ndviViz = {min: 0, max: 0.9, palette: palette};

// 5. Display the Map
Map.addLayer(ndvi2025, ndviViz, 'NDVI Median 2025');

// --- 6. ADDING THE LEGEND ---

// Create the legend panel
var legend = ui.Panel({
  style: {
    position: 'bottom-left',
    padding: '8px 15px'
  }
});

// Create legend title
var legendTitle = ui.Label({
  value: 'NDVI Index (2025)',
  style: {fontWeight: 'bold', fontSize: '16px', margin: '0 0 4px 0', padding: '0'}
});
legend.add(legendTitle);

// Helper function to create rows in the legend
var makeRow = function(color, name) {
  var colorBox = ui.Label({
    style: {
      backgroundColor: color,
      padding: '8px',
      margin: '0 0 4px 0'
    }
  });
  var description = ui.Label({
    value: name,
    style: {margin: '0 0 4px 6px'}
  });
  return ui.Panel({
    widgets: [colorBox, description],
    layout: ui.Panel.Layout.Flow('horizontal')
  });
};

// Define labels for the palette colors
var names = ['Low/No Veg (< 0.1)', 'Soil / Sparse', 'Moderate Veg', 'Dense Veg', 'Healthy Forest', 'Peak Vigour (> 0.8)'];
var colors = ['#FFFFFF', '#CE7E45', '#FCD163', '#66A000', '#207401', '#011301'];

// Add rows to legend
for (var i = 0; i < 6; i++) {
  legend.add(makeRow(colors[i], names[i]));
}

// Add legend to the map
Map.add(legend);

Export.image.toDrive({
  image: ndvi2025,            // The variable name of your NDVI map
  description: 'NDVI_2025_Tuscany', 
  folder: 'EarthEngine_Exports', // Name of the folder in your Google Drive
  fileNamePrefix: 'NDVI_2025_Site',
  region: areaOfInterest,     // Export only the area inside your polygon
  scale: 30,                  // Landsat resolution is 30 meters
  crs: 'EPSG:4326',           // Standard Coordinate Reference System (WGS84)
  fileFormat: 'GeoTIFF',
  formatOptions: {
    cloudOptimized: true      // Makes the file easier to open in QGIS/ArcGIS
  }
});

 

Landsat 7

// 1. Define the Study Area
var areaOfInterest = 
    /* color: #d63000 */
    /* displayProperties: [
      {
        "type": "rectangle"
      }
    ] */
    ee.Geometry.Polygon(
        [[[10.904865172222156, 43.01615135702198],
          [10.904865172222156, 43.00648615894694],
          [10.925807860210437, 43.00648615894694],
          [10.925807860210437, 43.01615135702198]]], null, false);
// 2. Pre-processing Function for Landsat 8
var processL8 = function(image) {
  var opticalBands = image.select('SR_B.').multiply(0.0000275).add(-0.2);
  var ndvi = opticalBands.normalizedDifference(['SR_B5', 'SR_B4']).rename('NDVI');
  return image.addBands(ndvi).select('NDVI');
};

// 3. Load 2025 Median NDVI
var l7_2005 = ee.ImageCollection("LANDSAT/LE07/C02/T1_L2")
  .filterBounds(areaOfInterest)
  .filterDate('2005-01-01', '2005-12-31')
  .map(function(image) {
    var opticalBands = image.select('SR_B.').multiply(0.0000275).add(-0.2);
    // Landsat 7 NDVI: (B4 - B3) / (B4 + B3)
    var ndvi = opticalBands.normalizedDifference(['SR_B4', 'SR_B3']).rename('NDVI');
    return image.addBands(ndvi).select('NDVI');
  })
  .median() // Create a clean composite for the year
  .clip(areaOfInterest);

// 4. Visualization Parameters
var palette = [
  '#FFFFFF', '#CE7E45', '#FCD163', '#66A000', '#207401', '#056201', '#011301'
];
var ndviViz = {min: 0, max: 0.9, palette: palette};

// 5. Display the Map
Map.addLayer(l7_2005, ndviViz, 'NDVI Median 2005');

// --- 6. ADDING THE LEGEND ---

// Create the legend panel
var legend = ui.Panel({
  style: {
    position: 'bottom-left',
    padding: '8px 15px'
  }
});

// Create legend title
var legendTitle = ui.Label({
  value: 'NDVI Index (2025)',
  style: {fontWeight: 'bold', fontSize: '16px', margin: '0 0 4px 0', padding: '0'}
});
legend.add(legendTitle);

// Helper function to create rows in the legend
var makeRow = function(color, name) {
  var colorBox = ui.Label({
    style: {
      backgroundColor: color,
      padding: '8px',
      margin: '0 0 4px 0'
    }
  });
  var description = ui.Label({
    value: name,
    style: {margin: '0 0 4px 6px'}
  });
  return ui.Panel({
    widgets: [colorBox, description],
    layout: ui.Panel.Layout.Flow('horizontal')
  });
};

// Define labels for the palette colors
var names = ['Low/No Veg (< 0.1)', 'Soil / Sparse', 'Moderate Veg', 'Dense Veg', 'Healthy Forest', 'Peak Vigour (> 0.8)'];
var colors = ['#FFFFFF', '#CE7E45', '#FCD163', '#66A000', '#207401', '#011301'];

// Add rows to legend
for (var i = 0; i < 6; i++) {
  legend.add(makeRow(colors[i], names[i]));
}

// Add legend to the map
Map.add(legend);

Export.image.toDrive({
  image: l7_2005,                // Updated to the 2005 variable
  description: 'NDVI_L7_2005_Tuscany', 
  folder: 'EarthEngine_Exports', 
  fileNamePrefix: 'NDVI_Landsat7_2005',
  region: areaOfInterest,     
  scale: 30,                  
  crs: 'EPSG:4326',           
  fileFormat: 'GeoTIFF',
  formatOptions: {
    cloudOptimized: true      
  }
});


 

 

Nessun commento:

Posta un commento

Mappa NDVI medio annuale con Google Earth Engine

Al contrario del precedente post https://debiaonoldcomputers.blogspot.com/2026/02/analisi-multitemporale-ndvi-con-google.html  qui l'ide...