I have a single-band raster (US landcover) where each pixel is assigned to one of a few classes. I want to get a random sample of points (lat-long) throughout an area such that each of those points is located within large neighborhoods of pixels of the same landcover class. Currently, I use EE's "randomPoints" to get a random sample of n points but many of those pixels may be isolated pixels belonging to one landcover class surrounded by pixels of other classes. What I need is a set of randomly selected points each surrounded by a neighborhood of radius r of pixels of the same class (as determined by the raster). Any ideas on how to approach this problem?
To clarify, I want points like the one on the right of the figure below (large yellow neighborhood) and not the point on the left.
Here is my code for the landcover raster within California:
import ee
ee.Initialize()
california = ee.FeatureCollection("TIGER/2018/States").filter(ee.Filter.eq("NAME", 'California'))
landcover = ee.ImageCollection('USGS/NLCD_RELEASES/2016_REL')\
.filter(ee.Filter.eq('system:index', '2004')).first().select('landcover')
landcover_ca = landcover.clip(california)
point_sample = ee.FeatureCollection.randomPoints(california, points=1000, seed=10)
Best Answer
I suppose you first could mask your landcover image, to remove "isolated" pixels, then sample that masked image. Here's an approach that masks based on ratio of pixels of the same class within a given radius:
https://code.earthengine.google.com/3dcf1c81c18b4b979fb4e160d8c67383
UPDATE
If you are getting "Computation timed out" errors, you can try to split your sampling into smaller batches of points:
https://code.earthengine.google.com/0d602e28fd5f5b6d2e85ac358f382d5b