之前写过一个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())