5

I've marked an area of interest on a map (aoi2) and applied the clip() function to satellite images based on these coordinates.

However, there is a discrepancy between the actual size of the coordinates and the size of the box in the clipped satellite images.

I'm really curious about the reason for this. I am a university student studying in South Korea, and I have been looking for a solution for over half a day but can't find an answer, so I'm asking here.

import geemap, ee
ee.Initialize()
Map2 = geemap.Map()

aoi2 = ee.Geometry.Polygon(
    [[[-80.12480511144283, 9.34580402882444],
      [-79.66126346730358, 9.34580402882444],
      [-79.66126346730358, 8.982192273390574],
      [-80.12480511144283, 8.982192273390574]]])

Map2.addLayer(aoi2)

startDate = '2018-07-01'
endDate = '2018-07-31'


sentinelImageCollection = ee.ImageCollection('COPERNICUS/S2') \
    .filterBounds(aoi2) \
    .filterDate(startDate, endDate)
print("Number of images = ", sentinelImageCollection.size())



sentinelImage = sentinelImageCollection \
    .sort('CLOUDY_PIXEL_PERCENTAGE') \
    .first() \
    .clip(aoi2)
print("Sentinel image taken at = ", sentinelImage.date())

Map2.addLayer(
    sentinelImage,
    {'min': 0.0, 'max': 2000, 'bands': ['B4', 'B3', 'B2']},
    'RGB'
)

# Calculate NDWI (Normalized Difference Water Index)
ndwi = sentinelImage.normalizedDifference(['B3', 'B8']).rename('NDWI')

# Add NDWI layer to the map
Map2.addLayer(
    ndwi,
    {'palette': ['red', 'yellow', 'green', 'cyan', 'blue']},
    'NDWI'
)
Map2.centerObject(aoi2,12)
Map2

enter image description here

2 Answers 2

8

Since you use .first() function, you get one image which you see after the code runs. To solve this issue:

(1) remove .first() method and filter the collection by CLOUDY_PIXEL_PERCENTAGE.

sentinelImageCollection = ee.ImageCollection('COPERNICUS/S2') \
    .filterBounds(aoi2) \
    .filterDate(startDate, endDate) \
    .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 15)) \
    .sort('CLOUDY_PIXEL_PERCENTAGE')

(2) use .map function to get normalized and clipped images.

ndwi = sentinelImageCollection.map(
        lambda image: image.normalizedDifference(['B3', 'B8']) \
            .clip(aoi2) \
            .rename('NDWI'))

Script:

import geemap, ee
ee.Initialize()

Map2 = geemap.Map()

aoi2 = ee.Geometry.Polygon(
    [[[-80.12480511144283, 9.34580402882444],
      [-79.66126346730358, 9.34580402882444],
      [-79.66126346730358, 8.982192273390574],
      [-80.12480511144283, 8.982192273390574]]])

Map2.addLayer(aoi2)

startDate = '2018-07-01'
endDate = '2018-07-31'

sentinelImageCollection = ee.ImageCollection('COPERNICUS/S2') \
    .filterBounds(aoi2) \
    .filterDate(startDate, endDate) \
    .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 15)) \
    .sort('CLOUDY_PIXEL_PERCENTAGE')

Map2.addLayer(sentinelImageCollection, {'min': 0.0, 'max': 2000, 'bands': ['B4', 'B3', 'B2']}, 'RGB')

# Calculate NDWI (Normalized Difference Water Index)
ndwi = sentinelImageCollection.map(
    lambda image: image.normalizedDifference(['B3', 'B8']) \
        .clip(aoi2) \
        .rename('NDWI'))

# Add NDWI layer to the map
Map2.addLayer(ndwi, {'palette': ['red', 'yellow', 'green', 'cyan', 'blue']}, 'NDWI')

Map2.centerObject(aoi2, 11)
Map2

enter image description here

The result still has empty space because the images placed there have higher cloud value than 15 and I guess 1-month interval is not adequate because the images between '2018-07-01' and '2018-07-31' are too cloudy.

1
  • I'm really thankful. As someone majoring in public administration, I had a hard time studying these topics. Thanks to you, it's been a happy morning. I sincerely appreciate it.
    – DonghoSHin
    Commented Nov 26, 2023 at 3:47
1

I think it is caused by the method .first(). Your AOI may consist of two images. you can remove this .clip() to confirm it.

1
  • 1
    I sincerely appreciate your comments Correct. As you mentioned, upon checking the .first() image, I found that this image strangely only covers a part of the designated aoi range. When I looked at the fifth image in the index, it closely (about 90 percent) fell within the specified aoi2 range. But I'm not sure if I need to keep checking by entering the index like this every time, or if there was an issue with the first filterBounds(aoi2).
    – DonghoSHin
    Commented Nov 25, 2023 at 15:13

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.