I have an MNDWI (Modified Normalised Difference Water Index) layer for an area of interest (aoi). I then identified water sources as pixels with MNDWI > 0 and created a water layer where a pixel either has a value of 0 (no water) or 1 (water). Now, I would like to calculate the distance from every 0 (no water) pixel to the nearest pixel with water (value = 1) and get an output raster where its values correspond to distances. Below, is my code for calculating MNDWI and the water layer.
First, get Sentinel imagery:
var S2 = ee.ImageCollection('COPERNICUS/S2')
.filterDate("2018-08-16", "2018-09-12")
.filterBounds(aoi);
var image = ee.Image(S2.sort('CLOUD_COVER', false).mosaic());
Then, calculate MNDWI and the water layer:
var image_mndwi = image.normalizedDifference(['B3','B8']);
var image_water = image_mndwi.gt(0)
Map.addLayer(image_water, {}, 'water')
Best Answer
I think you will need image.cumulativeCost, define every pixel with constant value 1 (so the 'cost' to travel through each pixel is similar to one pixel).
I tried something like this, but maybe a problem is that the answer is variable with zoom-level. You possibly will have to set a scale when you export the result.