I have some points and know how to extract the Landsat data at these points and build and export a training dataset to feed an external classifier. But how can I efficiently and automatically build a small patch of 2-D pixels (say 3x3 neighborhood) around each sample point to build and export a similar training dataset to feed an external convolutional classifier?

1 Answer 1


I think what you need is ee.Image.neighborhoodToArray. I give an example:

var i = ee.Image.random().addBands(ee.Image.random(1)).clip(geometry)

var neig = i.neighborhoodToArray(ee.Kernel.square(1))

var training = neig.reduceRegions({
  scale: 1000,
  reducer: 'first'


link: https://code.earthengine.google.com/a406f32c989923eae10d73af8b1871be

  • Thank you very much @Rodrigo for your kind attention and quick reply. Yes it seems to be the function that I need and I wonder why GEE tutorials are completely silent about this and some other useful functions. But I don't understand one thing in your code: Why you use ReduceRegions and what does the 'First' reducer? I don't get what it does. Why not using SampleRegions function instead?
    – Shahriar49
    Apr 2, 2019 at 21:09
  • The first reducer takes the first value of the inputs, doesn't actually reduce them, but as it is a point feature collection, it's the same to use first or mean (or any). If you have a polygon feature collection the mean reducer will compute the mean of all inputs, and the first will take just the first (which doesn't make sense in most cases) Apr 2, 2019 at 22:25
  • Thanks again @Rodrigo. I understood it and found my mistake in some of previous comments. It was all about dependency of inspector values to the zoom level! After a certain zoom level I get matching values between original image and samples. But still I have a confusion: When I add neig raster to the display, I can never get the correct neighborhood values by inspecting it (regardless of zoom level and even when the inspector shows the value of original image correctly). Why?
    – Shahriar49
    Apr 3, 2019 at 14:25

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