7

I need to reclassify slope raster (val: 0-90) to 9 classes (1-9). I used this code but the output raster cannot be displayed. What did I do wrong?

// Add features
var sumatera = ee.FeatureCollection("users/putraditama/sumatera");

// 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);

// Remap values
var slopereclass = slopesmrt
.remap([0-10,11-20,21-30,31-40,41-50,51-60,61-70,71-80,81-90],
       [1,2,3,4,5,6,7,8,9]);

// Display the result.
Map.addLayer(slopesmrt, {min: 0, max :90, palette: ['black', 'white']},'slope');
Map.addLayer(slopereclass, {min: 1, max :9}, 'slopereclass');

2 Answers 2

12

The accepted solution addresses the very specific case at hand, and while admirably clever (kudos for that), does not answer the question, which is "How to reclass ranges in GEE".

A more general case of reclassification of ranges into user specific classes would be with conditional assignment using where

// Add features.
var feature = ee.FeatureCollection("USDOS/LSIB_SIMPLE/2017")
             .filter(ee.Filter.eq('country_na', 'Switzerland'));

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

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

// Remap values.
var slopereclass = ee.Image(1)
      .where(slopesmrt.gt(10).and(slopesmrt.lte(20)), 1)
      .where(slopesmrt.gt(20).and(slopesmrt.lte(30)), 2)
      .where(slopesmrt.gt(30).and(slopesmrt.lte(40)), 3)
      .where(slopesmrt.gt(40).and(slopesmrt.lte(50)), 4)
      .where(slopesmrt.gt(50).and(slopesmrt.lte(90)), 5)

// create colors for those classifications
var visparams = {
   "opacity": 1,
    "min": 1,
    "max": 5,
    "palette": ["147218", "7cb815", "f2fe2a", "ffac18", "fe3c19"]
}

Map.addLayer(slopereclass, visparams)

...and so on. Clearly, more to write but now I could specify any class range I want.

reclassified image in earth engine

1
  • This code seems quite dangerous in the general case as omitted values (here 0 to 10) will still be attributed a 1, without the user being aware of it... Basically, ee.Image(1) acts as the "default" value. I think it would be safe to change the default value in ee.Image(1), say ee.Image(10), then make sure to mask it out, so that user sees omitted categories?
    – Matifou
    Jun 13 at 14:06
10

ee.Image.remap() operates on individual values, not ranges of numbers like your example shows.

Given that you are trying to convert the slope values to a set of bins with equal spacing, you can just divide and set it next highest integer using ee.Image.ceil().

// Add features.
var feature = ee.FeatureCollection("USDOS/LSIB_SIMPLE/2017")
             .filter(ee.Filter.eq('country_na', 'Switzerland'));

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

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

// Remap values.
var slopereclass = slopesmrt.divide(10).ceil();

// Display the result.
Map.addLayer(slopesmrt.clip(feature), {min: 0, max: 90, palette: ['black', 'white']},'slope');
Map.addLayer(slopereclass.clip(feature), {min: 1, max: 9, palette: ['black', 'red']}, 'slopereclass');
0

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.