This week brings us to our sixth module in the GIS Programming course, which is also our final assignment for this course.
This week focused on working with geometries within ArcPy and how to effectively read and write them in ArcGIS Pro. We learned to use geometry tokens for quick access to feature properties and the use of search cursors.
In our exercise, we had fun extracting XY coordinates from point and polyline features, which helped us understand how to work with individual vertices. Additionally, we explored single-part and multipart features using the isMultipart property and practiced creating new polyline features from a list of coordinates.
Our lab assignment required us to write a script that uses ArcPy to extract vertex information from a shapefile. The script initializes a SearchCursor to retrieve Object IDs, shapes, and names of river segments, counting and outputting the coordinates of each vertex. It also creates a text file and writes the results to that file. The screenshot above shows the IDLE window after running the script, displaying the output.
The pseudocode for my script is below:
START
Configure environment:
SET arcpy.env.workspace to "S:/GISProgramming/Module6/Data"
SET arcpy.env.overwriteOutput to TRUE
Define feature class:
SET fc = "rivers.shp"
Open output text file:
PRINT "Opening output text file" message
OPEN output_txt FOR WRITING "S:/GISProgramming/Module6/Results/rivers_akj39.txt"
Create search cursor for rivers layer:
SET cursor = call SearchCursor(fc, ["OID@", "SHAPE@", "NAME"])
Initialize vertex number:
SET vertex_num = 0
Iterate through rows in cursor:
CREATE FOR loop for each row in cursor
SET oid = row[0]
SET shape = row[1]
SET name = row[2]
Iterate through parts of the shape:
CREATE FOR loop for each part in shape.getPart()
Iterate through points in the part
CREATE FOR loop for each point in part
INCREMENT vertex_num
SET output_line = STRING(oid) + " " + STRING(vertex_num) + " " + STRING(point.X) + " " + STRING(point.Y) + " " + name + "\n"
WRITE output_line TO output_tx
PRINT output_line
END FOR loop
END FOR loop
END FOR loop
Clean Up
del cursor
CLOSE output_txt
PRINT "Processing complete"
END
No comments:
Post a Comment