0

I'm trying to reclassify multiple bands from my raster. It is a land use raster of the Amazon basin by Mapbiomas. I need to reclassify all 36 bands at once.

## Reclassifying multiple bands raster values in Google Earth Engine
#Inport amazonia mapbiomas
AmazMapbiomas = (ee.Image('projects/mapbiomas-raisg/public/collection3/mapbiomas_raisg_panamazonia_collection3_integration_v2'))
#A list of pixel values to replace.
fromList = [1, 4, 5, 6, 10, 12, 14, 22, 24, 25, 26, 27, 29, 30, 33, 34]
# A corresponding list of replacement values (10 becomes 1, 20 becomes 2, etc).
toList   = [1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 9, 11, 6, 8, 9, 10]
#list length check if is the same length
print(len(fromList))
print(len(toList))
## Replace pixel values in the image. If the image is multi-band, only the
#here I wanted to reclassify all 36 stalls, how can I do this at once?

AmazMapbiomasReclass = img.remap({
    From: fromList,
    to: toList,
    defaultValue: 0,
    bandName: 'all'
});

1 Answer 1

0

Here is the solution using the Javascript API; however, it should be pretty easy to translate it to the python API. The trick is to extract the band names and map the remap function to each band. Then you just need to transform the list into an ImageCollection, then convert it to a multiband image and finally, rename with the original band names.

var bands = AmazMapbiomas.bandNames();

// Create image collection from list of images
var resul = ee.ImageCollection.fromImages(
  // Map remap function on the list of all bands
  bands.map(function(band){
    // Remap values
    var AmazMapbiomasReclass = AmazMapbiomas.remap({
        // 'from' has to be written in lowercase
        from: fromList,
        to: toList,
        defaultValue: 0,
        // Band name according to the current band
        bandName: ee.String(band)
    });
    // Return the remapped image
    return AmazMapbiomasReclass;
  })
  // Convert from ImageCollection to multiband image
  ).toBands()
  // Rename bands to the original names
   .rename(bands);


Map.addLayer(AmazMapbiomas, {bands:'classification_2019'}, 'original');
Map.addLayer(resul, {bands:'classification_2019'}, 'reclassified');
3
  • Thanks, Jonathan! I'll try it. Commented Sep 24, 2022 at 14:38
  • the problem is that for some reason the "from" is not being recognized. Look the error: File "<ipython-input-34-76c029fe735c>", line 55 from: fromList, ^ SyntaxError: invalid syntax Commented Sep 26, 2022 at 10:10
  • Try defining the remapping function as: def remapF(band): # Remap values AmazMapbiomasReclass = AmazMapbiomas.remap(fromList, toList, 0, band) return AmazMapbiomasReclass Commented Sep 26, 2022 at 14:35

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.