Google Earth Engine: How to create a feature collection with bounded, randomly-assigned classes

feature-collectiongoogle-earth-engineimage classification

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?

Best Answer

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())
    })
Related Question