1

I have Sample RGB combination from 3 different Bands

RGB Imagery composite from 3 diferent band combination

// Input RGB imagery
var image = ee.Image("users/k2yulianto/Exercise/Ras/Imagery");
// Use Tree bands from RGB Imagery
var bands = ['b1', 'b2', 'b3'];
var b1 = ['b1'];
var b2 = ['b2'];
var b3 = ['b3'];
// Display the inputs imagery
Map.addLayer (image, {bands: ['b1', 'b2', 'b3'], max: 1, gamma:1, opacity:0.80} , 'RGB imagery',true);
Map.setCenter (101.4476, 0.4925, 12);

Sampling point distribution

And I have also sampling point distribution on top of RGB Imagery.

// Sampling point on top of RGB imagery to identify 4 diferent class
var Sampling = ee.FeatureCollection("users/k2yulianto/Exercise/Sampling/Sampling");
Map.addLayer (Sampling, {opacity:0.80},'Sampling point', true);

Based on the sampling point, I am to produce decision tree using WEKA to distinguish the type of class on RGB imagery.

WEKA Decision tree

//var exercise = ee.Algorithms.if(condition,truecase,falsecase)
var exercise = ee.Algorithms.If(b2 <= (0),(ee.Algorithms.If(b3 <= (0),"Sample 1","Sample 2")),(ee.Algorithms.If(b1 <= (0),"Sample 3","Sample 4")));
print('Imagery output: ',exercise);

I want to display if-then-else condition into an image classification.

// Display the 4 Class exercise output imagery
var RGBPalette = 
['#5f932c', // Class 01
'#bdff09', // Class 02 
'#fdf613', // Class 03 
'#abfffb', // Class 04
];
Map.addLayer(exercise,{min: 0, max: 4, palette: RGBPalette}, 'Exercise imagery');

After I run the scripts, I got an error warning:

Cannot add an object of type to the map.

Here is my full code:

https://code.earthengine.google.com/77b2238d1a628c59dfa440a27adfa378

How can I solve this?

3
  • 1
    Why have you tagged this with arcgis-desktop?
    – Vince
    Commented Mar 14, 2020 at 14:07
  • In arcgis destop i am already solved this problem trought raster calculator (if than else), i write it in Google Earth engine similar formula, but not work. Commented Mar 15, 2020 at 2:38
  • This question has nothing to do with ArcGIS, and therefore shouldn't be tagged with it.
    – Vince
    Commented Mar 15, 2020 at 4:18

1 Answer 1

5

The function you are using to solve the problem (ee.Algorithms.If) is for using an if statement inside a GEE loop (map or iterate), but not for this case.

For this, there are a couple of ways to do it, but I think the easier would be using the expression function:

var exercise = image.expression('b(1)<=0 ? (b(2)<=0 ? 1 : 2) : (b(0)<=0 ? 3 : 4)').rename('class')

Inside parenthesis you can use the number of the band, like in this example b(1) is band b2, or you could use the name b("b2") (https://developers.google.com/earth-engine/guides/image_math)

https://code.earthengine.google.com/708bfe3d64299bc0982390f0047be9ad

4
  • i have one more question. in class 4 have same classify with "no Data" . can i have other class as no data (except class 1-4) become transparent (or if "no data" will not be classify?). .. Thanks for your help Commented Mar 26, 2020 at 5:19
  • 1
    Add this line exercise = exercise.updateMask(image.select(0).mask()). If the answer is what you were looking for you can mark it as correct. Thanks Commented Mar 27, 2020 at 12:28
  • 1
    Thanks for your help Rodrigo E. Principle .... Now work and is correct.. ready to applied in the real complex's decision tree algorithms Commented Apr 15, 2020 at 11:58
  • 1
    @KokokYulianto Please consider clicking the green accept checkmark next to whichever answer to each of your questions helps you the most. That way you both acknowledge and reward the efforts of our answerers who volunteer their time here.
    – PolyGeo
    Commented Sep 30, 2021 at 7:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.