Module 6 - Working with Geometries: Extracting Vertex Coordinates from Line Features

The final module of GIS Programming had us write a script that extracts every vertex from a set of river/stream line features (Hawaii stream data) and writes them out to a text file. One line per vertex, containing the feature's OID, a vertex ID, the X/Y coordinates, and the stream's name.

The core of the script is a nested loop: an outer SearchCursor loop iterates over each river feature (row), and an inner loop walks through every point in that feature's geometry (row[1].getPart(0)), incrementing a vertex counter as it goes. For each vertex, I built a formatted string with the OID, vertex ID, coordinates, and stream name, then wrote it to a text file and printed it to the console for verification. 


I hit one bug worth mentioning: I initially tried to pull the river's name using "NAME@" as the cursor field, which failed. The @ suffix in arcpy cursor fields is reserved for special geometry/identity tokens like "OID@" and "SHAPE@." NAME is a normal attribute field, not one of those tokens, so it just needed to be "NAME" on its own. It was a quick fix once I understood why it failed, instead of just matching a pattern I'd seen elsewhere in the script.

One small habit from this module I've kept since: instead of writing the output string directly into both the write() and print() calls, I assigned it to a single variable (txtFormula) first and used that variable in both places. That way, if I needed to change the output format while debugging, I only had to edit one line instead of keeping two copies in sync.



No comments:

Post a Comment