ogr2ogr可以执行单个文件的转换。
示例代码:ogr2ogr -f 'ESRI Shapefile' output.shp input.kml
当需要批量转换时,需要通过bash脚本,循环调用以上方法。
# 创建输出目录(如果不存在)
mkdir -p output_shapes
# 遍历所有 KML 文件并转换
for input_file in input_dir/*.kml; do
output_name=$(basename "$input_file" .kml) # 去除扩展名
ogr2ogr -f 'ESRI Shapefile' "output_shapes/${output_name}.shp" "$input_file"
done
输入目录:将 input_dir 替换为你的 KML 文件所在路径。
输出目录:所有生成的 Shapefile 会保存在 output_shapes 文件夹中。
注意:若要重复覆盖现在文件,请添加-overwrite变量。
示例:ogr2ogr -overwrite -f 'ESRI Shapefile' "output_shapes/${output_name}.shp" "$input_file"
还有另一种思路:先用QGIS将待转换的Kml文件合并为一个图层,在用图层的Save As方法,导出为Shp文件。