GIS Programming Module 6: Working with Geometries
The assignment for Module 6 involved reading the geometry of a feature class and writing the vertices of each feature on the screen and to a text file. This required the use of a search cursor to get all the features. Several loops were used to iterate through all the point objects. Both the screen and file outputs contain a line with the feature ID, vertex ID, X coordinate, Y coordinate, and the name of the river feature.
Below is a screen shote of the screen output that was produced by the script.
Screen of text file that was written by the script.
I used IDLE to write the
script for the module 6 assignment. A template for the script was provided that
had comments that assisted in the placement of the code needed, including the
loops that were required to iterate through the feature class.
The assignment provided the input rivers shapefile which is a feature class that has all single part features. The script will not work for a feature class that had multipart features since we are using the getPart() method of the geometry object. A third iteration would be required to allow for multipart features. Below is a sample of code that would iterate through all parts of a feature.
cursor = arcpy.da.SearchCursor(fc, ["OID@", "SHAPE@"])
for row
in cursor:
print ("Feature {0}:
".format(row[0]))
partnum = 0
for part in row[1]:
print ("Part {0}:
".format(partnum))
for point in part:
print ("{0},
{1}".format(point.X, point.Y))
print ()
One of the challenges of the assignment was getting the outputs to have the correct values and look professional. I utilized the left-adjust (<) in the format method of the string to make sure all the columns in the output were lined up. The end of line escape character (“\n”) was added to the string in the write statement to make sure all the vertices were on separate lines. I also added the end of line escape character after each feature to give a separation between features.
Pseudocode
Enable overwrite output.
Set input and output variables.
Open output file for write.
Create SearchCursor containing input feature class.
Loop through each row of feature class.
Print feature ID and name
Set vertex number to 0.
Loop through each point in the row
Add 1 to vertex number.
Write line to output file.
Print line to screen.
Print blank line.
Write next line to output file.
Close all files.
Flow Chart
Comments
Post a Comment