目前仅针对utf-8编码的txt文件,如果不是可使用批量将ANSIgbk文本txt文件转换成UTF-8编码格式_文本批量转换ansi,txt文件ansi转utf8-VB工具类资源-CSDN下载
链接的工具进行转换(亲测可用)(如果有更好的方法欢迎指正)
编码参考了一些其他前辈的代码,自行编制部分尚待优化,有一些问题尚未得到解决,目前仅通过pycharm软件的调试(不用运行)功能进行成功运行(使用python2.7.3环境),亲测可用,有需要的同行可参考使用,本人还在学习阶段,有疑问和指正建议欢迎一起交流,QQ:775915005,同时也欢迎转载
# -*- coding:utf-8 -*-
import arcpy
import os
arcpy.env.overwriteOutput = True
import sys
reload(sys)
sys.setdefaultencoding("utf-8")
txt_folder = ur"E:\规范性文件\新建文件夹 (2) - 副本" # 修改坐标txt所在文件夹目录
txts = os.listdir(txt_folder)
# 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:
try:
name = os.path.splitext(txt)[0]
txt_p = os.path.join(txt_folder, txt)
fffs = open(txt_p, "r")
input_data = fffs.readlines()
fffs.close()
n = -1
list1 = []
for i1 in input_data:
n += 1
if "@" in i1:
list1.append(n)
if u"几度分带".encode("utf-8") in i1:
sprfd = i1[-2]
print(sprfd)
if u"带号".encode("utf-8") in i1:
sprdh = i1[-3:-1]
print(sprdh)
sp = spr[str(sprfd) + "-" +str(sprdh)]
list1.append(len(input_data))
list2 = [list1[i:i + 2] for i in range(0, len(list1), 1)]
list2.pop(-1)
createFC = arcpy.CreateFeatureclass_management(txt_folder, name+".shp", "POLYGON", spatial_reference = arcpy.SpatialReference(sp))
arcpy.AddField_management(createFC, u"界址点数", "LONG")
arcpy.AddField_management(createFC, u"地块面积", "FLOAT")
arcpy.AddField_management(createFC, u"地块编号", "TEXT", 254)
arcpy.AddField_management(createFC, u"地块名称", "TEXT", 254)
arcpy.AddField_management(createFC, u"图幅号", "TEXT", 254)
arcpy.AddField_management(createFC, u"地块用途", "TEXT", 254)
arcpy.AddField_management(createFC, u"图形类型", "TEXT", 254)
arcpy.AddField_management(createFC, u"地类编码", "TEXT", 254)
cursor = arcpy.da.InsertCursor(createFC, (
"SHAPE@", u"界址点数", u"地块面积", u"地块编号", u"地块名称", u"图幅号", u"地块用途", u"图形类型", u"地类编码"))
for i2 in list2:
a = i2.pop(0)
b = i2.pop(0)
ab = input_data[a:b]
ab1 = ab.pop(0) # 提取每条属性首行
ab2 = ab1[:-1].split(",") # 每条属性首行划分
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]))
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()
20221108更新