[GIS] Google Earth Engine: different stretch options

google-earth-enginesentinel-1

I'm working on a map view that detects new ice using Sentinel-1 together with Google Earth Engine.

When adding images to Google Earth Engine I can set the min and max values for my layer in numbers but then in the map view I have the possibility to change change these values and also select different stretch options.

visualization parameters

Is it possible to choose one of these options instead of entering a max and min view in the code? Right now my code end looks like this:

Map.addLayer(hh2017, {

bands: ["HH"],
max: -5,
min: -19,
opacity: 1,
palette: ['002bff','a4d5ff','979797','080358','000029' ]

}, 'sentinel-1');

I want it to be Stretch: 90% The reason I need to stretch the image each time is that they all look a bit different.

Best Answer

As described here, you may want to try a Styled Layer Descriptor. The previous answer also works, but requires you to use getInfo(), which can cause your browser to lock (see Client-Server doc for details). Here's another approach:

var i = ee.Image(ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA')
    .filterBounds(Map.getCenter())
    .sort('CLOUD_COVER')
    .first());
Map.centerObject(i);
Map.addLayer(i);

function setDisplay(dictionary) {
  var visParams = {
    bands: ['B4', 'B3', 'B2'], 
    min: [
      dictionary['B4_p5'], dictionary['B3_p5'], dictionary['B2_p5']
    ],
    max: [
      dictionary['B4_p95'], dictionary['B3_p95'], dictionary['B2_p95']
    ]
  };
  Map.layers().set(0, ui.Map.Layer(i, visParams));
}

Map.onChangeBounds(function() {
  var params = i.reduceRegion({
    reducer: ee.Reducer.percentile([5, 95]), 
    geometry: Map.getBounds(true), 
    scale: Map.getScale(),
  });
  params.evaluate(setDisplay);
});
Related Question