Tuesday, July 14, 2015

GIS Programming: Work with Geometries

Greetings GIS and Python enthusiasts,
      Welcome to this weeks GIS programming excursion delving deeper into geometries. We are looking specifically at referencing and manipulating vector based geometry objects using python. Some of the bigger portions of this assignment involved taking data in a text file, ie coordinate information, and building a feature class from it. Or taking existing features and retrieving their geometry data and writing the information to a text file.  The basic tools to interact with these geometry objects have already been looked at previously, however we are expanding into concepts and items like geometry tokens as shortcuts for accessing the properties of a geometry object, or expanding on previously learned for loops by creating nested for loops to go multiple layers deep into a features properties by using a cursor object.
The actual assignment for this week revolved around taking a known polyline feature class and extracting information for each features vertices, ID, name of the feature, etc and writing it to a newly created text file.  Lets look at a combined picture of the outcome and then discuss some of the other specifics.


This is a look at multiple windows active at once containing both the interactive python window, after having the finalized script ran, a snippet of the text files results, and showing the created file itself. Some of the keys that made this script work were the nested for loop that was alluded to above, and specific code to write the output to the file in the manner that is shown. What are nested for loops? These are used to iterate over many different levels of geometry characteristics. Think of a feature as a combination of objects, I can ask a question about the feature as a whole, or perform an action on multiple features with a simple for loop. Lets say I need to look at some of the composite parts that make up a particular feature, like a set of vertices that make up a polyline or polygon, then I need a loop to reference the set of features, and a loop within this one to reference the items making up the feature. These loops within loops are referenced as nested. You can see in the screens above that each feature is shown with its vertices labeled 1 through however many for more simplification.
The hardest part of this lab came not from the nesting, or from getting the vertex information to display, but the simple OID which is the identified for the individual features (first character in the results screen), and the NAME field at the end. The OID, NAME, and a shape token were referenced in a cursor object as a list of features. I tried to use the same input method and code in the cursor variable to have the OID and name written to the text file. Long story short, what I was doing was trying to combine (concatenate) list objects and string objects. Python repeatedly told me no, this isnt possible. My initial flawed troubleshooting was working with brackets because I thought reorganizing these held the key. What I had realistically walked right over was the fact that I had already defined these list object in the cursor variable. I did not need to reuse them when telling python to write them to a text file. I needed to tell python to reference the list I already created and pull the value from there. This was a huge epiphany. Why recreate an object that I already have available for reference. This was the big thing I learned this week working with geometries. Thank you python! 

No comments:

Post a Comment