StratifiedSample() gives an empty feature collection in Google Earth Engine

google-earth-engineresamplingsamplingsentinel-2

I am trying to sample 10 random points from each value in the 'QA60' band of a Sentinel-2 image for machine learning purposes. When I use the .stratifiedSample() function, I obtain an empty feature collection (0 lines and 23 columns).
here is my code:

var s2 = ee.ImageCollection("COPERNICUS/S2_SR_HARMONIZED"),
    geometry = 
    /* color: #d63000 */
    /* shown: false */
    /* displayProperties: [
      {
        "type": "rectangle"
      }
    ] */
    ee.Geometry.Polygon(
        [[[-3.0637214179048367, 57.68153528540062],
          [-3.0637214179048367, 57.077464515994826],
          [-1.0202643866548367, 57.077464515994826],
          [-1.0202643866548367, 57.68153528540062]]], null, false);

var img = s2.filterBounds(geometry).first()

Map.addLayer(img.select('QA60').randomVisualizer())
print(img)
var train = img.stratifiedSample({
  numPoints: 10,
  classBand: 'QA60'
})

print(train)

The QA60 band has a resolution of 60m/pixel but the rest of the bands in the image have lower resolution. I am not sure if this is the cause of the problem. If it is, is there a way to increase the resolution of the QA60 band to 10m/pixel?

Best Answer

StratifiedSample discards any samples that have null values in them. In this case, you're samling all the bands, which include the QA10 anb QA20 bands, which are completely empty.

To fix this, select all the bands that you want to sample:

var train = img.select('B.*', 'QA60').stratifiedSample({
  numPoints: 10,
  classBand: 'QA60'
})
Related Question