1

I want to randomly sample one point from each masked image. In my actual dataset most of the images are masked, so I need to use stratifiedSample as opposed to sample to retrieve a sample from each image (sample drops masked values).

In this simple example I have two overlapping images, with one band each named constant of value 1. I put them in an image collection named ic, and mapped a function to sample a random point in each image.

var sampler = function(image){
  var sample = image.stratifiedSample({
    numPoints: 1, 
    classBand: 'constant',
    seed: 123,
    scale: 5,
    geometries: true
  }) // returns a feature collection
  return sample.first() // returns a feature
}
var mySamples = ee.FeatureCollection(ic.map(sampler))
Map.addLayer(mySamples, {color:'green'}, "sample pts")
Map.centerObject(mySamples, 12)

The two "random" points are in the same exact location!

overlapping images with green point point location coordinates

I suspect this is due to the seed parameter in stratifiedSample. Is there a way to avoid setting the seed in GEE? Anyone have a workaround?

https://code.earthengine.google.com/?scriptPath=users%2Fcaseyengstrom%2Freprex%3AresetSeed

1 Answer 1

2

The randomness in stratifiedSample is based on the pixel's location and the seed. Your best bet is to use some portion of the image's metadata as the seed. If there's nothing there to use (like a start time), then assign a random number to each image using randomColumn and use that (multiplied by some large constant):

var sampler = function(image){
  var seed = image.getNumber('random').multiply(1000000).int()
  var sample = image.stratifiedSample({
      seed: seed
      ...
   })
}
var mySamples = ic.randomColumn().map(sampler)

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.