1

Following up with my previous question Export raster with its min max range from GEE. I have tried the code below: 1.adapted from this answer Using computed min/max image values to set min/max visualization parameters in GEE Use the .evaluate() function to convert server-side objects to client-side objects. Include the computed min and max values in an ee.Dictionary object and applied the .evaluate() function to it. A client-side dictionary object (dict) is made available within the scope of the anonymous function, where the min and max values can then be referenced and set as visualization parameters.

var minMax = ee.Dictionary({
  minVal: stats.getNumber('NDVI_min'),
  maxVal: stats.getNumber('NDVI_max')
});

print(minMax);

minMax.evaluate(function(dict) {
  var vizParams = {
    min: dict.minVal, 
    max: dict.maxVal, 
    palette: ['green']
  };

Export.image.toDrive({
  image: maskImage.visualize(vizParams),
  description: '21072016UNDVI',
  scale: 10,
  region: table,
  fileFormat: 'GeoTIFF',
  formatOptions: {
    cloudOptimized: true
  }
});
});

It is not working. Then I tried this code: 2.adapted from this answer Any way to have variable min and max in one visualization parameter?.

var minMax = ee.Dictionary({
  minVal: stats.getNumber('NDVI_min'),
  maxVal: stats.getNumber('NDVI_max')
});

print(minMax);

minMax.evaluate(function(val) {
  var vizParams = {
    min: val.minVal, 
    max: val.maxVal, 
    palette: ['green']
  };
// Export a cloud-optimized GeoTIFF.
Export.image.toDrive({
  image: maskImage.visualize(vizParams),
  description: '21072016UNDVI',
  scale: 10,
  region: table,
  fileFormat: 'GeoTIFF',
  formatOptions: {
    cloudOptimized: true
  }
});
});

It is still not working.... Does it have something to do with the Nodata or null data?

1

You can use the visualize function in a server-side mapping over your image collection, no need to evaluate at all. Define your min and max value and use them as arguments in the visualize function.

var visualizeMinMaxNDVI = function(image, region) {
  var minMax = image.reduceRegion(ee.Reducer.minMax(), region, 10);
  var visParams = {
          min: minMax.getNumber('NDVI_min'),
          max: minMax.getNumber('NDVI_max'),
          };
  return image.visualize(visParams)
            .set({min: minMax.getNumber('NDVI_min'), max: minMax.getNumber('NDVI_max')});
};

See a full example in this link, where I included an example to export multiple images at once (as i think your are supposing to do);

| improve this answer | |
  • Hi @Kuik thanks for your code. I don't think I can export multiple images at once because I'm looking for images on certain dates (due to tide level cloud cover ect.). When I export the image I can't use image.visualize(vizParams) any more because vizParams is an argument in the function now (am I right?). So I don't know how to use visualizeMinMaxNDVI (or NDVIvis you used in the link) as visualization parameter when exporting the image layer.. – Rose Chai Jan 28 at 1:09
  • Here is the link of my code so far [link] (code.earthengine.google.com/…). I changed all "image,region" with "maskImage", which is the only raster layer I want. I also have another attempt which after importing into ArcGIS min change to 0 max change to 255...[link] (code.earthengine.google.com/…) – Rose Chai Jan 28 at 1:27
  • Just first use visualize to visualize all the images in the collection the way you want them. Then export the images you want to export. Use the "get link" button to share your code. I don't see any of your code atm. – Kuik Jan 28 at 2:41
  • Hi @Kuik sorry for the link. This is the one I adapted your code:code.earthengine.google.com/6c3ff50b93788ab92ab984b49a9e21de. – Rose Chai Jan 28 at 3:02
  • This is the one I adapted your code:code.earthengine.google.com/6c3ff50b93788ab92ab984b49a9e21de. Sorry it's very long I start your code from line 86. I got a bit confused when checking your link as you used "image and region". I changed it to "maskImage" since this is the only layer I'm interested in. I tried to add visualize parameter when exporting but since now the visParams is in a function I can't use it. Here is a different attempt:code.earthengine.google.com/96877c8f0afd56f78ce0f40571670e25. When I import this raster into ArcGIS it changed min to 0 and max to 255... – Rose Chai Jan 28 at 3:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.