Gdal

From znlgis/opengis-skills · View interactive version

Gdal is an AI agent skill from the GitHub repository znlgis/opengis-skills. Use when processing geospatial raster/vector data via GDAL/OGR CLI — ogr2ogr format conversion (Shapefile/GeoPackage/GeoJSON), gdalwarp reprojection, gdal_translate, gdal_calc.py NDVI, COG, mosaicking, DEM contours, gdalinfo -json checks — or diagnosing \"not recognized as a supported file format\";

How to use this skill

Skill file

项目地址: <https://github.com/OSGeo/gdal>

官方文档: <https://gdal.org/en/latest/>

源码命令文档: <https://gdal.org/en/latest/programs/>

最新版本: 3.13.3(截至 2026-11,下载页)

许可证: MIT

概述

GDAL 是地理空间数据处理的事实标准库。它提供了 50+ 个命令行工具,分为两大类:


环境准备

前置条件

部分 Linux 发行版与 GIS 软件栈会预装 GDAL(QGIS、PostGIS 客户端等),先用下面的命令确认工具在 PATH 中:

gdalinfo --version   # 验证 GDAL 版本
ogrinfo --version    # 验证 OGR 版本

安装方法

Linux (Debian/Ubuntu)
apt-get update
apt-get install gdal-bin python3-gdal
Linux (CentOS/RHEL)
yum install gdal gdal-devel
macOS (Homebrew)
brew install gdal
Conda
conda install -c conda-forge gdal
Docker

Docker Hub 上的 osgeo/gdal 已迁移(该镜像最后一次更新为 2023 年),现由 GHCR 发布:

docker run --rm -it ghcr.io/osgeo/gdal:latest bash

Python 绑定(可选)

某些 GDAL 工具(如 gdal_calc, gdal_merge, gdal_grid, gdal_polygonize)是 Python 脚本,需要安装 Python 绑定:

pip install GDAL
# 或
conda install -c conda-forge gdal

核心命令结构

新式 CLI (GDAL 3.11+)

GDAL 3.11 引入了统一的 gdal 命令行入口(官方文档标注 "Added in version 3.11";最新稳定版 3.13.3,2026-08-18 发布,见 下载页),后续版本持续加入 gdal vector concave-hull/convex-hull/dissolve/sort 等子命令,gdal dataset check 自 3.13 起提供:

gdal <command> <subcommand> [options] <inputs>

主要命令:

# 矢量重投影 + 格式转换(等价 ogr2ogr -t_srs ... -f GPKG)
gdal vector reproject --output-crs EPSG:3857 in.shp out.gpkg --overwrite

# 栅格镶嵌(等价 gdal_merge.py / gdalwarp 多输入)
gdal raster mosaic tile1.tif tile2.tif tile3.tif mosaic.tif --overwrite

# 数据集体检(3.13+,有错时退出码为 1)
gdal dataset check data.gpkg

传统 CLI(广泛使用)

ogrinfo <datasource> [layer]
ogr2ogr <output> <input> [options]
gdalinfo <raster_file>
gdal_translate <input> <output> [options]
gdalwarp <input> <output> [options]

矢量数据工具(OGR)

完整参数表和高级示例见 reference/vector-tools.md

ogrinfo — 矢量数据信息查询

# 列出所有图层
ogrinfo mydata.shp

# 获取特定图层摘要
ogrinfo mydata.shp layername -so

# JSON 格式输出(GDAL 3.7+)
ogrinfo -json mydata.shp

# 显示所有要素及其属性
ogrinfo -al -geom=YES mydata.shp

# 按属性过滤
ogrinfo mydata.shp -where "AREA > 1000"

ogr2ogr — 矢量数据格式转换和处理

# 格式转换(Shapefile → GeoJSON)
ogr2ogr output.geojson input.shp

# 指定输出格式(Shapefile → GeoPackage)
ogr2ogr -f GPKG output.gpkg input.shp

# 重投影(WGS84 → Web Mercator)
ogr2ogr -t_srs EPSG:3857 output.shp input.shp

# 属性过滤
ogr2ogr output.shp input.shp -where "area > 1000"

# 选择特定字段(几何默认保留;列表里写 geometry 只在几何被登记为字段名时有效)
ogr2ogr output.shp input.shp -select "id,name"

栅格数据工具(GDAL)

完整参数表和高级用法见 reference/raster-tools.md

gdalinfo — 栅格数据信息查询

# 基本信息
gdalinfo dem.tif

# JSON 输出
gdalinfo -json dem.tif

# 显示统计信息
gdalinfo -stats dem.tif

gdal_translate — 栅格格式转换和重采样

# 格式转换(GeoTIFF → PNG)
gdal_translate input.tif output.png

# 格式转换(带压缩)
gdal_translate -co COMPRESS=DEFLATE input.tif output.tif

# 重采样(降低分辨率)
gdal_translate -outsize 50% 50% input.tif output.tif

# 裁剪到范围
gdal_translate -projwin -180 90 -170 80 input.tif output.tif

COG / 云优化 GeoTIFF

COG 是带内部金字塔和分块的 GeoTIFF,便于 HTTP Range 按需读取(S3、对象存储、静态服务器):

# GeoTIFF → COG(压缩 + 金字塔一次生成)
gdal_translate -of COG -co COMPRESS=DEFLATE -co OVERVIEWS=AUTO input.tif output_cog.tif

# 3.11+ 新式 CLI 等价写法
gdal raster convert --of COG -co COMPRESS=DEFLATE input.tif output_cog.tif --overwrite

# 校验:分块、金字塔、是否被识别为 COG
gdalinfo -json output_cog.tif | grep -E 'COMPRESSION|LAYOUT|BLOCK'

gdalwarp — 栅格重投影和镶嵌

# 重投影(UTM 50N → WGS84)
gdalwarp -t_srs EPSG:4326 input_utm.tif output_wgs84.tif

# 镶嵌多个文件
gdalwarp input1.tif input2.tif input3.tif output_mosaic.tif

# 重采样并重投影
gdalwarp -tr 30 30 -t_srs EPSG:4326 input.tif output.tif

# 用矢量边界裁剪
gdalwarp -cutline clip_boundary.shp -crop_to_cutline input.tif output.tif

gdal_merge.py — 栅格镶嵌融合

# 基本合并
gdal_merge.py -o output.tif input1.tif input2.tif input3.tif

# 分离波段(创建多波段文件)
gdal_merge.py -separate -o rgb.tif r.tif g.tif b.tif

gdal_calc.py — 栅格计算器

# 两个栅格平均
gdal_calc.py -A input1.tif -B input2.tif --calc="(A+B)/2" --outfile=mean.tif

# 计算 NDVI(植被指数)
gdal_calc.py -A nir.tif -B red.tif \
  --calc="(A-B)/(A+B)" --outfile=ndvi.tif

gdal_contour — 等值线提取

# 提取等高线(10 米间隔)
gdal_contour -a elevation dem.tif contours.shp -i 10

# 输出为 GeoJSON
gdal_contour -f GeoJSON dem.tif contours.geojson -i 50

gdal_grid — 从散点数据创建网格

# 基本网格化(使用平均值)
gdal_grid -a average input_points.shp output_dem.tif

# 反距离加权(IDW)插值
gdal_grid -a invdist:power=2:smoothing=0 input_points.shp output_dem.tif

gdal_polygonize.py — 栅格转矢量

# 基本转换
gdal_polygonize.py input.tif output.shp

# 指定输出格式
gdal_polygonize.py input.tif -f GeoJSON output.geojson

gdaltindex — 创建栅格瓦片索引

# 基本索引
gdaltindex index.shp *.tif

# 输出为 GeoPackage
gdaltindex -of GPKG index.gpkg *.tif

gdal_edit.py — 编辑栅格元数据

# 设置坐标参考系
gdal_edit.py -a_srs EPSG:4326 input.tif

# 设置 NoData 值
gdal_edit.py -a_nodata 0 input.tif

nearblack — 清理黑白边界

# 清理黑色边界(默认)
nearblack input.tif -o output.tif

# 搜索白色边界
nearblack -white input.tif -o output.tif

常用环境变量和配置

GDAL 环境变量

| 变量 | 说明 | 示例 |

|------|------|------|

| GDAL_DATA | GDAL 数据文件目录 | /usr/share/gdal/ |

| PROJ_LIB | PROJ 数据文件目录 | /usr/share/proj/ |

| CPL_DEBUG | 调试日志级别 | ON / OFF |

| GDAL_CACHEMAX | 块缓存大小(MB) | 512 |

| GDAL_NUM_THREADS | 线程数 | 4 / ALL_CPUS |

| GDAL_DISABLE_READDIR_ON_OPEN | 禁用目录读取 | YES |

| GDAL_HTTP_TIMEOUT | HTTP 超时(秒) | 30 |

| GDAL_HTTP_MAX_RETRY | HTTP 重试次数 | 3 |

| CPL_VSIL_CURL_ALLOWED_EXTENSIONS | 允许的远程文件扩展名 | .tif,.tiff,.vrt |

| AWS_ACCESS_KEY_ID | AWS 密钥 ID | 用于 S3 访问 |

| AWS_SECRET_ACCESS_KEY | AWS 密钥 | 用于 S3 访问 |

| GOOGLE_APPLICATION_CREDENTIALS | Google Cloud 凭证文件 | /path/to/credentials.json |

使用环境变量

# 启用多线程处理
export GDAL_NUM_THREADS=ALL_CPUS

# 增加块缓存
export GDAL_CACHEMAX=1024

# 启用调试
export CPL_DEBUG=ON

# 访问 S3 上的栅格
export AWS_ACCESS_KEY_ID=xxxxx
export AWS_SECRET_ACCESS_KEY=xxxxx
gdalinfo /vsis3/bucket/key.tif

# 运行命令
gdalwarp input.tif output.tif

数据格式支持

常用矢量 / 栅格格式与扩展名对照见 reference/data-formats.md。


典型工作流:常见使用模式

模式 1: 批量格式转换

#!/bin/bash
# 将目录内所有 Shapefile 转换为 GeoJSON

for shp in *.shp; do
  base="${shp%.shp}"
  ogr2ogr -f GeoJSON "$base.geojson" "$shp"
done

模式 2: 批量重投影

#!/bin/bash
# 重投影所有 GeoTIFF 为 Web Mercator (EPSG:3857)

for tif in *.tif; do
  base="${tif%.tif}"
  gdalwarp -t_srs EPSG:3857 "$tif" "${base}_3857.tif"
done

模式 3: 创建 DEM 等值线

#!/bin/bash
# 从 DEM 提取等高线(10 米间隔)

gdal_contour -a elev dem.tif -i 10 contours.shp

# 转换为 GeoJSON
ogr2ogr -f GeoJSON contours.geojson contours.shp

模式 4: 计算 NDVI

#!/bin/bash
# 从多光谱影像计算 NDVI

gdal_calc.py \
  -A nir_band.tif \
  -B red_band.tif \
  --calc="(A-B)/(A+B)" \
  --outfile=ndvi.tif

模式 5: 镶嵌和重投影

#!/bin/bash
# 合并多个影像并重投影到 WGS84

gdalwarp -t_srs EPSG:4326 \
  image1.tif image2.tif image3.tif \
  output_mosaic_wgs84.tif

模式 6: 创建栅格瓦片索引

#!/bin/bash
# 为目录内所有 GeoTIFF 创建索引

gdaltindex -recursive -of GPKG \
  tile_index.gpkg /path/to/rasters/

常见问题

| 问题 | 解决 |

|------|------|

| gdalinfo 提示 ERROR 4: file not recognized | 确认文件格式与扩展名匹配,必要时用 gdalinfo --formats 检查驱动是否可用 |

| Python 工具(gdal_calc.py 等)找不到 | 这些是 Python 脚本,需安装 Python 绑定(pip install GDAL 或 conda) |

| pip install GDAL 编译失败 | 版本需与本机 GDAL 库匹配,推荐 conda install -c conda-forge gdal |

| 重投影后数据错位 | 核对源坐标系(-s_srs)与目标坐标系(-t_srs),GDAL 3+ 默认按 EPSG 轴序处理 |

| 中文路径 / 属性乱码 | 确认系统编码,Shapefile 用 -lco ENCODING=UTF-8 输出 |

| 大文件处理内存不足 | 设置 GDAL_CACHEMAX、使用 -wm 分块,避免一次镶嵌全部文件 |

| 就地修改文件损坏 | gdal_edit.py 等会直接改写文件,操作前先备份 |

| S3/HTTP 远程数据读取失败 | 检查 /vsis3/ 凭证、GDAL_HTTP_TIMEOUT、CPL_VSIL_CURL_ALLOWED_EXTENSIONS |


AI 使用建议


相关技能

参考资源

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.