2

Link for the code: https://code.earthengine.google.com/89616ca3147334915eca9f3957bb778f

I am using GEE in order to access image, generate random points on top of it and then sample it using the random points.

I have found out unexpected behavior : I generate 200 random points, and then when I sample the region, I get back only 192 points. This happens for different numbers of sample points: (500 random points ->472 sampled
100 random points -> 95 sampled
1000 random points -> 946 sampled etc...)

var smap=ee.ImageCollection('NASA_USDA/HSL/SMAP10KM_soil_moisture').filterBounds(geometry).filterDate('2020-01-01', '2021-01-01').median().clip(geometry);

var randomPoints = ee.FeatureCollection.randomPoints(
    {region:geometry, points: 1000});
    
var fcPointSamp = smap.sampleRegions({
  collection: randomPoints,
  scale: 10000
});

Map.addLayer(smap,{},'SMAP');

Map.addLayer(randomPoints,{},'random_points')
Map.centerObject(geometry,8)

print('Number of points', randomPoints.size());
print('Number of sampled points', fcPointSamp.size());


Why does this happen? Is there any way I can control the number of points that are being sampled?

Link for the code: https://code.earthengine.google.com/89616ca3147334915eca9f3957bb778f

EDIT:
based on Daniel's answer: workaround for geemap:

dummy_band=img.addBands(ee.Image(-9999).rename('temp')).clip(fc)

img_replaced_masked = img.unmask(dummy_band.select('temp'))

###replace the masked values in img with the -9999 values
img_mask_masked_values = img.unmask(dummy_band.select('temp'))

then, when I used sampleRegions, the no value pixels got value of "-9999"

1 Answer 1

3

You miss samples because there are areas in geometry not covered by smap. So, randomPoints will contain samples in this area, but not fcPointSamp.

A fix to this is to use stratifiedSample() instead. It will give you a random sampling, and you will not end up with missing values. Since you don't actually want to stratify on anything, you can create a dummy constant image, and use that band to stratify, so you end up with a single stratum.

var fcPointSamp = smap
  .addBands(ee.Image(1).rename('temp')) // Add dummy band
  .stratifiedSample({ 
    numPoints: 1000, 
    classBand: 'temp', // We stratify on the dummy band - a single class
    scale: 1000, // Since your geometry is so small, you cannot fit 1,000 samples with a scale of 10,000
    geometries: true // Just included so it can be put on a map, to highlight that this is random sampling
  });

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

2
  • thank you for the answer. From your answer I understand that the issue is that I have these "masked" pixels. You code worked great on GEE , however, I wonder if I can change the masked pixels into -9999 (wasn't able to do it but it should be possible no? ) also, my original script is writtenw ith geemap, wonder if you used stratified sample with geemap, couldn't find it there, and I get this error "EEException: Unable to transform geometry into requested projection. also when adding the projection as parameter. Do you know something about it? :)
    – ReutKeller
    Aug 21 at 12:12
  • just updating that it worked with unmask on geemap , thanks for the help! adding to the main post
    – ReutKeller
    Aug 21 at 12:30

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.