1

I need to calculate cost distance for each cell in my raster (bounding box: Sumatera island) to points (palm oil mills), with slope as its cost. The cost layer (slope) has float data type (val: 0-90), and I can't seem to get the right code to make the variable cost calculation works.

// Add features
var sumatera = ee.FeatureCollection("users/putraditama/sumatera"); // Sumatera island boundary (polygon)
var po_mills = ee.FeatureCollection("users/putraditama/po_mills"); // palm oil mills (points)

// Add raster
var elev = ee.Image("USGS/SRTMGL1_003");

// Clip elev to Sumatera boundary
var elevsmrt = elev.clip(sumatera);

// Get slope
var slopesmrt = ee.Terrain.slope(elevsmrt);

// Create a source image (palm oil mill=1, others=0)
var sources = ee.Image().toByte().paint(po_mills, 1);

// Mask the sources image with itself.
sources = sources.updateMask(sources);

// The cost data is generated from slope 
var cover = slopesmrt.select(0);

// Variable cost
var cost =
  cover.eq(0-10).or(cover.eq(11-20)).or(cover.eq(21-30))
      .multiply(1).add(
  cover.eq(31-40).or(cover.eq(41-50)).or(cover.eq(51-60))
      .multiply(2).add(
  cover.eq(61-70).or(cover.eq(71-80)).or(cover.eq(81-90))
      .multiply(3)));

// Compute the cumulative cost to traverse the land cover.
var cumulativeCost = cost.cumulativeCost({
  source: sources,
  maxDistance: 1000 * 1000 // 1000 kilometers
});
1

Found the solution. Changed the variable cost script into this:

var cost = 
  cover.gt(0).and(cover.lt(30))
      .multiply(1).add(
  cover.gt(31).and(cover.lt(60))
      .multiply(2).add(
  cover.gt(61).and(cover.lt(90))
      .multiply(3)));
  • Please mark this answer as the correct one using the grey/green tick to the left. – Kersten Dec 5 '17 at 8:13

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.