Module 5 - Working with Geometries
This week's laboratory assignment involved writing a code that creates a blank txt file and a Search Cursor that runs through all River shapefile geometries. This data from the cursor is then populated into the txt file using a series of nested for loops and is printed to ensure it is accurate and includes all required fields. I enjoyed working through this lab assignment and have become skilled at determining what an error code is trying to tell me and how to resolve my code to allow it to run properly. Below I have included the Pseudocode and flowchart for the script I wrote for this lab assignment, both of which highlight the basic elements of the raw code. I also included screenshots of the output generated in the Spyder IDE for this code as well as the txt output file. The script output and txt file matching ensures that the txt file was successfully written and that my arguments were correct.
This week’s lab involved the usage of a nested for loop, in which I was previously fairly unfamiliar. I found it important to ensure that the indentation of these for loops was logical and had absolutely no errors. If there were any extra spaces of indents in the for loops, the code would not run properly, as the IDE cannot distinguish which code belongs in the for loop and doesn’t.
The understanding of indexes is vital when calling certain fields from created lists, tuples, etc. Thus, I have learned that when a certain field is wanted (in this case, OID, name, coordinates), I should think logically about at which index that field (or in this case, row) lies. For example, when adding the ‘NAME’ field to the output argument, I had to ensure I used [2] as the index in the argument, otherwise the name of the river would not show
The tokens used in this script created a new dimension to spatial data that I will certainly use in the future. Simply calling a row on its own will generate the basic element of that specified row, but adding the ‘@,’ ‘@XY,’ and so forth allow for the program to call upon certain nested elements of the data. If the @ token was not added to the end of the OID and SHAPE fields, they would not generate as these elements were not on the “surface layer” data rows listed in the ArcGIS Pro attribute tables for the Rivers shapefile.
Working with Geometries Script Pseudocode
Start
Import arcpy and env
Set workspace to Module 5 Data folder
Create txt file and open using open() method
Assign variable to rivers shape file
Assign variables/tokens to required fields
Create Search Cursor
For each row in the searchCursor:
For point in row getPart():
Add 1 to Vertex ID
Write features to created txt file
Print txt output to ensure formatting/arguments are correct
Close txt file
Delete row
Delete Search Cursor
End