Google Earth Engine – Techniques for Grouping/Clustering Pixels Using GEE

clusteringgoogle-earth-enginegoogle-earth-engine-python-api

I am trying to cluster the pixels in one class based on the pixel value. But when I execute this code then class1 is correctly clustered but class2 merges the pixels of class1 as well. Similarly, class3 and class4 merges the pixels of previous classes' pixels.

class1 = img.gte(0.60) and (img.lte(0.70)).selfMask().set('val', 1)
class2 = img.gte(0.70) and (img.lte(0.80)).selfMask().set('val', 2)
class3 = img.gte(0.80) and (img.lte(0.90)).selfMask().set('val', 3)
class4 = img.gte(0.90).selfMask().set('val', 4)

Why is it happening and how to resolve this issue?

Best Answer

This is a Client vs. Server issue. You cannot use regular Python operators like and to act on Earth Engine images; you need to use the methods on the image type.

Additionally, in the particular case of and, because it's a Python keyword (making it not allowed as a method name) you have to capitalize it. (This is a special case for the names and, or, and not only.)

class1 = img.gte(0.60).And(img.lte(0.70)).selfMask().set('val', 1)
class2 = img.gte(0.70).And(img.lte(0.80)).selfMask().set('val', 2)
class3 = img.gte(0.80).And(img.lte(0.90)).selfMask().set('val', 3)
class4 = img.gte(0.90).selfMask().set('val', 4)
Related Question