【问题标题】:How to create multiple polygons using text-based coordinates?如何使用基于文本的坐标创建多个多边形?
【发布时间】: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 字段,但看起来几何图形并没有被覆盖。

【问题讨论】:

    标签: arrays text polygon arcpy


    【解决方案1】:

    您的脚本在我的环境中运行,从两个 txt 文件 poligono1.txt 开始:

    X   Y
    -46,9   -23,3
    -46,3   -23,3
    -46,3   -24
    -46,9   -24
    

    还有poligono2.txt:

    X   Y
    -52,8   -22,1
    -52,1   -22,1
    -52,1   -22,7
    -52,8   -22,7
    

    我用列中的预期信息创建了两个多边形:

    这是指向我的文件的脚本:

    #setting up my functionality and my environment
    import arcpy
    from arcpy import env
    env.workspace = r'C:\Teste\createPolygons'
    env.overwriteOutput = 1
    env.qualifiedFieldNames = "UNQUALIFIED"
    arcpy.CheckOutExtension("Spatial")
    
    #figuring textfiles that need to be managed
    import os, glob
    os.chdir(r'C:\Teste\createPolygons\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 coordinates, 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 coordinates [x, y]
            xs=[]
            ys=[]
            array= arcpy.Array() #the array is created beforehand and added to later
            #two separate 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 array feature - can be displayed using list(shape)
            #these are the two fields 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
    

    您可以在您的文件夹中搜索您的文件并查找任何异常情况,例如文件夹安全访问等。因为您的代码有效。

    P.S.:我有一个小窍门,生成一个点特征中的所有点,看看它们是否相干形成多边形。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-01
      • 2015-08-30
      • 2013-08-28
      • 2012-07-05
      • 1970-01-01
      • 2021-07-29
      • 2015-08-04
      • 1970-01-01
      相关资源
      最近更新 更多