Wednesday, July 19, 2017

GIS Programming Module 8: Working With Geometries

This week we learned how to use Python to access geometric elements of features, like their shape type, coordinates, and length/area. For the assignment, we were tasked with writing a script that would iterate over all parts of all features in a shapefile and write the coordinates of each vertex, along with ID numbers and the name of the feature, to a separate text file (created within the script).

The first step is to create a search cursor to find the necessary data > then create a writable text file > then loop over each feature > and for each part of each feature, print the vertex coordinates and other data to both the text file and the interactive window.

Here's the pseudocode I wrote to show how the script works:

Cursor = OID, Shape, Name in rivers.shp
Text = rivers.txt
For row in cursor:
     Vertex = 0
    For point in row
        Vertex + 1
        Write to text OID, Vertex, X and Y coordinates, and Name
        Print OID Vertex, X and Y coordinates, and Name

And here's part of the text file my script produced:


For me, this was definitely the hardest assignment yet, but I was really looking forward to this lesson and I'm glad to be starting to understand how manipulating geometries works.

Wednesday, July 12, 2017

GIS Programming Module 7: Manipulating Spatial Data

This week we tackled some new data structures and the use of cursors to find and edit data in tables. The script for this week's assignment had a number of steps:

create a new geodatabase > create a variable containing a list of all the feature classes in the module data folder > copy each shapefile from the module data folder to the database > create a search cursor that retrieves name, feature, and population for only the county seats from the "cities" shapefile > create an empty dictionary > add the name and population of each city to the dictionary > print the dictionary

It also prints messages at each step to let the user know what's happening and whether the script is running correctly, as you can see below in a screenshot of part of my script's output:


The hardest part for me this week was writing the search cursor correctly and then the right code for adding data to the dictionary. At one point after sorting out the latter, I was still getting mysterious error messages (I was running lines of code in the interactive window as I went so I could see if they worked before writing them into the standalone script) and it turned out I still had a problem with the search cursor. It's always syntax issues for me!