I want to select random 90000 pixels/points from region of interest wherever there is built-up area. To my surprise when I print number of pixels selected it shows only 1044. I am literally lost how is this possible. I tried even with 3000 also and got 113 only output. Can anyone help me in identifying how I can get my desired sample.

Here is my code:

 var Districts = ee.FeatureCollection('users/phd20muddasira/Districts_geom_corrected_ID')

var JK= Districts.filter(ee.Filter.eq('ST_NM', 'Jammu & Kashmir'))
var HP= Districts.filter(ee.Filter.eq('ST_NM', 'Himachal Pradesh'))
var PUN= Districts.filter(ee.Filter.eq('ST_NM', 'Punjab'))

// Map.addLayer(JK,{color:'pink'},'JK')
// Map.addLayer(HP,{color:'purple'},'HP')
// Map.addLayer(PUN,{color:'green'},'PUN')

// We will make a feature collection of three states 

var States = JK.merge(HP).merge(PUN)



// We will use ESA Land cover dataset to use builtup class to identify urban areas

var ESA = ee.ImageCollection("ESA/WorldCover/v100")

var classification = ee.Image(ESA.first()).select('Map').clip(States)

//Map.addLayer(classification,{},'worldCover Classification')

 var builtup = classification.eq(50).selfMask();
var NLSample = builtup.sample({

// // Due to computation time out we will export samples as an asset and then import it

 var NLPixels = ee.FeatureCollection(NLSample);
// print(result)  
 collection:  NLPixels,
description: 'NLSample90K'})

I have given access to my used asset

GEE code link


1 Answer 1


Using sample with the numPixels option generates that many random points in the given region and then samples them. However it doesn't check if the points are masked in the image or not and any points that are masked, get dropped.

You probably want to use stratifiedSample because most of the image you're sampling from is masked inside your geometry.

var NLSample = builtup.stratifiedSample({
  • Thank you Noel. It worked for me. Commented Oct 29, 2022 at 21:23
  • I exported above sample and tried to add ID to each point in QGIs, but to my surprise I see only three points there. Can anyone help me what is happening wrong. Is it possible that I can add ID the in the above code also and other properties like to which state and district the point belongs. Commented Oct 30, 2022 at 12:44

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.