0

i am trying to export a layer containing ndvi max value and the ndvi max date of each pixel. the visualization layer is good, but when it is exported to tif, i cant use it in GIS software (i use QGIS). the value is nan.

here is the snippet of my code

var geometry = 
    /* color: #d63000 */
    /* shown: false */
    /* displayProperties: [
      {
        "type": "rectangle"
      }
    ] */
    ee.Geometry.Polygon(
        [[[107.43022430567432, -6.146478462634124],
          [107.43022430567432, -6.164910996249429],
          [107.46043670801807, -6.164910996249429],
          [107.46043670801807, -6.146478462634124]]], null, false);
var S2 = ee.ImageCollection("COPERNICUS/S2_SR_HARMONIZED");
var kecamatan = 'Kutawaluya' //  kecamatan 
//ROI
var ROI = geometry
Map.centerObject(ROI, 12)
var addNDVI = function(image){
  var ndvi = image.normalizedDifference(['B8A', 'B4']).rename('ndvi')
  return image.addBands(ndvi)
}
var S2 = S2.map(addNDVI)
var S2 = S2.select('ndvi')
var MOD = S2
          .filterDate('2018-01-01','2018-12-31')
          .filterBounds(ROI)

// Define a list of start dates, end dates, and periods
var startDate = '2018-01-01'; // replace with your start date
var endDate = '2022-12-31'; // replace with your end date

var half_year_periods = [];

for (var year = parseInt(startDate.slice(0, 4)); year <= parseInt(endDate.slice(0, 4)); year++) {
  half_year_periods.push([year + '-04-01', year + '-09-30', 'dry_' + year]);
  half_year_periods.push([year + '-10-01', (year + 1) + '-03-31', 'rain_' + year]);
}
// loop over each half-year period
for (var i = 0; i < half_year_periods.length; i++) {
  var period = half_year_periods[i];
  var start = period[0];
  var end = period[1];
  var label = period[2];
  
  // Filter the Sentinel 2 image collection for the current period
  var MOD = S2
    .filterDate(start, end)
    .filterBounds(ROI)
  
  // Compute the maximum NDVI for the current period
  var addDate = function(image) {
    return image.addBands(ee.Image.constant(ee.Number.parse(image.date().format("YYYYMMdd"))).rename('time').float());
  };
  var NDVI = MOD.map(addDate)
  var array = NDVI.toArray()
  var axes = { image:0, band:1 }
  var sort = array.arraySlice(axes.band, 0, 1);
  var sorted = array.arraySort(sort);
  var length = sorted.arrayLength(axes.image)
  var values = sorted.arraySlice(axes.image, length.subtract(1), length);
  var max = values.arrayProject([axes.band]).arrayFlatten([['ndvi', 'time']])
  var ndviMax = max.select(0)
  var time = max.select(1)
    //print(time.bandTypes())
  var time_float=time.toFloat()
  time_float = time_float.setDefaultProjection(time_float.projection())
  print(time_float.bandTypes())
  // Add the NDVI max value and time layer to the map
  Map.addLayer(ndviMax.clip(ROI), {min: 0, max: 1}, 'NDVI max value - ' + period)
  Map.addLayer(time_float.clip(ROI).randomVisualizer(), {}, 'NDVI time for max - ' + period)
  // Export the NDVI max value and time layers as images
  Export.image.toDrive({
    image: ndviMax.clip(ROI),
    description: kecamatan + '_NDVI_max_' + label,
    scale: 100,
    region: ROI
  });

  Export.image.toDrive({
    image: time_float.clip(ROI),
    description: kecamatan + '_NDVI_time_' + label,
    scale: 100,
    region: ROI,
    crs: 'EPSG:4326'
  });
}
// Define a list to store the images
var image_list = []

// Loop over the list of start dates, end dates, and periods
for (var i = 0; i < half_year_periods.length; i++) {
  var start_date = half_year_periods[i][0]
  var end_date = half_year_periods[i][1]
  var period = half_year_periods[i][2]
  
  // Filter the Sentinel 2 image collection for the current period
  var MOD = S2
    .filterDate(start_date, end_date)
    .filterBounds(ROI)
  
  // Compute the maximum NDVI for the current period
  var addDate = function(image) {
    return image.addBands(ee.Image.constant(ee.Number.parse(image.date().format("YYYYMMdd"))).rename('time').float());
  };
  var NDVI = MOD.map(addDate)
  var array = NDVI.toArray()
  var axes = { image:0, band:1 }
  var sort = array.arraySlice(axes.band, 0, 1);
  var sorted = array.arraySort(sort);
  var length = sorted.arrayLength(axes.image)
  var values = sorted.arraySlice(axes.image, length.subtract(1), length);
  var max = values.arrayProject([axes.band]).arrayFlatten([['ndvi', 'time']])
  
  // Add the period name as a band to the image
  var period_band = ee.Image(0).byte().rename('period').set('period', period);
  var max_with_period = max.addBands(period_band);
  
  // Add the image to the list of images
  image_list.push(max_with_period);
  
  // Add the image to the list of images
  
  image_list.push(max_with_period);
  // Add the ndviMax and time image to the list
 // image_list.push(max)
}

when i open in qgis, the layer is image below [enter image description here](https://i.stack.imgur.com/XcYjO.png

could anyone help?

0 Answers0