Sunday, June 25, 2017

Module 6

Geoprocessing with Python


For Module 6 we were introduced to Geoprocessing with Python which allows for creating script in Python that can be integrated into ArcMap.  The first step is to import ArcPy as the first line of code and set the active workspace. These steps will allow the user to integrate Geoprocessing tools into the script. The tool name and toolbox alias is required to integrate Geoprocessing tools into Python.

Geoprocessing tools can be assessed using two different lines of syntax.

1. arcpy.<toolname_toolboxalias>(<parameters>)
2. arcpy.<toolboxalias>.<toolname>(<parameters>) 

Along with system toolboxes, custom toolboxes can also be accessed in Python, but the custom toolbox must be imported.

This weeks assignment entailed importing ArcPy, adding XY coordinates using the AddXY tool, creating a Buffer with the Buffer tool and Dissolving the buffer into single features using the Dissolve tool. Once each task was performed a message was printed utilizing the GetMessages() function
The screen cap shows the printed message after each Geoprocessing tool was ran.


Flowchart of my script that adds XY coordinates, creates 1000 meter buffer, and dissolves the hospital buffer.


.

Sunday, June 18, 2017

Module 5

Module 5 Geoprocessing in ArcGis


Module 5 introduced the very useful concepts of working with Geoprocessing tools, creating models, and utilizing scripts within ArcGIS. The assignment for this week consisted of creating a toolbox, creating a tool using  ModelBuilder, export a script from ModelBuilder, creating a model-derived script to work outside of ArcMap, and creating a script tool. 

The most useful tool I found was the ability to first create a Model using ModelBuilder in ArcMap and then exporting the model into a script. The script will need several adjustments before it can be ran properly, but using the visual aid of ModelBuilder can help assist in creating a text based script.

Flow chart of ModelBuilder that removes "Not prime farmland"
from the clipped soil shapefile.
The first assignment was creating a model in ModelBuilder. The Clip Tool, Select Tool, and the Erase Tool were utilized. Utilizing these three tools clipped one shapefile into another then selected all soils that did not meet the criteria using a SQL expression. The erased tool was then used to remove the items from the shapefile.

The tool was then exported to a Python script. Several small changes needed to be made in order for the script to run properly. The script was then made into a script tool within ArcToolbox.
Final Result Featured "Not prime farmland" Removed

Tuesday, June 13, 2017

Peer Review 1

"Scripting MODFLOW Model Development Using Python & FloPy" by M. Bakker et al. M. Bakkers et al.



I chose to review the article "Scripting MODFLOW Model Development Using Python & FloPy" by M. Bakker et al. M. Bakkers et al. which discusses the benefits of utilizing Python packages as a tool to construct numerical groundwater flow and transport models. These packages include MODFLOW which is part of a family of codes that has been developed for the construction of numerical groundwater flow and transport models. According to Bakker et al(2016) the article focuses on utilizing FloPy to create new models and work with existing ones. The first example is a generic scenario created by Bakker et. al which created a one dimensional scenario with fixed water levels of two canals. Bakker et al.(2016) provide an eight step scenario for developing a FloPy script which is listed below: 

1. Import the Flo Package
2. Create a MOD FLOW model object
3. Define the model setup, including the discretization, active model cells, starting heads, hydraulic properties, and layer types.
4. Add packages to simulate features of the flow system (e.g., wells, recharge, or rivers).
5. Define the solver that MODFLOW uses to obtain a head solution.  Define what output MODFLOW needs to save.
7. Generate MODFLOW input files and call MODFLOW to obtain a solution.
8. Read the (binary) output files for display and further analysis.

The second example was a real world scenario where Leake et. al. (2010) computed which fraction of pumpage comes from which source in the Upper San Pedro Basin utilizing the eight step scenario created by Bakker et al.(2016).

According to Bakker et al.(2016) the FloPy program in conjunction with the MODFLOW package is relatively easy groundwater flow modeling program for users who are not Python experts. Bakker et al.(2016) states that "All that is required is familiarity with the Python package for the specific task, and some lines of Python code to import and convert the data into a format that FloPy can handle.Once loaded into Python, model results can be written to a large number of formats, including image files, shapefiles, georeferenced images, or NetCDF files.This article sparked my interest because I am interested in looking into the MODFLOW package to utilize at work. Currently the majority of groundwater flow models we use are based on historical data and it would be beneficial to create updated models utilizing the MODFLOW package.

Source:
Bakker, M., Post, V., Langevin, C.D., Hughes, J.D., White, J.T., Starn, J.J., & Fienen, M.N, 
          (2016 January) Scripting MODFLOW Model Development Using Python & FloPY. 
          Retrieved from http://onlinelibrary.wiley.com/doi/10.1111/gwat.1

Link:


Friday, June 9, 2017

Module 4

Module 4: Debugging and Error Handling


For module 4 we were introduced to error handling and debugging. No matter how experienced a programmer is, they are bound to make mistakes, so it is very important to have a good foundation in debugging and error correction.

There are three main types of errors in Python that we were introduced to. They are syntax errors, exceptions, and logic errors. Syntax errors prevent the script from running and are usually spelling, punctuation, and indentation errors. An easy way to check for syntax errors is to use the check button. This will place the cursor where any syntax error occur.

Exceptions are errors that are detected while the script is running. A good error handling technique for exceptions is a try-except statement. This statement does not allow the script to fail and provides the user with usable feedback, such as what kind of error is occurring.

(Part 1 ) 
Logic errors will run but the result is often wrong. In addition to the check button and the try-except statement there is the debugging procedure that can be used to help identify errors line by line.

The assignment for module 4  provided three scripts that contained errors.  Part 1 and Part 2 assignments required error checking methods to correct the script in order for the script to run properly. The check button was utilized as well as running the code and interpreting the error messages. All error messages provide what kind of error occurred and what line the error occurred on. Part 3 did not need to be fixed but modified using the try-except statement to catch any type of exception and print a relevant error message.

(Part 2)

(Part 3)

Tuesday, June 6, 2017

Module 3: Python Fundamentals 2

Python Fundamentals 2

Module 3 was a continuation of Python fundamentals introduced in Module 2. For Module 3 a few basics were introduced such as saving python code as scripts, conditional statements, loop structures, commenting in a scripts, and searching for and correcting script errors.

The Module 3 assignment provided a half completed script to find and edit mistakes as well as complete the code. The final result produced code that used conditional statements and the while loop.

Final Results
In order to run the correct code the random number module needed to be imported.

Once the correct module was imported the following block of code had several errors preventing the script from running. Once the errors were identified and corrected the IF-Then-Else statement was utilized based on the length of players names.

Following the IF-Then-Else statement a while loop was created that added 20 random integers between 0 & 10. A empty list was created so that the full list printed at the end.

The final block of code contained another IF/Else statement that was used to determine if a preselected number was within the list and how many times. A while loop was then used to remove the number from the list.
Process Flow Diagram I

Process Flow Diagram II

Monday, May 29, 2017

Module 2: Python Fundamentals Part 1

Week 2 introduced the class to assigning string variables, using methods and functions to manipulate strings and lists, as wells as performing a bit of basic math.

Our lab assignment involved creating lines of code. The first code created a string of our full name. The second code broke our name into an individual list. We then extracted the last name using indexing and created a function that would calculate
the total number of letters in the last name. We were then instructed to assign a variable that multiplies the length of our last name by three.

This assignment was fairly straightforward but I certainly found that spelling mistakes is an easy way to mess up a code. I often received an error message that would seemingly set my process backwards.
More often then not the error was because my variable was misspelled. Another note is that when creating a string you do not have to print each line of code to produce the desired results.

Friday, May 19, 2017

GIS Programming Module 1

Introduction to Python

For the first programming assignment we were tasked with running a script using PythonWin 
that automatically creates a file path containing all of the folders needed. This task could be done 
by hand, but by using the provided script we are able to set up our folder file path 
instantaneously and exactly how the instructor requires. 

Here is a screenshot representing all folders in the S:drive.
The first task was to acquire Module 1 from the R:drive and paste into our S:drive. Module 1 contained a script that would automatically set up all files for the semester. While examining the script I was able to understand why each section of the code was created and its specific task because each new function had a description. After the script was reviewed and the Run function was used all of my folders appeared under the S:drive.