0

I would like to sample training data from some raster data source (e.g. Sentinel-2) based on labels extracted from an already available dataset (e.g. some binary layer or classification map) in Google Earth Engine. While it might seem straight-forward to use ee.Image.sampleRegions(), this is apparently not the case. The reason being that the first input argument ee.Image.sampleRegions() is a FeatureCollection with one or more geometries defining the areas to sample from. If your input is an already existing classification layer, this won't work from scratch. Thus, the question possibly narrows down to how one can create a FeatureCollection where each class of the already available raster layer corresponds to one Feature, and how this can be integrated with ee.Image.sampleRegions().

1 Answer 1

2

SampleRegions is what you use when you've got a bunch of regions; it's the wrong tool for what you're trying to do. You can either use stratifiedSample if you want a random sampling (see example below), or just use sample() if you want all of the pixels. Either way, you need to include your class band in the stack of bands that you want to sample.

var classes = ee.Image("MODIS/051/MCD12Q1/2013_01_01").select(0).rename("class")
var img = ee.Image("srtm90_v4")

var samples = img.addBands(classes).stratifiedSample({
  numPoints: 100, 
  classBand: "class", 
  region: ee.Geometry.Polygon([[[-73, 42],[-73, 41],[-72, 41],[-72, 42]]]), 
  scale: 1000,
  geometries: true,
})

// Colorize the samples according to the modis palette.
var palette = ee.List(classes.get('Land_Cover_Type_1_class_palette'))
var values = ee.List(classes.get('Land_Cover_Type_1_class_values'))
samples = samples.map(function(f) {
  var value = f.get('class')
  var index = values.indexOf(value)
  var color = palette.get(index)
  return f.set({style: {color: color}})
})
Map.addLayer(samples.style({styleProperty: 'style'}))

https://code.earthengine.google.com/f5798da88f000b0e136a109cd01c630a

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.