使用​​fastDistanceTransform()​​的邻域操作 

对于某些卷积运算,​​fastDistanceTransform()​​​可能比​​reduceNeighborhood()​​​或更有效​​convolve()​​。例如,要对二进制输入进行腐蚀和/或膨胀:

var aw3d30 = ee.Image('JAXA/ALOS/AW3D30_V1_1');

// 从高程阈值制作一个简单的二元层。
var mask = aw3d30.select('AVE').gt(300);
Map.setCenter(-122.0703, 37.3872, 11);
Map.addLayer(mask, {}, 'mask');

// 以像素为单位的距离。
var distance = mask.fastDistanceTransform().sqrt();
// 膨胀的距离阈值(三个像素)
var dilation = distance.lt(3);
Map.addLayer(dilation, {}, 'dilation');

// 对mask进行相反的操作。
var notDistance = mask.not().fastDistanceTransform().sqrt();
var erosion = notDistance.gt(3);
Map.addLayer(erosion, {}, 'erosion');

这里来分别进行了两种操作,一个是向外膨胀,另外一个是向内侵蚀!

Google Earth Engine(GEE)——容易犯的错误5(fastDistanceTransform()、reduceNeighborhood() 和iterate())_google maps

 Google Earth Engine(GEE)——容易犯的错误5(fastDistanceTransform()、reduceNeighborhood() 和iterate())_GEE_02

Google Earth Engine(GEE)——容易犯的错误5(fastDistanceTransform()、reduceNeighborhood() 和iterate())_google maps_03

使用优化 ​​reduceNeighborhood()​​ 

如果您需要执行卷积并且不能使用​​fastDistanceTransform()​​​,请使用优化​​reduceNeighborhood()​​。

var l8raw = ee.ImageCollection('LANDSAT/LC08/C01/T1_RT');
var composite = ee.Algorithms.Landsat.simpleComposite(l8raw);

var bands = ['B4', 'B3', 'B2'];

var optimizedConvolution = composite.select(bands).reduceNeighborhood({
reducer: ee.Reducer.mean(),
kernel: ee.Kernel.square(3),
optimization: 'boxcar' // Suitable optimization for mean.
}).rename(bands);

var viz = {bands: bands, min: 0, max: 72};
Map.setCenter(-122.0703, 37.3872, 11);
Map.addLayer(composite, viz, 'composite');
Map.addLayer(optimizedConvolution, viz, 'optimizedConvolution');

Google Earth Engine(GEE)——容易犯的错误5(fastDistanceTransform()、reduceNeighborhood() 和iterate())_google earth_04

Google Earth Engine(GEE)——容易犯的错误5(fastDistanceTransform()、reduceNeighborhood() 和iterate())_google earth_05

 这副影像加载速度明显比上面的更快,因为领域内它选择进行了内核的合并

及时对neighbor使用前向差分 

假设您有一个时间排序​​ImageCollection​​​(即时间序列),并且您想将每个图像与前一个(或下一个)图像进行比较。与其​​iterate()​​为此目的而使用 ,使用基于数组的前向差分可能更有效。以下示例使用此方法对 Sentinel-2 集合进行重复数据删除,其中重复项定义为一年中同一天的图像:

var sentinel2 = ee.ImageCollection('COPERNICUS/S2');
var sf = ee.Geometry.Point([-122.47555371521855, 37.76884708376152]);
var s2 = sentinel2
.filterBounds(sf)
.filterDate('2018-01-01', '2019-12-31');

var withDoys = s2.map(function(image) {
var ndvi = image.normalizedDifference(['B4', 'B8']).rename('ndvi');
var date = image.date();
var doy = date.getRelative('day', 'year');
var time = image.metadata('system:time_start');
var doyImage = ee.Image(doy)
.rename('doy')
.int();
return ndvi.addBands(doyImage).addBands(time)
.clip(image.geometry()); // Appropriate use of clip.
});

var array = withDoys.toArray();
var timeAxis = 0;
var bandAxis = 1;

var dedupe = function(array) {
var time = array.arraySlice(bandAxis, -1);
var sorted = array.arraySort(time);
var doy = sorted.arraySlice(bandAxis, -2, -1);
var left = doy.arraySlice(timeAxis, 1);
var right = doy.arraySlice(timeAxis, 0, -1);
var mask = ee.Image(ee.Array([[1]]))
.arrayCat(left.neq(right), timeAxis);
return array.arrayMask(mask);
};

var deduped = dedupe(array);

// 检查这些输出以确认已删除重复项。
print(array.reduceRegion('first', sf, 10));
print(deduped.reduceRegion('first', sf, 10));

Google Earth Engine(GEE)——容易犯的错误5(fastDistanceTransform()、reduceNeighborhood() 和iterate())_google maps_06

重复项减少了3个