做遥感影像处理,Python生态有哪些核心库?本文梳理完整工具链。
工具链总览
数据读写:rasterio、GDAL数值计算:numpy、scipy机器学习:scikit-learn深度学习:PyTorch、TensorFlow可视化:matplotlib、folium地理处理:geopandas、shapely
核心库详解
1. Rasterio
基于GDAL的Python封装,读写栅格数据的首选。
import rasterio# 读取影像with rasterio.open('image.tif') as src: print(f'波段数: {src.count}') print(f'尺寸: {src.width} x {src.height}') print(f'坐标系: {src.crs}') print(f'变换矩阵: {src.transform}')# 读取数据 data = src.read()# 所有波段 band1 = src.read(1)# 单波段
核心功能:
2. GDAL
地理数据处理的瑞士军刀。
from osgeo import gdal# 读取影像ds = gdal.Open('image.tif')band = ds.GetRasterBand(1)data = band.ReadAsArray()# 格式转换gdal.Translate('output.png', 'input.tif')
核心功能:
3. NumPy
遥感数据本质是多维数组,NumPy是基础。
import numpy as np# 影像数据是3D数组: (波段, 高, 宽)data = src.read()print(f'形状: {data.shape}')# (4, 1000, 1000)# 波段运算ndvi = (data[3] - data[0]) / (data[3] + data[0])# NIR和Red
4. Scikit-learn
遥感分类的主力工具。
from sklearn.ensemble import RandomForestClassifierfrom sklearn.cluster import KMeans# 监督分类clf = RandomForestClassifier()clf.fit(X_train, y_train)prediction = clf.predict(X_test)# 非监督分类kmeans = KMeans(n_clusters=5)labels = kmeans.fit_predict(pixels)
5. PyTorch/TensorFlow
深度学习语义分割。
import torchimport segmentation_models_pytorch as smp# 加载预训练模型model = smp.Unet( encoder_name='resnet34', encoder_weights='imagenet', classes=5)
6. Matplotlib
可视化遥感影像。
import matplotlib.pyplot as plt# 显示单波段plt.imshow(band1, cmap='gray')plt.colorbar()plt.show()# RGB合成rgb = np.stack([red, green, blue], axis=-1)plt.imshow(rgb)plt.show()
7. GeoPandas
矢量数据处理。
import geopandas as gpd# 读取矢量gdf = gpd.read_file('boundary.shp')# 空间查询within = gdf[gdf.geometry.within(buffer)]
安装建议
推荐方式:conda
# 创建专用环境conda create -n remote_sensing python=3.10conda activate remote_sensing# 安装核心库conda install -c conda-forge rasterio gdal numpy scipy matplotlibconda install -c conda-forge geopandas shapely fionaconda install scikit-learn# 深度学习(可选)pip install torch torchvisionpip install segmentation-models-pytorch
pip安装
pip install rasterio numpy scipy matplotlibpip install geopandas scikit-learn
常见问题
Q1: GDAL安装报错
建议用conda安装,避免依赖问题。
Q2: rasterio和GDAL的关系
rasterio是GDAL的Python封装,更Pythonic。
Q3: 内存不够怎么办
# 窗口读取with rasterio.open('big_image.tif') as src: window = rasterio.windows.Window(0, 0, 1000, 1000) data = src.read(window=window)
工具选择建议
总结
Python遥感处理的核心工具链:
- rasterio
- NumPy
- scikit-learn
- PyTorch
- matplotlib
掌握这套工具链,90%的遥感任务都能搞定。
作者:坐标系6297 | 用地图看懂世界,用数据讲清故事