I am very new to Earth Engine and Javascript.

I'm trying to get the cumulative cost distance between some areas of interest and some tide gauges in the coastal U.S. To do this, I first calculated the pixel cost based on a national elevation map. Then, I calculated the cumulative cost using to the built-in function cumulativeCost. That went all pretty well. Now I'm trying to extract the cumulative cost values at the position of the tide gauges. To do this, someone suggested me to use the reduceRegions method. I've tried the following code but unsuccessfully.

I'm going to post my whole code so that is replicable. Please note that the part with which I have a problem is the second one.

var imageCollection =    
sourceVis = {"opacity":1,"bands":["b1"],"min":1,"max":1,"gamma":1},
DEMVis = {"opacity":1,"bands":
imageVisParam = {"opacity":1,"bands":
imageVisParam2 = {"opacity":1,"bands":
cosVisParam = {"opacity":1,"bands":
imageVisParam3 = {"opacity":1,"bands":

//get IDs for images in image collection 
var getID = function(image){ return image.set('ID', image.id());};
var okID = imageCollection.map(function(image) { return image.set('ID', 

// Set general estethic parameters 
var dem_vis = {bands:"elevation", min:0, max:0.05,         
var cost_vis = {bands:"cumulative_cost", min:0, max:10000,     

// PART 1: Cumulative Cost based on source rasters
 //import elevation map 
var dem = ee.Image('USGS/NED');

// pixel cost calculation 
var elThreshold = ee.Number(5); //set elevation threshold
var subDEM = dem.updateMask(dem.lt(elThreshold)); //mask pixel above 
elevation threshold
var costDEM = (subDEM.add(30)).divide(1000); //calculate the cost of each 
pixel (height + width pixel (30m)) in km 

// Add DEM to the map
Map.addLayer(costDEM, dem_vis, "SRTM");

// Cumulative cost
var calcCumCost = function(img) {
return costDEM.cumulativeCost({
}; //write a function that perform the cumulative cost calculation for each 
image given the cost of the pixel 

var demCost = ee.ImageCollection(okID.map(calcCumCost)); // caulcuate     
cumulative cost for each source raster in the image collection 

// PART 2 - Reduce Region: extract cumulative cost for tide gauges 

var tideGauges = 
//import fusion table of tide gauges

// Empty Collection to fill
var ft = ee.FeatureCollection(ee.List([]));

// function that extract values from cumulative cost rasters and reduce it for points region
var fill = function(img) {
 // gets the values for the points in the current img
var ft2 = img.reduceRegions(tideGauges, ee.Reducer.first(),30);
// set ID
var ID = ee.Feature(null, {'id':img.id()});

// writes the ID in each feature
 var ft3 = ft2.map(function(f){return f.set("id", ID)});
// merges the FeatureCollections
return ft.merge(ft3);

// Apply the function to each image in the ImageCollection
var newft = ee.FeatureCollection(demCost.map(fill));
print(newft, 'Potentially: region-reduced cost');
  • Have you tried exporting newft instead of trying to display it directly? – Kersten Nov 6 '17 at 16:12

Exporting is definitely the right choice, as described here. Also, for memory exceeded errors, try cranking the tileScale to 8 or 16 in the reduceRegion() call (reference).

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.