The current implementation in gee.js uses:
reduceToVectorsto vectorize forest fragmentsee.Join.saveBestwithwithinDistancefor spatial join to find nearest neighbors
This is computationally expensive because:
| Issue | Impact |
|---|---|
| Vectorization overhead | reduceToVectors on large areas creates massive feature collections |
| Spatial join complexity | ee.Join.saveBest with withinDistance is O(n²) worst case |
| Per-tile processing | Even with grid subdivision, each tile requires expensive vectorization |
Instead of vectorizing and computing pairwise vector distances, use raster distance operations:
// For each patch, compute distance to nearest "edge" of any OTHER patch
// This avoids vectorization entirely
var forestMask = native_i.gt(0); // Binary: is forest
// Compute Euclidean distance to nearest forest pixel
var distToForest = forestMask.not().cumulativeCost({
source: forestMask,
maxDistance: searchRadius
});
// OR simpler approach using fastDistanceTransform
var distToForest = forestMask.fastDistanceTransform().sqrt().multiply(30); // in metersThe challenge is distinguishing distance to same patch vs other patches. One workaround:
// Dilate each patch and find where they "touch"
// Edge pixels of each fragment
var edges = forestMask.focal_min(1).subtract(forestMask).eq(-1);
// Distance from edge to nearest edge
var ennRaster = forestMask.cumulativeCost({source: edges, maxDistance: searchRadius});// Extract fragment boundaries
var fragmentBoundaries = native_i.reduceNeighborhood({
reducer: ee.Reducer.countDistinct(),
kernel: ee.Kernel.square(1)
}).gt(1).selfMask();
// Distance from each fragment to nearest boundary of different fragment
var enn = fragmentBoundaries.cumulativeCost({
source: fragmentBoundaries,
maxDistance: searchRadius
}).clip(grid_i);If exact per-pixel ENN isn't required:
- Compute ENN at coarser resolution (e.g., 250m)
- Interpolate results back to 30m
- Is per-pixel ENN required, or would per-fragment (single distance value per fragment) suffice?
- What's the typical fragment size and count per tile?
- Are you computing edge-to-edge distance or centroid-to-centroid?
- What's the acceptable error margin for distance?
The distance transform approach could potentially eliminate vectorization entirely and run much faster on GEE's parallel raster infrastructure. Prototype this first before exploring hybrid approaches.