Arcpy读取CAD软件dwg、dxf等格式数据,转换geojson

Arcpy读取CAD软件dwg、dxf等格式数据,转换geojson

code

import os
import sys
import uuid
import arcpy

# 获取当前脚本的物理路径
def getScriptDirectory():
    script_path = os.path.abspath(__file__)
    script_directory = os.path.dirname(script_path)
    script_directory = script_directory.replace('\\', '/')
    c= script_directory[-1]
    if script_directory[-1] != '/':
        script_directory += '/'
    return script_directory

# 检查目录,若不存在则创建
def checkDir(dir):
    if not os.path.exists(dir):
        os.makedirs(dir)

# 工作目录
ws = getScriptDirectory()
uid = str(uuid.uuid4())
# 临时目录
tmp_path = ws + 'tmp/' + uid + '/'
checkDir(tmp_path)
out_gdb_name = 'out.gdb'
out_gdb_path = tmp_path + out_gdb_name

def dwg2gdb(input_dwg_file, out_gdb_path, spatial_reference):
    arcpy.CreateFileGDB_management(tmp_path, out_gdb_name)
    ds = arcpy.conversion.CADToGeodatabase(input_dwg_file, out_gdb_path, 'out_ds', 1000, spatial_reference)
    return str(ds).replace('\\', '/')

def filter_layer(dataset, out_gdb):
    polygon_classes = dataset + '/Polygon'
    f1 = 'Layer = \'拆迁房屋\''
    f2 = 'Layer = \'临迁房屋\''
    f3 = 'Layer = \'临时借地\''
    f4 = 'Layer = \'永久征地\''
    out_f1 = 'chai_qian_fang_wu'
    out_f2 = 'lin_qian_fang_wu'
    out_f3 = 'lin_shi_jie_di'
    out_f4 = 'yong_jiu_zheng_di'
    arcpy.conversion.FeatureClassToFeatureClass(polygon_classes, out_gdb, out_f1, f1)
    arcpy.conversion.FeatureClassToFeatureClass(polygon_classes, out_gdb, out_f2, f2)
    arcpy.conversion.FeatureClassToFeatureClass(polygon_classes, out_gdb, out_f3, f3)
    arcpy.conversion.FeatureClassToFeatureClass(polygon_classes, out_gdb, out_f4, f4)
    out_classes = [
        out_gdb + '/' + out_f1,
        out_gdb + '/' + out_f2,
        out_gdb + '/' + out_f3,
        out_gdb + '/' + out_f4,
    ]
    return out_classes

def dwg2geojson(input_dwg_file, out_path, dwg_sr):
    ds = dwg2gdb(input_dwg_file, out_gdb_path, dwg_sr)
    out_classes = filter_layer(ds, out_gdb_path)
    checkDir(out_path)
    all_success = True
    for fc in out_classes:
        fc_name = os.path.basename(fc)
        out_json_file = out_path + fc_name + '.geojson'
        try:
            arcpy.conversion.FeaturesToJSON(fc, out_json_file, 'FORMATTED', 'NO_Z_VALUES', 'NO_M_VALUES', 'GEOJSON', 'KEEP_INPUT_SR', 'USE_FIELD_NAME')
            print(out_json_file)
        except Exception as ex:
            all_success = False
            print('fail:' + str(ex))
    if all_success:
        print('complete')
    else:
        print('error')

if __name__ == "__main__":
    # dwg_file = sys.argv[1]
    dwg_file = r'D:\file\production\Py\data\dwg\testdwg.dwg'
    sr = arcpy.SpatialReference(4326)
    dwg2geojson(dwg_file, tmp_path, sr)

output

image-20240409112203457

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
arcpyArcGIS中的一个Python库,可用于进行地理数据处理和分析。使用arcpy可以方便地实现数据格式之间的转换。 在arcpy中,可以使用工具函数来进行数据格式转换。以下是一些常用的数据格式转换函数: 1. FeatureClassToFeatureClass:用于将一个要素类(feature class)转换为另一种要素类格式。可以将矢量数据格式(如Shapefile)转换为其他矢量数据格式(如文件地理数据库或KML)。 2. FeatureClassToShapefile:用于将要素类转换为Shapefile格式。Shapefile是一种常见的矢量数据格式,可以在多个GIS软件中使用。 3. RasterToOtherFormat:用于将栅格数据转换为其他栅格数据格式,如TIFF、JPEG或GRID格式。这个函数可以根据不同的需求选择合适的输出格式。 4. TableToTable:用于将表格数据(如Excel电子表格或数据库表)转换ArcGIS可识别的表格数据格式。 5. FeatureClassToGeodatabase:用于将要素类(feature class)转换文件地理数据格式文件地理数据库是一种可以在ArcGIS中进行编辑和查询的数据存储格式。 上述只是其中一些常见的数据格式转换函数,在arcpy中还有很多其他函数可供使用。使用这些函数,可以根据需要对数据进行格式转换,并根据输出要求选择合适的格式。 总之,通过arcpy库中的函数,可以方便地实现数据格式之间的转换,从而提高地理数据处理和分析的效率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

WineMonk

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值