Terry's GIS Studies and Transition to a New Career

Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts

Friday, June 5, 2020

Module 4--Geoprocessing

During Module 4, I learned several tools to conduct geoprocessing functions using Python and Spyder. This was a much more enjoyable module because it allowed me to actually perform mapping functions. In other words, I saw the reason why we learn Python.

Some of the new tools I learned for Python:
--Model Builder, which actually built a model using a click/drag function and then having it perform the function. This is a much more visual method to use the tool.
--Buffering, to include dissolve.
--Clip, selects, erase tools.
--Add XY coordinates to a shapefile.
--Using the GetMessages() function to add commentary about the function performed.
--Make a toolbox.

Again, a few points to remember:
--Be very cautious of spelling (module, especially).
--Ensure capitalization is consistent with syntax.
--Consistency with quotation marks and contractions.
--Ensure URL of environment is accurate (spelling, slashes, etc.).
--Ensure comments section at top is complete (name, date, etc) using the # symbol.

Feedback of Hospital Script: Add XY Coordinates, Buffer,m Buffer Dissolve
The above screenshot displayed the results of the script written to add XY coordinates to the hospital shapefile, add a buffer, and then diffuse that buffer to a single feature. To complete this assignment, I broke the script into three parts, where I tested each script to ensure it worked properly before going to the next script. I also commented out commentary to explain what each step would perform. I used the GetMessages function to return the start time, date, etc.. I then added a print statement (separately) to return commentary on what the function performed.

All in all, this was an enjoyable module and a welcome change from the previous modules.







Monday, May 25, 2020

Module 2: Python Fundamentals

For this week's exercise and assignment, there were numerous operations broken down into four steps. These steps included creating a string variable, creating a list, creating loops and iterating variables, and removing specific variables.

In Step 1, I created a string of my full name,  split the string to separate my individual names, and then used the index function to return a specific name. It is important to note that one uses 0 to return the first name and -1 to return the last portion of a string.

Step 2 required me to determine errors in a random dice game. This required me to first import the random module, which is the first line of code. I then looked at the script provided in the assignment to find two errors. A few things to note in errors: Ensure your quotation marks are not confused with contractions as it will confuse the program and return an error, make sure the case (upper/lower) is consistent, especially when naming a variable, and watch indention.

Step 3 caused me quite a bit of problems, as I overthought everything. In this scenario, I had to create a loop using a while statement and then append numbers to it until I reached 20 separate numbers. Again, this script required me to import the random module as the first step. One lesson I learned is that if you want to break the loop, the break must have the same indention as the loop. Otherwise, Python will return an error. I also learned that there are many different ways to arrive at a break statement.

For Step 4, I used the numbers generated in Step 3 (or I could have added this script to the script in Step 3). I then chose an integer as an unlucky number, used the while loop to remove the unlucky number, and then created a new list. This was fairly straightforward and of no issue. However, the next logical step in the future could be to remove the unlucky number and replace it so that there were still 20 numbers in the list.

Below are the results of my script, which has the return of each script. The explanations are written above, so I will not repeat them.

Script for Module 2. This shows the return items for each step in the assignment as explained above.


Flow Chart for Step 4.


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).