Terry's GIS Studies and Transition to a New Career

Showing posts with label Pseudocode. Show all posts
Showing posts with label Pseudocode. Show all posts

Sunday, June 21, 2020

Module 6--Working with Geometries

Because of COVID, we were only required to do the last two modules. I completed M7 first, because it seemed easier.

In this module, I had to work with nested loops, search cursors, For Loops, and writing to text files for a system of rivers in Hawaii. The lab was very confusing and you definitely needed to use a pseudocode and to break up the lab into small parts. However, the exercise provided a base of knowledge from which to build upon.

Below are the results of the text file, which returned the Feature ID, Vertex ID, X and Y Coordinate, and the name of the feature:
Screenshot Showing OID, Vertex, X/Y Coordinates, and Name


Below is the pseudocode:


Start
               Import arcpy
               Import env from arcpy
               overwrite output = true
               workspace = “S:\GISProgramming\Module6\Data”
    new text file = “rivers_TJD.txt” (use the write function, “w”)
    fc = “rivers.shp”
               cursor = arcpy.da.SearchCursor(fc, [OID@, SHAPE@, NAME])
               use for loop through the cursor for each point
               vertex = 0
               vertex += 1
               f.write the results to the text file
               print results
               close text file
               delete row
               delete cursor
               print statement: The lab is complete
End


As always, take it one step at a time and look closely at indents, spaces, and other small issues to prevent syntax errors. Ensure you enable the overwrite function so you do not get the error code if you have to start another portion of the script again. Remember to think of the information you write as a table of columns and rows. Of course, the columns are the headers, OID, Vertex, X Coord, Y Coord, Name, etc. The rows are the actual features. Therefore, when you iterate over the loop, you are using {} to identify each column (e.g., {0} will be OID, {1} will be vertex, etc.).

Monday, May 11, 2020

GIS 5103--Programming--Module 1

After what seems like an eternity, classes have started again. This class will teach me how to write script using Python, which is the preferred scripting language for ArcGIS Pro. Though I was introduced to the Integrated Development and Learning Environment (IDLE), this class will use Spyder as the script editor. A word of caution is that if you use a virtual desktop, you must save the your products into the portal. If you were to complete the lab on your personal desktop and then dragged the finished product to the school's portal, there would be errors.

As part of the learning, we were introduced to The Zen of Python, which is a light-hearted set of guidelines for programmers. In a nutshell, the guidelines favor simplicity, utility, and practicality versus nested layers, complexity, etc. These guidelines also ensure Python can remain open source software and that others can add to its body of knowledge. Besides a few inside jokes in Zen, there is another: If you type in "import this" in Spyder, the Zen of Python will populate in Spyder.

The reading assignment also provided the steps to solve a problem for scripting. According to Agarawal et al (2010), I must first identify my inputs, determine the overall goals of the process, and then develop the steps to achieve those goals. Once I complete these steps, I can then write a pseudocode.  For this lab, I created a pseudocode for converting radians to degrees. The pseudocode must begin with Start and finish with End. All the interior lines are indented and completed in order with the variables placed prior to the actual operational function because the script runs in order from top to bottom and left to write just as we read. Once I created the pseudocode, I decided to convert this into Python script using Spyder, which yielded the correct computation. Prior to coding, I could have developed a flowchart, but I did not because it was a very simple computation.

For the lab deliverable requirement, I used Spyder and imported a .py file from the school portal (File>Open>*.py). Once I select the script and ran it (either using the run button or F5), eight folders (with three sub-folders each) populated into my share drive. This was important so that each student had the same folders. All data, scripts, and work will be placed in these folders in the future.

Screenshot of Lab 1 that Utilized Script to Produce 8 Folders in My Student Drive.
Though a little intimidating at first because I have never coded, the lab was very straightforward and there were very few pitfalls. My main pitfall was that I tended to overthink things. I have embedded many of my lessons learned throughout this post. One thing I did differently from the lab was that I went directly to Spyder from ArcGIS Pro instead of using the run option. To me, this was easier and saved a step. Another lesson is that when writing script, ensure that quotation marks are used following the command and be aware that a script must be saved prior to being run (the program will prompt you).