1

Here's an example script: https://code.earthengine.google.com/d511144dde609f4cf1fd8607499e2026

I would like to create a set of random points with random classes to use as training data in order to generate and evaluate a null distribution. The .randomColumn function only gives values between 0-1. To get random classes, I need uniformly distributed integers from 0 to 3, but it does not look like Feature Collections support the kind of math required to manipulate .randomColumn() like that.

My next thought is .randomPoints(), but I am unfamiliar with its use. If I use .randomPoints() to create a random Feature Collection to use as null training data, how do I:

  1. Make sure the points are only on land/unmasked area?
  2. Make sure the points don't overlap with the existing validation data?
  3. Add random feature classes?

1 Answer 1

3

If you've got image masks involved, the easiest way is to generate a random image (multiply by 4 and round down), mask it with your valid/land mask, and sample it with stratifiedSample. That'll make sure you don't sample points from places you don't want and give you fine control over number of points and how close they are to each other (via scale).

var source = ee.Image.random().multiply(4).int()
    .updateMask(mask)
    .stratifiedSample({
      numPoints: 1000, 
      region: geometry, 
      scale: 1000, 
      geometries: true})

That said, your original question about how to create random classes on a feature collection is with randomPoints + randomColumn() and mapping over the collection to do whatever math you want to with random number. Something like:

var fc = ee.FeatureCollection.randomPoints(geometry, 1000)
    .randomColumn()
    .map(function(f) {
      return f.set("class", f.getNumber('random').multiply(4).int())
    })
1
  • The first one did the trick, thanks!
    – Matt
    Commented Mar 27, 2023 at 18:18

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.