【发布时间】:2019-09-15 20:59:29
【问题描述】:
我正在尝试遍历 3 个包含 XY 坐标的文本文件。我需要读取坐标并使用它们在一个循环中构建多边形特征。此外,我需要添加两个描述多边形的字段。我可以使用新字段创建 3 个表,但在循环结束时看不到多边形。似乎我无法 arcpy.Polygon 我创建的点数组,或者表示多边形对象的变量没有被放置在我需要它去的地方。
我能够成功打开读取并解析每个文件中的文本行。我将 XY 坐标定义为点并将这些点添加到数组中。从那组点中,我尝试制作一个多边形特征。使用 insert cursur 我定义了我希望写入的字段,然后使用 insert Rows。如果我打印出包含多边形几何的列表(多边形),则坐标存在,我只是不知道为什么它没有被写入 shapefile。
#setting up my functionality and my environment
import arcpy
from arcpy import env
env.workspace = r'G:\Fall19\program\lab2\data\data'
env.overwriteOutput = 1
env.qualifiedFieldNames = "UNQUALIFIED"
arcpy.CheckOutExtension("Spatial")
#figuring the the textfiles that need to be managed
import os, glob
os.chdir(r'G:\Fall19\program\lab2\data\data\districts')
text_file=[i for i in glob.glob("*.txt")]
text_file
#creating a new shapefile that is ready to be imported into
poly=arcpy.CreateFeatureclass_management(env.workspace, "districts.shp",
"POLYGON")
#adding the two extra fields needed by the instructions
arcpy.AddField_management(poly, "vert_count", "DOUBLE", 100, "", "", "",
"NULLABLE", "REQUIRED")
arcpy.AddField_management(poly, "dist_id", "DOUBLE", 100, "", "", "",
"NULLABLE", "REQUIRED")
#the single loop function that will open the files, pull the coordintes,
convert the coordinates to points
#create a polygon from the points and write to the new shapefile created
above.
for t in range(len(text_file)):
with open(text_file[t]) as text:
lines= text.readlines()
list_coord=[i.split('\t') for i in lines]
list_coord.remove(['X', 'Y\n']) #i now have a nested lists of
coordinated [x, y]
xs=[]
ys=[]
array= arcpy.Array() #the array is created beforehand and added
to later
#two seperate lists of x and y coordinates will be used later
with the arcpy.point function
for coord in list_coord:
xs.append(coord[0])
ys.append(coord[1])
for i in range(len(list_coord)):
point= arcpy.Point(xs[i], ys[i]) #the xy coordinates now
created a point feature which then gets added to the array above
array.add(point)
shape= arcpy.Polygon(array) #this should be creating polygon
geometry from the aray feature can be displayed using list(shape)
#these are the two fiends that also need to be written into the
shapefile
vert_count=len(list_coord)
dist_id= text_file[t][-5]
#i believe this is where i am having issues as the count and id
get written over, but there seems to be no geometry
cursor = arcpy.da.InsertCursor(poly,['Shape@', 'vert_count',
'dist_id'])
cursor.insertRow([shape, vert_count, dist_id])
del cursor
我希望得到 1 个带有 3 个单独多边形的 shapefile。这些多边形中的每一个都将具有唯一的几何形状、坐标数和 ID。我得到了顶点计数和 id 字段,但看起来几何图形并没有被覆盖。
【问题讨论】: