当前位置: 首页 > news >正文

在Python中处理GDB、MDB和Shapefile文件转换

在Python中处理GDB、MDB和Shapefile文件转换,有多种库可以使用。以下是常用的方法和工具:

主要工具库

1. GDAL/OGR

最强大的地理数据处理库,支持多种格式:

python

from osgeo import ogr# GDB/MDB转Shapefile
def gdb_to_shp(gdb_path, output_shp):driver = ogr.GetDriverByName('FileGDB')gdb_ds = driver.Open(gdb_path, 0)for i in range(gdb_ds.GetLayerCount()):layer = gdb_ds.GetLayer(i)shp_driver = ogr.GetDriverByName('ESRI Shapefile')shp_ds = shp_driver.CreateDataSource(f"{output_shp}_{layer.GetName()}.shp")shp_ds.CopyLayer(layer, layer.GetName())shp_ds = Nonegdb_ds = None# MDB转Shapefile
def mdb_to_shp(mdb_path, output_shp):driver = ogr.GetDriverByName('PGeo')mdb_ds = driver.Open(mdb_path, 0)for i in range(mdb_ds.GetLayerCount()):layer = mdb_ds.GetLayer(i)shp_driver = ogr.GetDriverByName('ESRI Shapefile')shp_ds = shp_driver.CreateDataSource(f"{output_shp}_{layer.GetName()}.shp")shp_ds.CopyLayer(layer, layer.GetName())shp_ds = Nonemdb_ds = None

2. Fiona

更Pythonic的地理数据处理库:

python

import fiona# GDB转Shapefile
def convert_gdb_to_shp(gdb_path, output_dir):with fiona.open(gdb_path, 'r', driver='FileGDB') as gdb:for layer_name in gdb.layer_names:with fiona.open(gdb_path, 'r', layer=layer_name, driver='FileGDB') as layer:output_path = f"{output_dir}/{layer_name}.shp"# 创建输出schemaoutput_schema = layer.schemawith fiona.open(output_path, 'w', driver='ESRI Shapefile',schema=output_schema,crs=layer.crs) as output:for feature in layer:output.write(feature)# 使用示例
convert_gdb_to_shp('input.gdb', './output')

3. GeoPandas

适合数据分析和处理的库:

python

import geopandas as gpd
import osdef convert_gdb_to_shapefiles(gdb_path, output_folder):# 获取GDB中的所有图层layers = gpd.list_layers(gdb_path)for layer_name in layers:# 读取图层gdf = gpd.read_file(gdb_path, layer=layer_name)# 输出路径output_path = os.path.join(output_folder, f"{layer_name}.shp")# 保存为Shapefilegdf.to_file(output_path, driver='ESRI Shapefile')print(f"已转换: {layer_name} -> {output_path}")# 使用示例
convert_gdb_to_shapefiles('input.gdb', './shapefiles')

完整转换脚本

python

import os
from osgeo import ogr
import geopandas as gpdclass GeodatabaseConverter:def __init__(self):self.supported_drivers = {'gdb': 'FileGDB','mdb': 'PGeo','shp': 'ESRI Shapefile'}def convert(self, input_path, output_dir, output_format='shp'):"""转换地理数据库文件Args:input_path: 输入文件路径output_dir: 输出目录output_format: 输出格式 ('shp', 'geojson', etc.)"""# 检测输入文件类型if input_path.endswith('.gdb'):driver_name = self.supported_drivers['gdb']elif input_path.endswith('.mdb'):driver_name = self.supported_drivers['mdb']else:raise ValueError("不支持的输入格式")# 打开数据源driver = ogr.GetDriverByName(driver_name)source_ds = driver.Open(input_path, 0)if source_ds is None:raise Exception(f"无法打开文件: {input_path}")# 创建输出目录os.makedirs(output_dir, exist_ok=True)# 遍历所有图层for i in range(source_ds.GetLayerCount()):layer = source_ds.GetLayer(i)layer_name = layer.GetName()# 输出文件路径output_path = os.path.join(output_dir, f"{layer_name}.{output_format}")# 创建输出数据源out_driver = ogr.GetDriverByName(self.supported_drivers.get(output_format, output_format))if out_driver is None:out_driver = ogr.GetDriverByName('ESRI Shapefile')out_ds = out_driver.CreateDataSource(output_path)out_ds.CopyLayer(layer, layer_name)out_ds = Noneprint(f"转换完成: {layer_name} -> {output_path}")source_ds = None# 使用示例
if __name__ == "__main__":converter = GeodatabaseConverter()# 转换GDBconverter.convert('input.gdb', './output_shapefiles')# 转换MDBconverter.convert('input.mdb', './output_shapefiles')

安装依赖

bash

# 使用conda(推荐)
conda install gdal fiona geopandas# 使用pip
pip install gdal fiona geopandas pyogrio

注意事项

  1. GDAL版本:确保安装正确版本的GDAL,不同版本对GDB/MDB的支持可能不同

  2. 文件路径:GDB是文件夹,MDB是单个文件

  3. 编码问题:中文路径或字段名可能需要特殊处理

  4. 大文件处理:对于大型数据库,建议分批处理

高级功能

python

# 批量处理多个文件
def batch_convert(input_dir, output_dir, file_ext='.gdb'):converter = GeodatabaseConverter()for root, dirs, files in os.walk(input_dir):for item in dirs if file_ext == '.gdb' else files:if item.endswith(file_ext):input_path = os.path.join(root, item)output_subdir = os.path.join(output_dir, os.path.splitext(item)[0])converter.convert(input_path, output_subdir)# 转换坐标系
def convert_with_reprojection(input_path, output_path, target_crs='EPSG:4326'):gdf = gpd.read_file(input_path)gdf = gdf.to_crs(target_crs)gdf.to_file(output_path)

这些工具和方法应该能够满足大多数GDB、MDB到Shapefile的转换需求。根据你的具体需求选择合适的库和方法。

http://www.dtcms.com/a/353997.html

相关文章:

  • 滥用Mybatis一级缓存引发OOM问题
  • 如何使用asyncio库
  • 汽车电气系统的发展演进为测试带来了哪些影响?
  • LangChain4J-(3)-模型参数配置
  • AI生成音乐模型发展现状与前景
  • prettier、eslint、stylelint在项目中使用
  • 理解虚拟 DOM:前端开发中的高效渲染利器
  • Linux操作系统——TCP服务端并发模型
  • Java全栈开发面试实战:从基础到复杂场景的深度解析
  • 【51单片机】【protues仿真】基于51单片机点阵屏系统
  • 全域管控,一触可达:复合机器人远程监控方案重塑智能制造
  • Boosting(提升法)详解
  • Spring Boot + Dubbo 实战教程:打造高性能微服务架构
  • 深度学习12 Reinforcement Learning with Human Feedback
  • openwrt ubus 深入分析
  • C# 字符和字符串
  • 怎么解决大模型幻觉问题
  • 【完全二叉树】 P10990 [蓝桥杯 2023 国 Python A] 彩色二叉树|普及+
  • 车辆识别码vin构成
  • python // 和%区别
  • K8S EFK日志收集全流程实战
  • MySQL数据库精研之旅第十二期:探秘视图,数据库中的 “虚拟表” 魔法
  • stm32 hal库spi dma_tx_rx的几个关键函数执行过程jlink trace分析
  • Rust 登堂 之 迭代器Iterator(三)
  • 如何构建灵活、可控、可扩展的多云网络底座
  • 【高级机器学习】1. Hypothesis 与 Objective Function
  • solidworks2024保姆级安装教程及解锁版安装包下载!
  • 【编号478】美国土地利用数据本土、阿拉斯加、夏威夷岛土地利用数据
  • 蛋白质残基 - 残基距离计算:单蛋白工具与批量处理方案
  • 【目标检测】论文阅读5