基于python&arcpy的批量CAD/dwg文件转shp/shapefile 您所在的位置:网站首页 arcmap导出cad数据 基于python&arcpy的批量CAD/dwg文件转shp/shapefile

基于python&arcpy的批量CAD/dwg文件转shp/shapefile

2023-09-18 00:58| 来源: 网络整理| 查看: 265

前言

在将多个CAD图纸合并到一张图上,或者实现图形的数据库管理时,往往会面临数据坐标转换和格式转换的问题,当前主流是统一用shapefile或者gdb地理数据库等方式进行管理。 CAD转shapefile有很多种方式,以下方法基于ArcGIS的arcpy工具包进行,可以实现批量CAD文件转gdb或者shp格式。

算法流程

arcpy是ArcGIS的python工具包,如果电脑有安装ArcGIS或者ArcGIS Pro,在默认安装路径下有安装arcpy,python运行的时候选择ArcGIS安装的python程序运行。

ArcGIS提供help文档链接: https://pro.arcgis.com/zh-cn/pro-app/2.6/tool-reference/conversion/cad-to-geodatabase.htm

具体流程是先将CAD文件(格式为dwg)导入创建的地理数据库中,然后将地理数据库的文件导出成shapefile文件,调用的arcpy的主要函数为: CreateFileGDB_management(创建地理数据库) CADToGeodatabase_conversion(CAD导入地理数据库) FeatureClassToShapefile_conversion(导出成shapefile) 在这里插入图片描述

import arcpy import os # 自定义输入文件路径 defaultpath=r"D:\03程序开发小工具" # 输入CAD文件名称 CADname='test' # 定义工作空间 arcpy.env.workspace = defaultpath # CAD文件路径 input_cad_dataset =os.path.join(defaultpath,'cad',CADname+'.dwg') # gdb文件路径 out_gdb_path = os.path.join(defaultpath,'gdb',CADname+'.gdb') # 要素集文件名称 out_dataset_name = CADname # CAD转shp坐标比例 reference_scale = "1" # 先创建一个gdb地理数据库 arcpy.CreateFileGDB_management(os.path.join(defaultpath,'gdb'), CADname+'.gdb') # 将CAD文件导入到gdb地理数据库,新建一个要素集 arcpy.CADToGeodatabase_conversion(input_cad_dataset, out_gdb_path, out_dataset_name, reference_scale) # 切换工作空间到gdb中 arcpy.env.workspace = out_gdb_path # 获取gdb中的文件列表 datasets = arcpy.ListDatasets(feature_type='feature') # 输入shp文件的保存路径 output_shp_path=os.path.join(CADname+defaultpath,'shp') datasets = [''] + datasets if datasets is not None else [] # 获取每个地理数据库中的要素集 for ds in datasets: for fc in arcpy.ListFeatureClasses(feature_dataset=ds): path = os.path.join(arcpy.env.workspace, ds, fc) # print(path) outfc = arcpy.ValidateTableName(fc) # print(outfc) # 将要素集里的要素转为shp文件 arcpy.FeatureClassToShapefile_conversion(outfc, output_shp_path)

至此,就生成了从CAD转shp的代码,但是转换后的shp是没有定义坐标系的,根据CAD的坐标进行坐标系的定义。



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有