I want to create polygons based on two separate text files:
- the first one (as parents) lists the Polygon ID (ID_el) and three to four Points ID (IDpt1, IDpt2, IDpt3..)
- the second contains the Points ID and their xyz coordinates (RW, HW,HH).
I want for each ID in the first file to set an Array (3 to 4 rows) of the coordinates in the second file
I found different scripts to do some of the parts of the process, but I really need help to figure out how to get the Points ID and coordinates for each polygon into an Array, keeping also the attributes of the parent element (ElTyp, ID_el).
Here is the bit of code I used to extract these 2 text files:
with open(arcpy.GetParameterAsText (0),'r') as f1:
with open(r'Nodes.txt', 'a') as f2:
f2.write("ID RW HW HH" + "\n")
for line in f1:
if line.startswith("ND"):
f2.write(line.replace("ND", "").lstrip())
with open (arcpy.GetParameterAsText (0),'r') as f1:
with open (r'Polygonen.txt', 'a') as f3:
f3.write("ELtyp ID_el IDpt1 IDpt2 IDpt3 IDpt4 Mat" + "\n")
for line in f1:
coord = " ".join(line.split() [0:-1])
mat = " ".join(line.split() [-1:])
if line.startswith ("E3T"):
f3.write ((coord.lstrip())+ " " + mat+ "\n")
elif line.startswith ("E4Q"):
f3.write(line.lstrip())
Answer
A similar approach to what has Erica published, but with more details:
- Export text files to a file geodatabase table (useful because you will get
0
for the vertex ID for those polygons which have only 3 points - thus no need to handle this later on); - 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)
No comments:
Post a Comment