我是 Google Earth Engine 的新手,我正在尝试获取从 2024/05/01 到 2024/06/01 的 Sentinel-2 图像,以获取从 aeronet 位置获取的坐标](https://aeronet.gsfc.nasa .gov/aeronet_locations_v3.txt).
# Define date range for dataset filtering
start_date = '2024-05-01'
end_date = '2024-06-01'
dataset = (
ee.ImageCollection('COPERNICUS/S2_HARMONIZED')
.filterDate(start_date, end_date)
# .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 20))
.map(mask_s2_clouds)
)
count = 4466
def export_image_for_day(image, date_str, location):
global count
if not image.Not(): # Ensure image is not None
# Define the export task without region parameter
export_task = ee.batch.Export.image.toDrive(
image=image,
description=f"image_{date_str}_{location[0]}_{location[1]}",
folder='Sentinel2_Images',
fileNamePrefix=f"image_{date_str}_{location[0]}_{location[1]}",
region=ee.Geometry.Point(location).Buffer(500),
fileFormat='GeoTIFF',
maxPixels=1e13
)
export_task.start()
print(f"{count}: Export task started for {date_str} at {location}")
else:
print(f"{count}: No image found for {date_str} at {location}")
count += 1
# Loop through dates and locations
for date_str in pd.date_range(start_date, end_date).strftime('%Y-%m-%d').tolist():
# Calculate the next day as the end date for filtering
next_day = (datetime.strptime(date_str, '%Y-%m-%d') + timedelta(days=1)).strftime('%Y-%m-%d')
# Use date_str as the start date and next_day as the end date for filtering
daily_images = dataset.filterDate(date_str, next_day)
for _, row in stations_df.iterrows():
location = (row['Longitude(decimal_degrees)'], row['Latitude(decimal_degrees)'])
image = daily_images.first() # Retrieve first image of the day
export_image_for_day(image, date_str, location)
time.sleep(.2) # Small delay between exports
函数 mask_s2_clouds 定义为:
def mask_s2_clouds(image):
"""Masks clouds in a Sentinel-2 image using the QA band.
Args:
image (ee.Image): A Sentinel-2 image.
Returns:
ee.Image: A cloud-masked Sentinel-2 image.
"""
qa = image.select('QA60')
# Bits 10 and 11 are clouds and cirrus, respectively.
cloud_bit_mask = 1 << 10
cirrus_bit_mask = 1 << 11
# Both flags should be set to zero, indicating clear conditions.
mask = (
qa.bitwiseAnd(cloud_bit_mask)
.eq(0)
.And(qa.bitwiseAnd(cirrus_bit_mask).eq(0))
)
return image.updateMask(mask).divide(10000)
输出为:
4466: No image found for 2024-05-01 at (-56.070214, -15.555244)
4467: No image found for 2024-05-01 at (-56.104453, -9.871339)
4468: No image found for 2024-05-01 at (-63.068552, -9.19907)
4469: No image found for 2024-05-01 at (-110.953003, 32.233002)
4470: No image found for 2024-05-01 at (-76.839833, 38.9925)
4471: No image found for 2024-05-01 at (-74.476387, 39.802223)
4472: No image found for 2024-05-01 at (-48.41, -10.7)
4473: No image found for 2024-05-01 at (-47.900002, -15.917)
4474: No image found for 2024-05-01 at (-72.188, 42.532)
所有地点都会如此。由于我对GEE不太熟悉,所以我被困在这里。
“if not image.Not():”语句始终为 False。 图像是存储在服务器中的
ee.Image
对象。除非您调用 getInfor 或实际使用导出方法下载图像,否则该图像实际上并不是由服务器计算的。
此外,Not也不是用来检查图像是否存在,它只是检查像素是否为空(并且是按像素进行的)。
如果您确实需要在集合为空或不为空时更改此行为,您可以检查集合的大小:
def export_image_for_day(collection, date_str, location):
global count
if not collection.size().getInfo(): # Ensure image is not None
# Define the export task without region parameter
export_task = ee.batch.Export.image.toDrive(
image=collection.mosaic(),
description=f"image_{date_str}_{location[0]}_{location[1]}",
folder='Sentinel2_Images',
fileNamePrefix=f"image_{date_str}_{location[0]}_{location[1]}",
region=ee.Geometry.Point(location).Buffer(500),
fileFormat='GeoTIFF',
maxPixels=1e13
)
export_task.start()
print(f"{count}: Export task started for {date_str} at {location}")
else:
print(f"{count}: No image found for {date_str} at {location}")
count += 1
请注意,您需要将 daily_images 提供给函数而不是
image
。