2

I am trying to compute a distance raster in GEE. I am looking for a tool similar to ArcMap’s Euclidean Distance.

I tried to use ee.Image.distance, however, I get an error related to the kernel.

Here is the the code:

// Country variable
var pais = 'Honduras';

// Collection adm1
var limites =ee.FeatureCollection("FAO/GAUL/2015/level1");

// Define ROI (Country)
var roi= limites.filter(ee.Filter.eq('ADM0_NAME', pais));

// Load GFC
var dataset = ee.Image('UMD/hansen/global_forest_change_2020_v1_8');
print (dataset);

Map.setCenter(-86.62,14.82, 8);
var treeLossVisParam = {
  bands: ['lossyear'],
  min: 0,
  max: 20,
  palette: ['yellow', 'red','purple']
};
Map.addLayer(dataset.clip(roi), treeLossVisParam, 'tree loss year');

// compute euclidian distance map from deforested pixels
var dist= dataset.select('lossyear').distance({kernel:null, skipMasked:false})
print('dist', dist)

and its link in GEE:

https://code.earthengine.google.com/5211d228ce97e647f9078eee7ad45ee2

This is the type of raster that I am looking for:

enter image description here

1 Answer 1

2

After running your code, I got this error message:

Image (Error)
Image.distance: Must specify a distance kernel.

So, I modified it as follows (by using an euclidean kernel distance equal to 50):

// Country variable
var pais = 'Honduras';

// Collection adm1
var limites =ee.FeatureCollection("FAO/GAUL/2015/level1");

// Define ROI (Country)
var roi= limites.filter(ee.Filter.eq('ADM0_NAME', pais));

// Load GFC
var dataset = ee.Image('UMD/hansen/global_forest_change_2020_v1_8');
print (dataset);

Map.setCenter(-86.62,14.82, 8);
var treeLossVisParam = {
  bands: ['lossyear'],
  min: 0,
  max: 20,
  palette: ['yellow', 'red','purple']
};
Map.addLayer(dataset.clip(roi), treeLossVisParam, 'tree loss year');

// compute euclidian distance map from deforested pixels
var dist= dataset.select('lossyear')
  .distance({kernel:ee.Kernel.euclidean(50), skipMasked:false})
  .rename('distance')
  .clip(roi);

print('dist', dist);

var imageVisParam = {"opacity":1,
                     "bands":["distance"],
                     "max":11.180339887498949,
                     "palette":["22ff20","1a35ff","ffa925","ff0a36","2fe1ff","fd4bff"]};

Map.addLayer(dist, imageVisParam, 'distance');

After running above code without any error, I got following distance map. You can try other kernel distances for a particular result (I tried out until 500, but this one is too high). I hope this helps.

enter image description here

2
  • Thank you @xunilk, that is exactly what I was looking for!
    – OscarBau
    May 31, 2021 at 13:44
  • You're welcome.
    – xunilk
    May 31, 2021 at 13:45

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.