Wednesday, July 15, 2015

Module 8 - Working with Geometries

The lab tasks in GIS Programming are getting a little more complicated but completing them definitely feels more rewarding. This week's assignment was to take an existing line shapefile and write a script that captures the geometry for each line segment then writes the data to a new text file. The text file needed to include five items for each vertex in the river shapefile: the OID, the vertex ID, the x-coordinate, the y-coordinate, and the river name. I used PythonWin to write the script and verified my results by viewing the original shapefile in ArcMap. 

Here is the psuedocode for the script I wrote:

§Import arcpy module

§ - enable geoprocessing environment file overwriting

§ - set workspace environment/file location to Module8/Data folder

§ - create variable for rivers.shp

§ - search rivers.shp for data using cursor search to find OID, XY coordinates, and river names

§ - create blank text document called rivers.txt and save in Module8/Results folder

§ - use for loop to iterate search through rivers.shp to find row information

-   add variable named vertID to identify the number of parts/vertices in each river

-   use for loop to iterate search through each row to find information about parts/vertices

-  add 1 to vertID variable to count the number of parts in each river

-  use write function to write data line for each stream part to the rivers.txt file (data line consists of OID, vertex number, X coordinate, Y coordinate, river name), include “\n” to separate each line. Note the data line elements are numbered in the order they are listed in the cursor search from earlier, ex. OID = 0
- print data line to verify script progress

§ - close write function

§ - delete row

§ - delete cursor

§  -------

§ - open rivers.txt to verify file was created and data was populated

While writing this script I got stumped two times. The first time was with creating the search cursor to sift through the original shapefile. I could not figure out the proper formatting for the different fields I wanted the search to return. After searching through the textbook and ArcMap's Help Menu I realized the issue was with my spacing and use of brackets/quotations. The next issue took me a lot longer to figure out but was even more simple than the first. I could not figure out how to add a vertex count to my second for loop. I mean I could not figure out how to use the variable I created to increase by one for each part that the loop parsed through. The key to that issue was following the directions and setting the vertex variable to zero in the first loop and then adding one to the variable in the second nested loop. I did a lot of experimenting with that section before the directions and my trials and errors finally clicked in my head.

The results of my script are in the screen capture below. The left column is the OID number (line number) from the shapefile. The second column is the vertex ID that I added within the nested for loops that added a count of vertices in each line. The third and fourth columns list the x and y
coordinates respectively. The last column lists the river name from the shapefile.
Geometries of rivers.shp in a text file



No comments:

Post a Comment