Module 6: Working Geometries

*Click image to enlarge* 



  • Start → Begin Script
  • Setup → Import modules, set workspace and overwrite output
  • Open TXT File → Open rivers_970680461.txt for writing
  • Create Cursor → SearchCursor on OID@, SHAPE@, NAME
  • Loop Rows → For each feature in cursor
  • Loop Points → For each vertex in geometry
  • Write Line → Write OID, vertex ID, X, Y, name to file
  • Print Line → Print same info to console
  • Cleanup → Close file, delete cursor and row
  • End → Script Complete

    This week’s lab focused on Python scripting with geometry data in IDLE and ArcGIS Pro Notebooks. We used arcpy.da.SearchCursor() to read features from a shapefile and practiced specifically choosing vertex coordinates within each feature’s geometry. The goal was to extract each vertex’s X and Y location along with its associated object ID and name and write this structured information to a .txt file.

    One big takeaway was working with nested loops to access geometry parts. Iterating through each row in the shapefile and then through each vertex allowed us to record and print spatial information accurately. It also highlighted the importance of geometry-aware scripting when working with polyline features like rivers in rivers.shp.

    Additional attention was given to formatting output properly. Writing five-part lines to a text file required careful use of type conversions and string concatenation. Printing each line during the script helped confirm that the data structure was correct and made debugging easier. The results are evident in the first image posted above. 

Some notes from this lab include:

  • The “NAME” field doesn’t need an “@” symbol since it is a user-defined attribute and not geometry data.
  • Not having the “NAME” field defined properly as in the previous note also throws an error. Without it properly defined, the row tuple didn’t have three elements as expected.
  • Following the spacing (indentations) on the template script was rough. I constantly had to go back and re-look at the indentations throughout the script.
  • Always have to be signed into ArcGis in order to use imported modules.
  • Nested loops are not that complicated, but remembering the syntax and indentations is something I really need to work on.
  • I understand what I was doing when creating and accessing a .txt file, but I could really use more repetition to really nail it down. 
  • Use getPart(0) to access each vertex in a polyline feature’s geometry.
  • Always close your output file after writing to prevent data loss.
  • Printing output during each iteration helps script behavior.
  • Clean up your cursor and row objects after loops to free memory.

Comments

Popular Posts