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.
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