0

When I mask, all the masked pixels have no value. So I go to unmask, but it then assigns a value of 0 to every single pixel, including outside of my region. How can I only have the unmask work within the boundaries of my region? I know of .clip and .clipToCollection and .filterBounds, but each one is not fixing the problem. My code:

//attempt at filtering out non-field bits
var classDiffIndex = cropFields.gt(0);
var connCount = classDiffIndex.updateMask(classDiffIndex).connectedPixelCount(40,true);
var classDiffIndex = cropFields.updateMask(connCount.gt(39)).unmask();
// var classDiffIndex = cropFields.where(connCount.gt(39), 0);
Map.addLayer(classDiffIndex,{min:0, max:1, palette:['000000', 'bb6666']},"Classified Difference Index L8")

Where I essentially classified my fields, used .connectedPixelCount to mask out the obvious non-field pixels, updated the mask to mask out places with less than 39 connected pixels, and then unmask the masked to give them values of 0. You can see in the commented out line another way that I tried to do this.

1
  • Okay so I basically got around my problem. I created a new Feature Collection, then did .clip(FeatColl). I don't know if this would work for everyone, but hopefully it helps someone!
    – Blake M.
    Commented Mar 4, 2020 at 19:22

2 Answers 2

1

Okay so I basically got around my problem. I created a new Feature Collection, then did .clip(FeatColl). I don't know if this would work for everyone, but hopefully it helps someone!

var classDiffIndex = cropFields.updateMask(connCount.gt(39)).unmask().clip(featColl);
1

Another thing you could try is save the original mask of cropFields as a layer and then use it to update the mask of the final classified layer.

var origMask = cropFields.select(0).mask();

...more code...

var classDiffIndex = cropFields.updateMask(connCount.gt(39)).unmask()
  .updateMask(origMask); // <<< Apply the original mask here.

This alternative avoids clipping, which can be resource intensive if the feature (featureCollection) is complex.

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.