Google Earth Engine – Reclassifying Raster Image in Google Colab with NumPy

google-colabgoogle-earth-enginegoogle-earth-engine-python-apinumpy

In Google Earth Engine's Java API, I have the following code that works and am trying to use this in Google Colab with Python.

How do you reclassify the pixels in a raster image in Python?

var FinalResult= datasetA.where(sdA.gt(sdAthreshold),56).where(sdA.lte(sdAthreshold).and(mA.gte(All_A)),102).where(sdA.lte(sdAthreshold).and(mA.lt(ALL_A)),76).rename('Result');

I have tried the following after reading NumPy reclassification documentation to no avail. This did not work.

import numpy as np
FinalResult= np.select.datasetA([sdA > sdAthreshold, (sdA <= sdAthreshold) & (mA >= AllA), (sdA <= sdAthreshold) & (mA < AllA)],[56,102,76])

And neither did this.

datasetA[np.where(sdA > sdAthreshold)] = 1 
datasetA[np.where((sdA <= sdAthreshold) & (mA >= AllA))] = 2 
datasetA[np.where((sdA <= sdAthreshold) & (mA < AllA))] = 3 

Best Answer

If your variables where numpy arrays, you could simply do this:

datasetA[sdA > sdAthreshold] = 1 
datasetA[np.logical_and(sdA <= sdAthreshold, mA >= AllA)] = 2 
datasetA[np.logical_and(sdA <= sdAthreshold, mA < AllA)] = 3

But in earth engine, your variables aren't numpy arrays, so numpy will not help you here.

EaarthEngine API is mostly similar in JavaScript and Python. So you can use something similar to what you provided in your question in JS. here is what I tried and it didn't raise any errors. Give it a try and see if this is what you were seeking.

FinalResult = sdG.where(sdG.gt(sdGthreshold),56) \
                 .where(sdG.lte(sdGthreshold).And(mG.gte(CLallyearsG)),102) \
                 .where(sdG.lte(sdGthreshold).And(mG.lt(CLallyearsG)),76) \
                 .rename('Result') 
Related Question