I need to find the nearest distance between every pixel in my area of interest to the streamline closest to it. I'm using the following dataset for the streamlines ee.FeatureCollection("WWF/HydroSHEDS/v1/FreeFlowingRivers").

Initially, I thought image.cumulativeCost() would do the trick, but it seems like increasing the maxDistance only increases the buffer around the streamlines. The resulting value of cumulative_cost is 0 when the area is within the 1km buffer of the streamlines, and 'masked' otherwise.

What are the values of the 'masked' pixels? Is it even feasible to achieve what I need using image.cumulativeCost()?

var streams = ee.FeatureCollection("WWF/HydroSHEDS/v1/FreeFlowingRivers");
var roi = ee.Geometry.Polygon(
        [[[31.79857587268242, 40.34237834590226],
          [31.79857587268242, 39.1087726665225],
          [35.77013348986992, 39.1087726665225],
          [35.77013348986992, 40.34237834590226]]], null, false);

//polylines representing river networks
var filteredstreams = streams.filterBounds(roi);

var streamImg = filteredstreams
    properties: ['LENGTH_KM'],    //picked a random property here just to convert the FeatureCollection
    reducer: ee.Reducer.first()   //into an image

//create a source image where the geometry is 1, everything else is 0
var streamsources = ee.Image().toByte().paint(roi, 1);
streamsources = streamsources.updateMask(streamImg);
var emptysource = ee.Image().toByte().paint(roi, 0);

//define distance
var maximumdist = 1000; //1km

//cost image
var thedist = emptysource.cumulativeCost({
  source: streamsources , 
  maxDistance: maximumdist,  

1 Answer 1


You can compute the distance using fastDistanceTransform(). See also here:

// calculate the distance from the strem (in pixels)
var thedist = streamImg.fastDistanceTransform().sqrt().multiply(ee.Image.pixelArea().sqrt());

See link

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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