GEE统计特定区域特定时间上的Landsat/Sentinel的影像信息

GEE统计特定区域特定时间上的Landsat/Sentinel的影像信息

前言

很多时候,我们会想查看一个特定的研究区和研究时间范围内的Landsat 或者 Sentinel系列数据集中影像的一些参数信息,如成像时间,云量,数量等等,以帮助我们确定应该如何筛选影像。比如,我想知道1990年至2020年期间,所有覆盖到北京市的Landsat影像的信息,如下图所示。Landsat系列和Sentinel系列是目前使用最为广泛的两个影像数据集,现在,我提供了实现这一操作的代码框架,大家可以在此基础上进行修改,以得到你想要提取的信息。基于此框架,你还可以扩展到其他影像数据集(如MODIS等)。

统计Landsat影像的信息

统计Landsat系列信息的代码如下:

python 复制代码
import ee
import geemap
import pandas as pd
geemap.set_proxy(port=7778)
geemap.ee_initialize()
Map = geemap.Map()

# 加载研究区
json_path = './北京市.geojson'
study_region = geemap.geojson_to_ee(json_path)
bound = study_region.bounds()
start_year = 1990
end_year = 2020
start_month = 4
end_month = 10

# 创建Landsat影像集合,过滤日期和区域
landsat5 = ee.ImageCollection("LANDSAT/LT05/C02/T1_L2")
landsat7 = ee.ImageCollection("LANDSAT/LE07/C02/T1_L2")
landsat8 = ee.ImageCollection("LANDSAT/LC08/C02/T1_L2")
landsat9 = ee.ImageCollection("LANDSAT/LC09/C02/T1_L2")

landsat_collection1 = ee.ImageCollection.merge(landsat5, landsat7)
landsat_collection2 = ee.ImageCollection.merge(landsat8, landsat9)
landsat_collection = ee.ImageCollection.merge(landsat_collection1, landsat_collection2) \
    .filterBounds(study_region) \
    .filter(ee.Filter.calendarRange(start_year, end_year, 'year')) \
    .filter(ee.Filter.calendarRange(start_month, end_month, 'month'))

# 云量和质量等级函数
# 云量小于20%-1级,20%到40%-2级,40%-60%-3级, 60%-80%-4级,大于80%-5级
def assign_quality(cloud_coverage):
    if 0 <= cloud_coverage < 20:
        return 1
    elif 20 <= cloud_coverage < 40:
        return 2
    elif 40 <= cloud_coverage < 60:
        return 3
    elif 60 <= cloud_coverage < 80:
        return 4
    elif 80 <= cloud_coverage <= 100:
        return 5
    else:
        return None  

# 提取影像的日期和传感器信息
def extract_info(image):
    date = image.date().format('YYYY-MM-dd')
    satellite = image.get('SPACECRAFT_ID')
    sensor = image.get('SENSOR_ID')
    year = image.date().get('year')
    month = image.date().get('month')
    day = image.date().get('day')
    cloud_coverage = image.get('CLOUD_COVER')
    quality = ee.Algorithms.If(
        ee.Number(cloud_coverage).lt(20), 1,
        ee.Algorithms.If(ee.Number(cloud_coverage).lt(40), 2,
        ee.Algorithms.If(ee.Number(cloud_coverage).lt(60), 3,
        ee.Algorithms.If(ee.Number(cloud_coverage).lt(80), 4, 5))))
    imgID = image.id()

    return ee.Feature(None, {
        '成像日期': date,
        '卫星型号': satellite,
        '传感器名称': sensor,
        '影像年份': year,
        '影像月份': month,
        '当月日': day,
        '云量': cloud_coverage,
        '质量': quality,
        'GEEID': imgID
    })
    
# 应用函数并创建特征集合
image_data = landsat_collection.map(extract_info)
feature_collection = ee.FeatureCollection(image_data)

# 获取数据并转换为DataFrame
data = feature_collection.getInfo()
features = data['features']
records = []

for feature in features:
    records.append(feature['properties'])

# 创建DataFrame
df = pd.DataFrame(records)

# 导出为Excel文件
df.to_excel('./landsat_statistic.xlsx', index=False)

在这段代码中,我统计1990年-2020年北京市生长季(4-10月)的Landsat影像的信息,最总结果输出为'landsat_statistic.xlsx'的excel表格。

统计Sentinel影像的信息

统计Sentinel影像信息的代码与统计Landsat影像的代码思路是一致的,以下是详细代码:

python 复制代码
import ee
import geemap
import pandas as pd
geemap.set_proxy(port=7778)
geemap.ee_initialize()
Map = geemap.Map()

# 加载研究区
json_path = './北京市.geojson'
study_region = geemap.geojson_to_ee(json_path)
bound = study_region.bounds()
start_year = 1990
end_year = 2020
start_month = 4
end_month = 10

# 创建Sentinel-2影像合集
s2Collection = (ee.ImageCollection("COPERNICUS/S2_SR_HARMONIZED")
                .filterBounds(study_region)
                .filter(ee.Filter.calendarRange(start_year, end_year, 'year'))
                .filter(ee.Filter.calendarRange(start_month, end_month, 'month'))
                )

# 云量和质量等级函数
def assign_quality(cloud_coverage):
    if 0 <= cloud_coverage < 20:
        return 1
    elif 20 <= cloud_coverage < 40:
        return 2
    elif 40 <= cloud_coverage < 60:
        return 3
    elif 60 <= cloud_coverage < 80:
        return 4
    elif 80 <= cloud_coverage <= 100:
        return 5
    else:
        return None  

# 提取影像的日期和传感器信息
def extract_info(image):
    img_id = image.get('system:id')
    date = image.date().format('YYYY-MM-dd')
    year = image.date().get('year')
    month = image.date().get('month')
    day = image.date().get('day')
    doy = ee.Date(image.get('system:time_start')).getRelative('day','year').add(1)
    cloud_coverage = image.get('CLOUDY_PIXEL_PERCENTAGE')
    quality = ee.Algorithms.If(
        ee.Number(cloud_coverage).lt(20), 1,
        ee.Algorithms.If(ee.Number(cloud_coverage).lt(40), 2,
        ee.Algorithms.If(ee.Number(cloud_coverage).lt(60), 3,
        ee.Algorithms.If(ee.Number(cloud_coverage).lt(80), 4, 5))))
    
    return ee.Feature(None, {
        'ID': img_id,
        '成像日期': date,
        '影像年份': year,
        '影像月份': month,
        '当月日': day,
        '云量': cloud_coverage,
        '质量': quality,
        'doy': doy
    })

# 应用函数并创建特征集合
image_data = s2Collection.map(extract_info)
feature_collection = ee.FeatureCollection(image_data)

# 获取数据并转换为DataFrame
data = feature_collection.getInfo()
features = data['features']
records = []

for feature in features:
    records.append(feature['properties'])

# 创建DataFrame
df = pd.DataFrame(records)

# 导出为Excel文件
df.to_excel('./S2影像信息统计.xlsx', index=False)

这段代码同样统计了1990至2020年北京市生长季的Sentinel2的影像信息

相关推荐
DanCheng-studio4 小时前
网安毕业设计简单的方向答疑
python·毕业设计·毕设
轻抚酸~4 小时前
KNN(K近邻算法)-python实现
python·算法·近邻算法
独行soc6 小时前
2025年渗透测试面试题总结-264(题目+回答)
网络·python·安全·web安全·网络安全·渗透测试·安全狮
汤姆yu6 小时前
基于python的外卖配送及数据分析系统
开发语言·python·外卖分析
如何原谅奋力过但无声7 小时前
TensorFlow 1.x常用函数总结(持续更新)
人工智能·python·tensorflow
翔云 OCR API7 小时前
人脸识别API开发者对接代码示例
开发语言·人工智能·python·计算机视觉·ocr
AndrewHZ8 小时前
【图像处理基石】如何在图像中提取出基本形状,比如圆形,椭圆,方形等等?
图像处理·python·算法·计算机视觉·cv·形状提取
ruleslol8 小时前
Spring Cloud: Sentinel入门讲解
spring cloud·sentinel
温轻舟9 小时前
Python自动办公工具05-Word表中相同内容的单元格自动合并
开发语言·python·word·自动化办公·温轻舟
习习.y10 小时前
python笔记梳理以及一些题目整理
开发语言·笔记·python