In this post, I will outline how to query and export Sentinel-2 images in Google Earth Engine. In GEE, you can query and merge multiple images for the time period and region of your choice. For example, it is possible to collect multiple images observed over a year of a specific national park, and then merge them into one image by summarizing (e.g., median) only the data with less than 5% cloud cover at the pixel level. Additionally, you can export this to Google Drive, download it, and then use it in geospatial software such as QGIS. Let’s take a look at the process through code.
First, I will call the ee and geemap libraries and proceed with Earth Engine authentication and initialization.
import ee
import geemap
Authenticate to Earth Engine
ee.Authenticate()
Initialize Earth Engine
ee.Initialize(project='my-project')
The target area will be Palgongsan National Park in South Korea. The boundary of Palgongsan National Park can be called from the WDPA Feature Collection using the WDPA ID.
Palgongsan: Protected area with WDPA ID 555571373
wdpa = ee.FeatureCollection("WCMC/WDPA/current/polygons") \
.filter(ee.Filter.eq('WDPAID', 555571373))
aoi = wdpa.geometry()
Next, let’s define a Sentinel-2 Image Collection that meets specific criteria. The period is from January 1, 2024, to January 31, 2024, and the area is within the boundary of Palgongsan National Park with a cloud cover of less than 5%.
Select Sentinel-2 Image Collection
collection = ee.ImageCollection("COPERNICUS/S2_SR_HARMONIZED") \
.filterDate('2024-01-01', '2024-01-31') \
.filterBounds(aoi) \
.filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 5))
Print the size of the Sentinel-2 Image Collection. A total of 11 images were queried, which are the results of checking all images that overlap with the Palgongsan National Park area while satisfying the date and cloud cover conditions.
Print the size of the S2 Image Collection
collection_size = collection.size()
print("The size of Sentinel-2 Image Collection:", collection_size.getInfo())
The size of the S2 Image Collection: 11
You can inspect the attribute information of each image in the queried Sentinel-2 Image Collection as follows.
Print the attribute information of each image in the S2 Image Collection
image_list = collection.toList(collection_size)
for i in range(collection_size.getInfo()):
image = ee.Image(image_list.get(i))
date = image.date().format("YYYY-MM-dd").getInfo()
cloud_percentage = image.get("CLOUDY_PIXEL_PERCENTAGE").getInfo()
print(f"Image {i+1}: Date={date}, Cloudy Pixel={cloud_percentage}%")
Image 1: Date=2024-01-04, Cloudy Pixel=3.074293%
Image 2: Date=2024-01-04, Cloudy Pixel=1.332756%
Image 3: Date=2024-01-07, Cloudy Pixel=4.386907%
Image 4: Date=2024-01-07, Cloudy Pixel=4.488421%
Image 5: Date=2024-01-12, Cloudy Pixel=0.448056%
Image 6: Date=2024-01-12, Cloudy Pixel=1.065743%
Image 7: Date=2024-01-14, Cloudy Pixel=0.721162%
Image 8: Date=2024-01-27, Cloudy Pixel=0.220503%
Image 9: Date=2024-01-27, Cloudy Pixel=2.45147%
Image 10: Date=2024-01-29, Cloudy Pixel=2.292939%
Image 11: Date=2024-01-29, Cloudy Pixel=4.434421%
The pixel values of each image in the Sentinel-2 Image Collection can be easily summarized. For example, with the following definition, calculating the median image is straightforward. Simple, isn’t it?
Calculate the median image
median_image = collection.median()
Now let’s map the median Sentinel-2 image along with the boundary of Palgongsan National Park. I’ll set the band combination of the median image to natural color (B4, B3, B2).
Map object creation
Map = geemap.Map(width="800px", height="500px")
Add median image
Map.addLayer(median_image, {'bands': ['B4', 'B3', 'B2'], 'max': 2000}, 'Median Image')
Add AOI
Map.addLayer(aoi, {
'color': '00FF00', # Green
'width': 2 # Outline thickness
}, 'Palgongsan National Park')
Map.centerObject(aoi, 12)
Now let’s export the median image to Google Drive. You can set the name (description), folder (export), region, scale, maximum pixels (maxPixels), etc., as shown below. Here, description refers to the file name, and maxPixels is set to 1e13, which means 1 trillion pixels, to prevent errors due to exceeding the pixel limit.
Export the image to Google Drive
geemap.ee_export_image_to_drive(median_image,
description='median_image',
folder='export',
region=aoi,
scale=10,
maxPixels=1e13)
The process of exporting the image to Google Drive may take some time depending on the file size. Once completed, you can find the median_image.tif file in the specified folder as shown below. You can download it and open it in QGIS.
Open the data in QGIS, setting the project coordinate system to EPSG:32652 and the band combination to natural color (B4, B3, B2). The median_image.tif file is a single multi-band image that contains complete Sentinel-2 band information, allowing for additional analysis through QGIS functionalities.
Top comments (0)