We often must break down shapefiles and feature classes to their most basic level, which is that of the vertices. This can be done with search cursors and nested for loops. The initial for loop will return the features or rows, the second will return the parts of features, and the last will produces the individual points or vertices that make up a line or polygon.
In the exercises, we practiced converting text files into shapefiles. In the assignment, we took a shapefile and populated a new text file with its vertices. Because the feature class consists of several river features, each of which is comprised of a number of points or vertices, this script requires a nested loop structure, in which rows with OID, X and Y coordinates, and feature name are retrieved by the search cursor, and inside that loop, the points are retrieved for each array of each feature. The X and Y coordinates for each vertex are then written to a text file, along with the OID and Name of each feature for the rivers shapefile.
Below, left, is a portion of the resultant text file. Each line includes the feature ID number (OID), vertex ID number within that array, X coordinate, Y coordinate, and feature name.
Besides writing to the text file, I also had my script print out the same data for each vertex, after each iteration of the second (inside) for loop. This way, the progress of the script can be followed as it writes data into the text file. A part of the Results from the Interactive Window is shown on the right of the text file.
Fig.1. Text File with vertex data for river.shp |
Pseudocode.
import arcpy site package
import env module
env = set workspace to …\Data
Set to override old output
fc = river.shp
Search Cursor = retrieve the fields OID, SHAPE@ (all geometry), and NAME for fc shapefile
f = open/create the new text file
Loop 1: for each row in cursor
v = vertex = 0 Create an ID number for each vertex, start over at zero for each OID.
Loop 2: For each row, get arrays (for part in row .getPart )
v +1 add 1 to the vertex number
write each point to textfile as a separate line:
f.write () = strings: row OID + vertex number + X coord + Y coord + row NAME + line break
f.close textfile
No comments:
Post a Comment