QGIS 谷歌卫星影像导出与 Python 切片全流程踩坑记录
摘要
本文记录了从 QGIS 加载 Google Satellite 在线影像、导出本地 GeoTIFF,到 Python 环境配置、影像切片、格式转换的完整流程及过程中遇到的典型问题与解决方案。环境:QGIS 3.44、Python 3.12、GDAL 3.9、Windows 11。
一、影像导出
1.1 加载 Google Satellite
通过 QGIS 插件 QuickMapServices 加载 Google Satellite 在线底图:
- 插件 → 管理并安装插件 → 搜索
QuickMapServices → 安装 - Web → QuickMapServices → Google → Google Satellite
1.2 导出本地 GeoTIFF
在线瓦片图层无法直接用于后续处理,需导出为本地栅格文件。
使用工具: 「将地图转为栅格」(Convert Map to Raster)
“不使用图层右键「另存为栅格」,该方式对在线瓦片支持有缺陷,易报「目标提供程序」错误。
参数设置:
注意事项:
- 输出模式必须选渲染图像,选「原始数据」会导致颜色发灰、偏色
二、Python 切片环境配置
影像切片使用 Python + GDAL 脚本实现,环境配置是最容易踩坑的环节。
2.1 坑一:PyCharm 直接 pip 安装失败
操作: 在 PyCharm 终端执行 pip install gdal
报错:
error: Microsoft Visual C++ 14.0 or greater is required.
原因: Windows 下 pip 安装 GDAL 需要本地编译 C++ 代码,缺少编译环境时必然失败。
解决方向: 改用 Conda 安装预编译包。
2.2 坑二:Conda 默认源安装不全
操作:
conda create -n gdal_env python=3.12
conda activate gdal_env
conda install gdal=3.9
报错:
from osgeo import gdal
# ModuleNotFoundError: No module named 'osgeo'
原因: Anaconda default 源的 GDAL 包可能不完整,缺少 osgeo 模块。
最终解决: 使用 conda-forge 源安装
conda install -c conda-forge gdal
验证:
python -c "from osgeo import gdal; print(gdal.__version__)"
2.3 坑三:PyCharm 解释器配置找不到入口
问题: 在 Settings 中找不到 Conda 环境配置入口。
解决:
- 点击 PyCharm 窗口右下角状态栏的 Python 版本显示
- Add Interpreter → Conda Environment
- 浏览找到 Anaconda 安装目录下的环境路径:
Python 3.12 (PV_shibie) D:/Anaconda3/install/envs/gdal_env/python.exe
2.4 最终环境配置命令
# 创建环境
conda create -n gdal_env python=3.12
# 激活环境
conda activate gdal_env
# 安装 GDAL(必须用 conda-forge 源)
conda install -c conda-forge gdal
三、影像切片
使用 Python + GDAL 将大尺寸 GeoTIFF 切分为指定尺寸的瓦片。
3.1 脚本
from osgeo import gdal
import os
# =========修改这里的路径========
input_tif = r"E:\PV_shibie\***.tif"
out_folder = r"E:\PV_shibie\***\images"
tile_w = 640
tile_h = 640
# ==============================
os.makedirs(out_folder, exist_ok=True)
ds = gdal.Open(input_tif)
w_full = ds.RasterXSize
h_full = ds.RasterYSize
count = 0
for y_off in range(0, h_full, tile_h):
for x_off in range(0, w_full, tile_w):
out_path = os.path.join(out_folder, f"pv_hb_{count}.tif")
gdal.Translate(out_path, ds,
srcWin=[x_off, y_off, tile_w, tile_h],
outputType=gdal.GDT_Byte)
count +=1
print(f"切片完成,一共生成 {count} 张图片")
3.2 说明
- 边缘不足
tile_w × tile_h 的瓦片自动跳过,保证尺寸统一 - 大影像建议在独立 Conda 环境运行,勿在 QGIS Python 控制台执行(易内存溢出崩溃)
四、批量转 JPG
TIFF 保留了完整地理信息,如需用于查看或后续处理,可转换为 JPG 格式。
4.1 脚本
from osgeo import gdal
import os
gdal.DontUseExceptions()
# ========= 改成你自己的路径 =========
in_dir = r"E:\PV_shibie\***\python_cut\images"
out_dir = r"E:\PV_shibie\***\python_cut\images_jpg"
# ====================================
os.makedirs(out_dir, exist_ok=True)
files = [f for f in os.listdir(in_dir) if f.endswith(".tif")]
total = len(files)
print(f"共 {total} 个tif文件,开始转jpg...")
for i, fname in enumerate(files):
tif_path = os.path.join(in_dir, fname)
jpg_path = os.path.join(out_dir, os.path.splitext(fname)[0] + ".jpg")
gdal.Translate(
jpg_path, tif_path,
format="JPEG",
creationOptions=["QUALITY=85"],
outputType=gdal.GDT_Byte
)
if (i + 1) % 100 == 0:
print(f"已转换 {i + 1}/{total}")
print(f"✅ 全部转换完成,输出目录:{out_dir}")
4.2 说明
- 建议保留 TIFF 原件,JPG 仅作为便捷查看/处理用
- TIFF 含地理坐标信息,后续空间分析、结果映射均需用到
五、数据清洗
转换完成后,建议手动清理:
清洗后可显著减少后续处理的数据量。
避坑速查表
| | |
|---|
| | |
| | |
pip install gdal | | 改用 conda install -c conda-forge gdal |
No module named 'osgeo' | | |
| | |
| | |
FutureWarning | | 脚本开头加 gdal.DontUseExceptions() |
| | |
流程总结
QuickMapServices 加载在线底图
↓
将地图转为栅格 → 本地 GeoTIFF
↓
Conda 环境配置(conda-forge GDAL)
↓
Python 脚本切片 → TIFF 瓦片
↓
批量转换 → JPG
↓
数据清洗
核心经验: Windows 下 GDAL 安装务必使用 conda-forge 源,避免 pip 编译和 default 源缺包问题。