1

Here's my code:

function getSentinel2(){
    var cloudSat2 = ee.ImageCollection("COPERNICUS/S2_SR_HARMONIZED") //Get Satelite
        .filterBounds(polygonGeoJSON) //Filter to the right place
        .filterDate('2018', new Date().getFullYear().toString() + "12" + "31") //Filter from 2018 to current
        .sort('system:time_start')  //Sort by in chronological order
        .map(function(img){
      var cloudBit = img.reduceRegion({reducer: ee.Reducer.median(), geometry: polygonGeoJSON}).get('QA60');
      
      print(cloudBit);
      
      if(cloudBit !== 0){
        return ee.Feature(null, {
          //data: img.normalizedDifference(['B8','B4']).multiply(ee.Image.constant(1.37)).subtract(ee.Image.constant(0.086)).reduceRegion({reducer: ee.Reducer.mean(), geometry: polygonGeoJSON}).get('nd'), //Kc
          data: cloudBit,
          time: img.get('system:time_start') //Time
        });
      } else {
        return null;
      }
    }, true);

  return cloudSat2;
}

The main thing to focus on is the cloudBit variable. If it is equal to 0, I return null. So, when I chart this FeatureCollection, why do I still have 0 values (among other values)? I can't figure out why 0 and 0 aren't equal to each other. Even if I only get the 0 values, it gives me no values at all.

I'd assume this is something weird with things being shown to me on the front end differently than they are on the backend, but I can't figure it out.

1 Answer 1

0

Your issue is related to client-side vs server-side objects. Your cloudBit is a server-side object which you perform a client-side conditional on. That doesn't work. You can use ee.Algorithms.If() instead. Read up on this here. It's better yet to avoid using conditionals altogether. You can simply return your features even if it has a zero cloudBit, then filter them out:

function getSentinel2() {
  var cloudSat2 = ee.ImageCollection("COPERNICUS/S2_SR_HARMONIZED") 
    .filterBounds(polygonGeoJSON) 
    .filterDate('2018', new Date().getFullYear().toString() + "12" + "31") 
    .sort('system:time_start') 
    .map(function(img) {
      var cloudBit = img.reduceRegion({
        reducer: ee.Reducer.median(),
        geometry: polygonGeoJSON
      }).get('QA60')
      return ee.Feature(null, {
        data: cloudBit,
        time: img.get('system:time_start') 
      })
    })
    .filter(ee.Filter.neq('data', 0))
  return cloudSat2
}

https://code.earthengine.google.com/d3e8d878facf9b857b38a5b35826e594

Note that you have to be careful with the QA60 band. From the data catalog:

QA60 is a bitmask band that contained rasterized cloud mask polygons until 2022-01-25, when these polygons stopped being produced. Starting 2024-02-28, legacy-consistent QA60 bands are constructed from the MSK_CLASSI cloud classification bands.

You might want to consider using Cloud Score+ instead.

1
  • Thanks! The filter version worked great Commented Jun 19 at 15:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.