1

I have a classification that goes from 1985 to 2020. I would like to reclassify them all to values ​​of 0 and 1, that is, vegetation and not vegetation. I tried using the .remap() function, but I must specify a band and it only works for one.

var asset = "projects/mapbiomas-workspace/public/collection6/mapbiomas_collection60_integration_v1";
var mapbiomas = ee.Image(asset).selfMask();

var or = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30]
var new = [1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]

var mapbiomas_1985 = mapbiomas.remap({
 from: or,
 to: new,
 defaultValue: 0,
 bandName: 'classification_1985'
});

1 Answer 1

1

You need to map over the band names using remap function. For this purpose, use the same scheme for writting a new function

var asset = "projects/mapbiomas-workspace/public/collection6/mapbiomas_collection60_integration_v1";
var mapbiomas = ee.Image(asset).selfMask();

var oldvalues = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30];
var newvalues = [1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];

var bnames = mapbiomas.bandNames();

var custom_fun = function(band){
  return mapbiomas.remap({
    from: oldvalues,
    to: newvalues,
    defaultValue: 0,
    bandName: band
  }).rename([band]);
};

var vegetation = bnames.map(custom_fun);

This function takes the band name while mapping. Then, if you want to obtain a ee.Image with the same band names, use this useful approach from Image collection to Single Image in Google Earth Engine

// from https://gis.stackexchange.com/a/295978/80215
var mergeBands = function(image, previous) {
  return ee.Image(previous).addBands(image);
};

var merged = vegetation.iterate(mergeBands, ee.Image([]));

I you want to obtain an ee.ImageCollection, just use ee.ImageCollection(vegetation) for converting the list.

2
  • Thank you for helping me. The code seems to be working, but I must be doing something wrong. When I try this command, it gives me an error var vegetation2 = ee.ImageCollection(vegetation) Map.addLayer(vegetation2). Layer 4: Tile error: Expected a homogeneous image collection, but an image with incompatible bands was encountered: First image type: 1 bands ([classification_1985]). Current image type: 1 bands ([classification_1986]). Some bands might require explicit casts. Also, Map.addLayer(merged). return Line 191: Cannot add an object of type <ComputedObject> to the map. Commented May 31, 2022 at 4:15
  • @CarlosEduardoCarvalho yep, it's because it's an ImageColelction with multiple single-bands images. You can use ee.ImageCollection(vegetation).toBands() for fixing it. And for merged, use ee.Image(merged)
    – aldo_tapia
    Commented May 31, 2022 at 12: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.