- Export text files to a file geodatabase table;
- Convert those tables into dictionaries;
polygons {41880: (26287, 26286, 26748, 26747), 41879: (26748, 26286,
26747, 0)}
vertices {26747: (19, 20, 21), 26748: (12, 14, 15), 26286: (13, 16,
17), 26287: (22, 24, 25)}
- Building an array of polygons (each has points with XYZ values);
- Pre-create the polygon feature class with the Z coordinates stored (important); doable with the GP tool and adding the PolyID field.
- When loading polygons, important to specify the has_z attribute, otherwise it won't store Z-coordinates. You can see the Z-value for every vertex of the polygon when in the Editing session and having the Edit Sketch Properties window open (while editing the polygon with vertices shown);
- As the last step, use the Join Field GP tool to transfer all other attribute fields from the Polygons file GDB table (based on the PolyID). I didn't want to take them into the dictionary just to keep things more clear.
Useful Esri Help links:
Polygon class; Writing geometries; da.Insert cursor
The ready-to-use code:
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))) #True - for has_z parameter
with arcpy.da.InsertCursor(fc,["PolyID","SHAPE@"]) as cur:
for feature in features:
cur.insertRow(feature)