I have an image ('biome') and a feature collection ('grid'). I want to sample points on the image, precisely 10 points per feature(a grid cell). Also, I want to drop points that intersect the masked pixel (oceans) of an image.

When I try with the ee.Image.sample function, I can drop points in the ocean but I can't specify the number of points per grid cell.

When I try with ee.FeatureCollection.randomPoints function, I can specify the number of points per grid cell but I can't drop the points in the ocean.

Does anyone have any ideas?

Link to the script : https://code.earthengine.google.com/91366ac39038271ed66115eb9bb87e7f

var biome = ee.Image("OpenLandMap/PNV/PNV_BIOME-TYPE_BIOME00K_C/v01");
var grid = american.coveringGrid('EPSG:4326', 500000); 

Map.addLayer(biome, null, "biome");
Map.addLayer(grid, null, "grid");

// sample function
var points = biome.sample({
  region : grid.geometry(),
  scale : 30,
  numPixels : 10

Map.addLayer(points, null, 'points');

//randomPoints function

var points1 = grid.map(function(feature){
  return ee.FeatureCollection.randomPoints({
    region:feature.geometry(), points:10})
var points1 = points1.flatten();


// How to sample 10 points randomly per grid cell while dropping all sampled points that intersect masked pixels of biome image? 

1 Answer 1


You were close; you want a combination of the two. Use a mapped stratifiedSample on the biome's mask band.

var points1 = grid.map(function(feature){
  return biome.mask().gt(0).selfMask().stratifiedSample({
    region : feature.geometry(),
    numPoints: 10,

Specifying a scale of 30 is extreme overkill since the biome map seems to be 1km.


  • Thank you very much! Actually, I have to join the biome's band value as well to each sampled points. As all band values turned into 1 when sampling according to your code, how can I add actual values?
    – yoyo
    Feb 13 at 14:26
  • biome.mask().gt(0).selfMask().addBands(...) Feb 14 at 11:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.