Working with Geometries - Module 6

 This week's lab had us working with geometry objects within the arcpy module. I was presented with a polyline shapefile of rivers in Hawaii and were tasked with taking specific attribute information, in this instance fields within an attribute table, and export the data to a .txt file. To do this, I had to utilize geometry objects with a searchCursor function. Geometry objects are processes within ArcGIS which allow us to iterate certain data within the attributes of a feature. In this instance I needed to pull the object ID field data, the geomtry data (coordinates for vertices which constitue each part of each feature). I used the "OID@" and "SHAPE@" geommtery objects which iterate the ID and shapefile fields then added the "NAME" field.

In my code, I imported arcpy and os to allow access to ArcGIS and operating system functions and proceeded to set the environment and workspace to my data folder where the rivers.shp resided. To create the new file, I set a variable to open (create) the new file and for it to reside in the results file. I ensured the file was writable.  A searchCursor was then added which contained my geometry objects and fields. An 'if loop' was added, printing the feature ID and adding a counter for the upcoming part number iterating. I added another 'if loop' using the .getPart function to pull our feature part number and its subsequent coordinates using .format functions. They were then printed. In order to ease the amount of writing within my upcoming write() function (and if I wanted to use these in functions later) I asribed the functions that called my iterations to variables. These were OID, VID, COORD, and NAME. 

This part took me some time to figure out as I had not used the write() function previously. I did not want the write function to look too untidy so, as before mentioned, I ascribed the .format functions I used to call data to variables. I then listed thse variables within a string adding spaces after each. When I printed the information that I pulled from the iterations, for the feature ID and part ID, I inserted a string denoting each with "Feature:" and "Part:" respectively to ensure my data printed in an organized  and understandable syntax. 

I then closed the file and deleted the row being iterated and deleted the cursor. The .txt file was created and the data written over. The .txt file that I created could also have been created in a simpler manner taking up less space in case this data needed to be printed out in a condensed form. What I could have done was take my .format functions for my interated data and place them into the write function as one long series of strings. Then repeat the same function for a print function. The data table would be far tighter and in one line. This was a trick I learned from my data after noting that the design of my script, though logical, felt too verbose. 

Below is a screenshot of one page of my .txt file:


Flowchart of the process for accomplishing this task:



Comments