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!
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
Best Answer
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):