0

I have this polygon:

ee.Geometry({
  "functionInvocationValue": {
    "functionName": "GeometryConstructors.Polygon",
    "arguments": {
      "coordinates": {
        "constantValue": [
          [
            [
              39.395406,
              49.043902
            ],
            [
              39.395406,
              49.046139
            ],
            [
              39.399011,
              49.046139
            ],
            [
              39.399011,
              49.043902
            ],
            [
              39.395406,
              49.043902
            ]
          ]
        ]
      },
      "evenOdd": {
        "constantValue": true
      }
    }
  }
})

I would like to get the value in Sentinel-2 band 4 for all the pixels.

This is the code I'm using:

start_date,end_date = '2022-08-05','2022-08-07'
bands = ['B4']
sensor = "COPERNICUS/S2_SR_HARMONIZED"

img_collection = (ee.ImageCollection(sensor).filterBounds(polygon).filterDate(start_date,end_date).select(bands))
pixels_info = img_collection.getRegion(geometry=polygon, scale=10)
df = pd.DataFrame(pixels_info.getInfo())
df.columns = df.iloc[0]
df = df.drop(0)
df.shape

>>> (1000, 5)

This means there are 1000 pixels for a specific date (2022-08-06).

However, when I directly count the pixels, I get a different number:

img_id = 'COPERNICUS/S2_SR_HARMONIZED/20220806T082611_20220806T083525_T37UEQ'
image = ee.Image(img_id)
t_pixel_count = image.select(['B4']).reduceRegion(reducer=ee.Reducer.count(),geometry=polygon,scale=10).rename(['B4'],['total_pixels'])
t_pixel_count.getInfo()

>>> {'total_pixels': 666}

Why is that? and how many pixels there are in the image?

1 Answer 1

0

After some consultation, I realized that I also needed to specify the CRS (in the getRegion function), because the scale was not enough.

Now it outputs the same number of pixels:

start_date,end_date = '2022-08-05','2022-08-07'
bands = ['B4']
sensor = "COPERNICUS/S2_SR_HARMONIZED"

img_collection = (ee.ImageCollection(sensor).filterBounds(polygon).filterDate(start_date,end_date).select(bands))
pixels_info = img_collection.getRegion(geometry=polygon, scale=10,crs='EPSG:32637')
df = pd.DataFrame(pixels_info.getInfo())
df.columns = df.iloc[0]
df = df.drop(0)
df.shape

>>> (664, 5)

This means there are 1000 pixels for a specific date (2022-08-06).

However, when I directly count the pixels, I get a different number:

img_id = 'COPERNICUS/S2_SR_HARMONIZED/20220806T082611_20220806T083525_T37UEQ'
image = ee.Image(img_id)
t_pixel_count = image.select(['B4']).reduceRegion(reducer=ee.Reducer.count(),geometry=polygon,scale=10, crs='EPSG:32637').rename(['B4'],['total_pixels'])
t_pixel_count.getInfo()

>>> {'total_pixels': 664}

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.