I'm trying to get a numpy array from a specific line region with no success. I have been trying to use ee.Image.sampleRectangle() but it is sampling only Polygons, and when I use it on Line region, I am getting some unwanted points. Is there any other function that sample the images?

polygon = ee.Geometry.LineString(multicoords[0],ee.Projection('EPSG:32636'))
planarPolygon = ee.Geometry(polygon);

band_arrs = image.sampleRectangle(region=polygon)
band_arr_B8A = band_arrs.get('B8A')
  • The problem is that a LineString has no area,
    – gene
    Jun 24, 2021 at 9:29
  • I know, but still, ee.Image.sampleRectangle() is sampling an area despite this fact.
    – YosDos
    Jun 24, 2021 at 9:45

1 Answer 1


Use reduceRegion with a toList reducer.

result = image.reduceRegion(reducer=ee.Reducer.toList(), geometry=polygon, scale=..., crs=...)
  • Hi Thank, it works fine, but somehow if my LineString falls on one pixel I'm not getting any value.
    – YosDos
    Jun 24, 2021 at 16:17
  • Either buffer it by 1/4 pixel so it's always at least 1/2 a pixel wide, or use the lineString to mask the original image by painting it into an image: mask = ee.Image().byte().paint(lineString, 1); image.updateMask(mask).reduceRegion(reducer=ee.Reducer.toList(), geometry=lineString.bounds(), ...` Jun 25, 2021 at 1:05

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.