Skip to main content
1 of 3
Alex Tereshenkov
  • 30k
  • 4
  • 54
  • 119

A similar approach to what has Erica published, but with more details:

import arcpy
import os

folder = "C:\GIS\Temp"
os.chdir(folder)
polygonsFile = "polygons.txt"
verticesFile = "vertices.txt"

outfileGDB = r"C:\GIS\Temp\test.gdb"

if not arcpy.Exists(r"C:\GIS\Temp\test.gdb\polygons"):
    arcpy.TableToTable_conversion(in_rows=polygonsFile,
                                  out_path="C:\GIS\Temp\test.gdb",
                                  out_name="polygons",
                                  where_clause="")

    arcpy.TableToTable_conversion(in_rows=verticesFile,
                                  out_path="C:\GIS\Temp\test.gdb",
                                  out_name="vertices",
                                  where_clause="")

arcpy.env.workspace = outfileGDB
with arcpy.da.SearchCursor("polygons","*") as poly_cur:
    print "polygons"
    poly_dict = {x[1]: x[2:] for x in poly_cur}
    print poly_dict

with arcpy.da.SearchCursor("vertices","*") as vertex_cur:
    print "vertices"
    vertex_dict = {x[1]: x[2:] for x in vertex_cur}
    print vertex_dict
    
polyArray = {}
for polykey in poly_dict:
    polynodes = poly_dict[polykey]
    coordsList = []
    for polynode in polynodes:
        coords = (v for k,v in vertex_dict.iteritems() if k == polynode)
        for coord in coords:
            coordsList.append(coord)
    polyArray[polykey] = coordsList

print polyArray
#>>>polyArray
#>>>{41880: [(22, 24, 25), (13, 16, 17),(12, 14, 15), (19, 20, 21)],
#>>>41879: [(12, 14, 15), (13, 16, 17), (19, 20, 21)]}

features = []
fc = r"C:\GIS\Temp\test.gdb\PolygonFC"

for key,values in polyArray.iteritems():
    features.append((key,arcpy.Polygon(arcpy.Array([arcpy.Point(*value) for value in values]),
                                       arcpy.SpatialReference(4326),True)))

with arcpy.da.InsertCursor(fc,["PolyID","SHAPE@"]) as cur:
    for feature in features:
        cur.insertRow(feature)
Alex Tereshenkov
  • 30k
  • 4
  • 54
  • 119