arcpy提取文本文件中坐标
程序员文章站
2022-07-01 20:54:22
...
arcpy提取文本文件中坐标
# _*_ coding: utf-8 _*_
import arcpy
import os
#提取坐标,转为shp,定义投影
def getTxtLine(txtpath):
file = open(txtpath,'r')#以指定的方式打开文本
lines_arr = file.readlines()#得到文本文件的每一行组成的列表
file.close()#关闭文件
return lines_arr
#定义分割文本每一行的方法
def splitMetho(instr):
newArray = instr.split(';')
new_linesArray = [int(newArray[0]),int(newArray[1]),[float(newArray[2]),float(newArray[3])]]
return new_linesArray
#定义得到不同线列表的方法
def getPolylineXY(linesArr):
FID_xyArr = [splitMetho(item) for item in linesArr[1:]]#FID、ID、XY
ID_Arr = [item[1] for item in FID_xyArr]#ID的列表
xyArr = [item[2] for item in FID_xyArr]#XY的列表
start_IdArr = [item for item in enumerate(ID_Arr )]#ID为零的地方就是开始的地方 enumerate(list)同时列出列表中的数据和下标
start_InArr = [item[0] for item in start_IdArr if item[1]==0]#得到为零的位置的索引
allPolyline = [xyArr[start_InArr[i]:start_InArr[i+1]]for i in range(0,len(start_InArr)-1)]
return allPolyline
'''
list_a = [0,1,2,0,1,2,3,0,0,1,]
l = (list(enumerate(list_a)))
print l
[(0, 0), (1, 1), (2, 2), (3, 0), (4, 1), (5, 2), (6, 3), (7, 0), (8, 0), (9, 1)]
ll = [item[0] for item in l if item[1]==0]
print ll
[0, 3, 7, 8]
'''
def metho(txtpath):
linesArray = getTxtLine(txtpath)
return getPolylineXY(linesArray)
if __name__ == "__main__":
polylinepath = "D:/hengshui_railway.txt"
polygonArray = metho(polylinepath)
print polygonArray[0]
print "Done!"