arcpy批量转换国土txt为shp(包含对应投影坐标)(Arcpro版本)

之前写过一个arcgis版的脚本:arcpy批量转换国土txt为shp(包含对应投影坐标)_国土报备txt坐标生成shp文件-CSDN博客

缺点很明显

重新写了Arcpro版的,对待中文更加友好,很多原来的缺点都去除。

将脚本文件放到txt所在文件夹,按右键选择第一个可打开文件(前提有装ArcgisPro),按F5运行;或者按第二个直接运行

运行后所在文件夹会生成一个.gdb

下面是源文件,如有想法或交流可私信或Q:七七五九一五零零五

# -*- coding:utf-8 -*-


import arcpy
import os

arcpy.env.overwriteOutput = True


txt_folder = os.getcwd()
txts=os.listdir(txt_folder)  
txts=[i for i in txts if i.endswith("txt") and \
not i.startswith("~$")]
arcpy.CreateFileGDB_management(txt_folder, "fGDB.gdb","10.0")
GDB = os.path.join(txt_folder, "fGDB.gdb")
# spr = {分带-带号:WKID},已整理好3度36到39带和6度19到20带,其他带号请自行查阅按格式补充
spr = {"3-36": 4524, "3-37": 4525, "3-38": 4526, "3-39": 4527, "6-19": 4497, "6-20": 4498}
for txt in txts:
    print(str(txt))
    try:
        # if txt[-3:].lower() == "txt":
        # print(str(txt))
        name = os.path.splitext(txt)[0]
        txt_p = os.path.join(txt_folder, txt)
        print(str(txt_p))


        fffs = open(txt_p, "r",encoding = "utf-8")
        input_data = fffs.readlines()
        print(input_data)
        fffs.close()

        n = -1
        list1 = []

        for i1 in input_data:
            n += 1
            if "@" in i1:
                list1.append(n)
            if "几度分带" in i1:
                sprfd = i1[-2]
                print(sprfd)
            if "带号" in i1:
                sprdh = i1[-3:-1]
                print(sprdh)

        sp = spr[str(sprfd) + "-" +str(sprdh)]
        print(sp)
        # print(list1)
        list1.append(len(input_data))
        # print(list1)
        list2 = [list1[i:i + 2] for i in range(0, len(list1), 1)]
        list2.pop(-1)

        # print(list2)
        """list = [x+1 for x in list]
        print(list)"""

        createFC = arcpy.CreateFeatureclass_management(GDB, "T"+ name, "POLYGON", spatial_reference = arcpy.SpatialReference(sp))
        # createFC = arcpy.CreateFeatureclass_management(txt_folder, name+".shp", "POLYGON") arcpy.SpatialReference(32145) , spatial_reference = arcpy.SpatialReference(sp)

        arcpy.AddField_management(createFC, "界址点数", "LONG")
        arcpy.AddField_management(createFC, "地块面积", "FLOAT")
        arcpy.AddField_management(createFC, "地块编号", "TEXT", 254)

        arcpy.AddField_management(createFC, "地块名称", "TEXT", 254)
        arcpy.AddField_management(createFC, "图幅号", "TEXT", 254)

        arcpy.AddField_management(createFC, "地块用途", "TEXT", 254)
        arcpy.AddField_management(createFC, "图形类型", "TEXT", 254)
        arcpy.AddField_management(createFC, "地类编码", "TEXT", 254)

        # cursor = arcpy.da.InsertCursor(createFC, ["SHAPE@"])
        cursor = arcpy.da.InsertCursor(createFC, (
        "SHAPE@", "界址点数", "地块面积", "地块编号", "地块名称", "图幅号", "地块用途", "图形类型", "地类编码"))
        for i2 in list2:

            a = i2.pop(0)
            b = i2.pop(0)
            # print(input_data[a:b])
            ab = input_data[a:b]
            ab1 = ab.pop(0)  # 提取每条属性首行
            ab2 = ab1[:-1].split(",")  # 每条属性首行划分
            # print(ab1)
            # print(ab2)
            # print(ab)
            if ab2[0] == '':
                jzds = 0
            if ab2[0] != '':
                jzds = ab2[0]
            if ab2[1] == '':
                dkmj = 0
            if ab2[1] != '':
                dkmj = ab2[1]
            if ab2[2] == '':
                bh = ''
            if ab2[2] != '':
                bh = ab2[2]
            if ab2[3] == '':
                mc = ''
            if ab2[3] != '':
                mc = ab2[3]
            if ab2[4] == '':
                lx = ''
            if ab2[4] != '':
                lx = ab2[4]
            if ab2[5] == '':
                tfh = ''
            if ab2[5] != '':
                tfh = ab2[5]
            if ab2[6] == '':
                yt = ''
            if ab2[6] != '':
                yt = ab2[6]
            if ab2[7] != '':
                dlbm = ab2[7]
            if ab2[7] == '':
                dlbm = ''
            list3 = []
            n1 = 0
            ar1 = arcpy.Array()
            ar2 = arcpy.Array()
            whArray = arcpy.Array()
            for i3 in ab:  # 坐标处理
                n1 += 1
                part = i3.split(",")
                print(part)
                if n1 == 1:
                    list3.append(part[1])
                if part[1] in list3:
                    ar1.add(arcpy.Point(part[3][:-1], part[2]))

                # print(part[3][:-1])

                else:
                    ar2.add(arcpy.Point(part[3][:-1], part[2]))

            whArray.append(ar1)
            whArray.append(ar2)
            features2 = arcpy.Polygon(whArray)


            cursor.insertRow((features2, jzds, dkmj, bh, mc, tfh, yt, lx, dlbm))
        del cursor
    except:
        print(arcpy.GetMessages())




  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值