1. 引言

aiearth-data 是达摩院 AI Earth 平台推出的 Python 数据工具包,面向遥感影像、地理空间数据的获取、处理与分析场景。它把卫星影像下载、波段运算、指数计算、数据可视化等高频操作封装成简洁接口,帮助开发者用更少的代码完成从数据获取到分析出图的全流程。

本文将从功能特性、安装方式、核心语法与参数入手,结合 9 个实际应用案例,系统讲解 aiearth-data 的使用方法,并整理常见错误与注意事项,帮助你快速上手。

2. 核心功能概览

aiearth-data 主要提供以下能力:

  • 遥感影像检索与下载:支持按时间范围、空间范围、云量等条件检索 Sentinel、Landsat 等卫星影像,并批量下载。
  • 波段管理与运算:提供波段提取、波段合成、波段运算接口,支持 NDVI、NDWI、EVI 等常用指数计算。
  • 数据预处理:包含辐射定标、大气校正、云掩膜、重投影、裁剪等预处理功能。
  • 地理空间分析:支持区域统计、变化检测、地物分类等基础分析能力。
  • 可视化与导出:提供快速渲染、伪彩色合成、结果导出为 GeoTIFF 等格式。

3. 安装与环境准备

3.1 环境要求

aiearth-data 基于 Python 3.7 及以上版本开发,推荐使用 3.8 或更高版本。依赖库主要包括 numpy、rasterio、geopandas、matplotlib 等常见科学计算与地理空间库。

3.2 安装命令

推荐使用 pip 直接安装:

pip install aiearth-data

如果需要安装最新开发版,可以从 GitHub 仓库安装:

pip install git+https://github.com/your-repo/aiearth-data.git

3.3 验证安装

安装完成后,可以通过以下命令验证是否成功:

import aiearth
print(aiearth.__version__)

4. 核心语法与参数详解

4.1 初始化与认证

使用前需要初始化客户端并完成认证。aiearth-data 通常需要配置访问令牌(Token):

import aiearth
初始化客户端
aiearth.initialize(token="your_token_here")
或者通过配置文件加载
aiearth.initialize(config_path="config.yaml")

4.2 影像检索

核心检索接口是 search_images,常用参数如下:

  • collection:数据集名称,如 Sentinel-2、Landsat-8 等。
  • start_time / end_time:时间范围,格式为字符串或 datetime 对象。
  • bbox:空间范围,格式为 [min_lon, min_lat, max_lon, max_lat]。
  • cloud_cover:最大云量百分比,用于筛选少云影像。
  • limit:返回结果数量上限。
images = aiearth.search_images(
    collection="sentinel-2",
    start_time="2024-01-01",
    end_time="2024-03-31",
    bbox=[116.3, 39.9, 116.5, 40.1],
    cloud_cover=20,
    limit=10
)

4.3 影像下载

检索到影像后,通过 download 方法下载到本地:

image = images[0]
image.download(output_dir="./data", format="tif")

4.4 波段运算与指数计算

aiearth-data 提供 band_math 接口,支持自定义波段运算表达式:

ndvi = aiearth.band_math(
    image=image,
    expression="(B8 - B4) / (B8 + B4)",
    output_name="ndvi"
)

同时内置常用指数函数:

ndvi = aiearth.calc_index(image, index="NDVI")
ndwi = aiearth.calc_index(image, index="NDWI")

5. 9 个实际应用案例

案例 1:城市植被覆盖度监测

通过计算 NDVI 指数评估城市植被覆盖情况,并统计区域平均值。

import aiearth
aiearth.initialize(token="your_token")
检索北京市区少云影像
images = aiearth.search_images(
collection="sentinel-2",
start_time="2024-06-01",
end_time="2024-08-31",
bbox=[116.3, 39.8, 116.5, 40.0],
cloud_cover=10,
limit=1
)
image = images[0]
计算 NDVI
ndvi = aiearth.calc_index(image, index="NDVI")
统计区域均值
mean_ndvi = ndvi.mean()
print(f"区域平均 NDVI:{mean_ndvi:.3f}")

案例 2:水体提取与面积估算

利用 NDWI 指数提取水体,并估算水域面积。

import aiearth
aiearth.initialize(token="your_token")
images = aiearth.search_images(
collection="sentinel-2",
start_time="2024-04-01",
end_time="2024-04-30",
bbox=[114.1, 30.5, 114.3, 30.7],
cloud_cover=5,
limit=1
)
image = images[0]
计算 NDWI
ndwi = aiearth.calc_index(image, index="NDWI")
阈值分割提取水体
water_mask = ndwi > 0.2
估算面积(假设影像分辨率 10 米)
pixel_area = 10 * 10  # 平方米
water_area = water_mask.sum() * pixel_area / 1e6  # 转换为平方公里
print(f"水体面积约:{water_area:.2f} 平方公里")

案例 3:农作物长势时序分析

获取多期影像,分析 NDVI 随时间变化趋势,判断作物生长状况。

import aiearth
import matplotlib.pyplot as plt
aiearth.initialize(token="your_token")
检索 2024 年 4 月至 9 月多期影像
images = aiearth.search_images(
collection="sentinel-2",
start_time="2024-04-01",
end_time="2024-09-30",
bbox=[116.8, 36.5, 117.0, 36.7],
cloud_cover=15,
limit=6
)
ndvi_series = []
dates = []
for img in images:
ndvi = aiearth.calc_index(img, index="NDVI")
ndvi_series.append(ndvi.mean())
dates.append(img.date)
plt.plot(dates, ndvi_series, marker="o")
plt.title("NDVI Time Series")
plt.xlabel("Date")
plt.ylabel("Mean NDVI")
plt.show()

案例 4:土地利用分类(监督分类)

使用简单阈值或机器学习方法对影像进行分类,区分建设用地、植被和水体。

import aiearth
import numpy as np
aiearth.initialize(token="your_token")
images = aiearth.search_images(
collection="sentinel-2",
start_time="2024-05-01",
end_time="2024-05-31",
bbox=[121.4, 31.2, 121.6, 31.4],
cloud_cover=10,
limit=1
)
image = images[0]
计算 NDVI 和 NDWI
ndvi = aiearth.calc_index(image, index="NDVI")
ndwi = aiearth.calc_index(image, index="NDWI")
简单规则分类
land_class = np.zeros(ndvi.shape, dtype=np.uint8)
land_class[ndwi > 0.2] = 1   # 水体
land_class[(ndvi > 0.3) & (ndwi <= 0.2)] = 2  # 植被
land_class[(ndvi <= 0.3) & (ndwi <= 0.2)] = 3  # 建设用地
统计各类面积
unique, counts = np.unique(land_class, return_counts=True)
for cls, cnt in zip(unique, counts):
print(f"类别 {cls}:{cnt * 100 / 1e6:.2f} 平方公里")

案例 5:洪水淹没范围评估

对比洪水前后影像,通过 NDWI 差值识别新增水体区域。

import aiearth
aiearth.initialize(token="your_token")
洪水前影像
before = aiearth.search_images(
collection="sentinel-2",
start_time="2024-05-01",
end_time="2024-05-15",
bbox=[114.0, 30.0, 114.3, 30.3],
cloud_cover=5,
limit=1
)[0]
洪水后影像
after = aiearth.search_images(
collection="sentinel-2",
start_time="2024-07-01",
end_time="2024-07-15",
bbox=[114.0, 30.0, 114.3, 30.3],
cloud_cover=5,
limit=1
)[0]
ndwi_before = aiearth.calc_index(before, index="NDWI")
ndwi_after = aiearth.calc_index(after, index="NDWI")
新增水体区域
flooded = (ndwi_after > 0.2) & (ndwi_before <= 0.2)
flooded_area = flooded.sum() * 100 / 1e6
print(f"新增淹没面积约:{flooded_area:.2f} 平方公里")

案例 6:森林火灾迹地检测

通过火灾前后 NBR(归一化燃烧指数)变化识别过火区域。

import aiearth
aiearth.initialize(token="your_token")
火灾前影像
before = aiearth.search_images(
collection="sentinel-2",
start_time="2024-02-01",
end_time="2024-02-28",
bbox=[102.0, 25.0, 102.3, 25.3],
cloud_cover=5,
limit=1
)[0]
火灾后影像
after = aiearth.search_images(
collection="sentinel-2",
start_time="2024-04-01",
end_time="2024-04-30",
bbox=[102.0, 25.0, 102.3, 25.3],
cloud_cover=5,
limit=1
)[0]
计算 NBR(使用 B8 和 B12 波段)
nbr_before = aiearth.band_math(before, "(B8 - B12) / (B8 + B12)")
nbr_after = aiearth.band_math(after, "(B8 - B12) / (B8 + B12)")
差值识别过火区
dNBR = nbr_before - nbr_after
burned = dNBR > 0.3
burned_area = burned.sum() * 100 / 1e6
print(f"过火面积约:{burned_area:.2f} 平方公里")

案例 7:海岸线变迁监测

对比不同年份影像,提取水陆边界,分析海岸线进退变化。

import aiearth
aiearth.initialize(token="your_token")
def extract_waterline(year):
images = aiearth.search_images(
collection="sentinel-2",
start_time=f"{year}-01-01",
end_time=f"{year}-12-31",
bbox=[121.8, 30.5, 122.0, 30.7],
cloud_cover=10,
limit=1
)
img = images[0]
ndwi = aiearth.calc_index(img, index="NDWI")
return ndwi > 0.1
提取 2020 年和 2024 年水陆边界
waterline_2020 = extract_waterline(2020)
waterline_2024 = extract_waterline(2024)
计算变化区域
erosion = waterline_2020 & (~waterline_2024)  # 被侵蚀区域
accretion = waterline_2024 & (~waterline_2020)  # 新增陆地
print(f"侵蚀面积:{erosion.sum() * 100 / 1e6:.2f} 平方公里")
print(f"淤积面积:{accretion.sum() * 100 / 1e6:.2f} 平方公里")

案例 8:城市热岛效应分析

利用 Landsat 热红外波段反演地表温度,分析城市热岛空间分布。

import aiearth
aiearth.initialize(token="your_token")
images = aiearth.search_images(
collection="landsat-8",
start_time="2024-07-01",
end_time="2024-07-31",
bbox=[116.3, 39.8, 116.5, 40.0],
cloud_cover=10,
limit=1
)
image = images[0]
提取热红外波段(Landsat-8 的 Band 10)
thermal = image.get_band("B10")
简单辐射定标与温度反演(示意)
实际需要根据元数据中的定标系数计算
temperature = thermal * 0.1 - 273.15  # 示意公式
统计城市与郊区温度差异
urban_temp = temperature[116.35:116.45, 39.85:39.95].mean()
suburb_temp = temperature[116.45:116.55, 39.95:40.05].mean()
print(f"城市平均温度:{urban_temp:.1f}°C")
print(f"郊区平均温度:{suburb_temp:.1f}°C")
print(f"热岛强度:{urban_temp - suburb_temp:.1f}°C")

案例 9:多时相变化检测(PCA 方法)

对两期影像做主成分分析,提取主要变化信息。

import aiearth
import numpy as np
from sklearn.decomposition import PCA
aiearth.initialize(token="your_token")
获取两期影像
img1 = aiearth.search_images(
collection="sentinel-2",
start_time="2024-03-01",
end_time="2024-03-31",
bbox=[120.0, 30.0, 120.3, 30.3],
cloud_cover=5,
limit=1
)[0]
img2 = aiearth.search_images(
collection="sentinel-2",
start_time="2024-09-01",
end_time="2024-09-30",
bbox=[120.0, 30.0, 120.3, 30.3],
cloud_cover=5,
limit=1
)[0]
提取多光谱波段数据
bands1 = img1.get_bands(["B2", "B3", "B4", "B8"])
bands2 = img2.get_bands(["B2", "B3", "B4", "B8"])
计算差值
diff = bands2 - bands1
PCA 降维提取主要变化
h, w, c = diff.shape
diff_flat = diff.reshape(-1, c)
pca = PCA(n_components=1)
change = pca.fit_transform(diff_flat).reshape(h, w)
阈值提取显著变化区域
change_mask = np.abs(change) >

 

《AI提示工程必知必会》为读者提供了丰富的AI提示工程知识与实战技能,主要包括各类提示词的应用,如问答式、指令式、状态类、建议式、安全类和感谢类提示词,以及如何通过实战演练掌握提示词的使用技巧;使用提示词进行文本摘要、改写重述、语法纠错、机器翻译等语言处理任务,以及在数据挖掘、程序开发等领域的应用;AI在绘画创作上的应用,百度文心一言和阿里通义大模型这两大智能平台的特性与功能,以及市场调研中提示词的实战应用。通过阅读《AI提示工程必知必会》,读者可掌握如何有效利用AI提示工程提升工作效率,创新工作流程,并在职场中脱颖而出。

 

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐