Showing posts with label GIS 5103 - GIS Programming. Show all posts
Showing posts with label GIS 5103 - GIS Programming. Show all posts

Friday, June 29, 2018

Geoprocessing with Python

This week we focused on executing geoprocessing steps outside of ArcGIS using Python scripting.  While the task for this weeks assignment seemed daunting at first, but when I broke it down and used the ArcGIS help pages to obtain the correct syntax, all the pieces fell into place rather simply and without much need for incessant troubleshooting.  The following steps were used to create the script:

  1. Import arcpy and the env tools
  2. Obtain the proper syntax for AddXY and add that tool to the script, export messages upon completion
  3. Obtain the proper syntax for Buffer and add that tool to the script, export messages upon completion
  4. Obtain the proper syntax for Dissolve and add that tool to the script, export messages upon completion
  5. End script.
The script was literally that easy to create.  Only required parameters were used for this script as well which made it that much easier to write.  Once the script was run, I opened ArcMap and verified that the steps were completed correctly.  Here is a screenshot of the interactive window from PythonWin showing all of the tool messages after I ran the script:


Let me know if you have any questions by using the comment section below!

Wednesday, June 20, 2018

Geoprocessing in ArcGIS

This weeks lab work focused on introducing us to creating toolboxes within ArcGIS that can hold custom created models (built from the ArcGIS model builder) as well as script tools that, in this case, were written in the PythonWin IDLE. 

The first step in the lab was to create a model tool using the model builder within ArcGIS.  The goal of the model was to take two shapefiles, create a clip of one using the other as a mask, then select certain features within that mask and create another output shapefile of the selected features.  The final step of the model was to delete the selected features from the original clipped shapefile.  What I ended up with is something like this:


The original clip was solid while the final output above shows all the deleted areas within it that were removed from running the model. 

The rest of the lab consisted of taking the model I created, exporting it to a python script, validating that the script worked and overwrote the existing output, and then finally taking the python script and creating a script tool within ArcGIS. 

Overall it was really neat to work through the process and see how all these tools can be interconnected and have varying degrees of power and flexibility.  While the processes we are working with right now are relatively simple and mundane and one would wonder, why even script to do something so simple?  It is plain to see that as you get into more complex and repetitive geoprocessing that scripting and model tools could be a huge help in reducing manhours and tedious manual processing.  

Thursday, June 14, 2018

Script Debugging

This week's focus was on debugging errors that is an integral part in script writing.  No matter how particular you are in your program writing, errors are bound to happen.  Whether they are syntax errors, exception errors, or even logic errors, they can be frustrating to locate and weed out when you are creating your scripts.  For the lab this week, it was our task to review and debug three different provided scripts. 

The first script contained two errors and both were simply typographic errors on different variables.  Once those type-o's were corrected, the script ran correctly as you can see below. 


The second script was more complex and contained 8 errors within it.  I had a lot of trouble getting past the first error and once I realized that the problem was not only the file name, but the file location, I was able to step through the remaining errors somewhat efficiently.  Some types of errors that I found within the second script included: file name and location errors, syntax of file paths, typographic errors for function/methods used, and missing or extra parameters for functions.  Once I corrected the 8 errors that I found, I was able to get a successful run of the script and here is how it turned out:


The last script had us take a different approach.  Instead of correcting the errors that were found, the goal was to implement the try-except callout to identify errors within a section of code and still successfully run the entirety of the script without erroring out.  The script had already been divided into two parts.  Part B was good to go with no errors.  There was an error in Part A however that needed to be isolated and reported using the try-except functions in Python.  Once implemented successfully, I was able to achieve and error output for Part A and then still complete Part B of the script.  The output is below:


So that is it.  No real flow charting on how I go through my debugging process.  I am sure debugging gets easier over time as one becomes more familiar with how different classes are used/called upon in Python and what is needed for those to run successfully.  As always, leave any comments below on how you approach debugging. 

Cheers!

Wednesday, June 6, 2018

Python Fundamentals II - Getting Acquainted with Modules and Conditionals

This week we got a bit deeper into programming with Python.  We delved deeper into using/importing modules and then were introduced to using conditional statements like if/else and while/for loops.  The particular script I worked on had three parts to it.  The first part was simply finding the errors in a section of code and fixing them.  There was two and the output you will see below shows that the errors were fixed.  This particular section of output shows 7 named players and whether their randomly assigned number based off of twice the length of their name is a winning number or not. 

The second part of the script was to create a list of 20 random numbers.  Each number had to be within 0-10.  Once the list was generated it was printed to the screen.  In the output picture below, this list will be the first one you see. 

The third part of the script was the more challenging part.  This used a combination of different conditional statements to determine if a predefined unlucky number was in the list.  If that number was not in the list, as statement was returned and the unmodified list was reprinted.  If the unlucky number was in the list, the number of times it occurred was counted, a statement was returned stating the number of times the number was in the list, actions were performed to remove the unlucky number, and then the modified list was printed to screen. 

Here is a flow chart for how my thought process worked for this last part:


And here is my output for the entirety of the script:


If you are curious as to how I achieved the outputs above, leave a comment.  I can give examples after the due date as to how I finalized my script.  Let me know if you have any questions and happy scripting to all!

Wednesday, May 30, 2018

Python Fundamentals Part 1

This week was a shallow dive into the basics of Python scripting.  We learned about basic variables, objects, functions, methods and how to use these elements to create a basic script.  The script itself only had one input in the beginning.  This input would be a users name.  From there the script would take the variable with the name, turn it into a list for every name in the initial string that was separated by a space, and then print the last name to the screen.  From there the length of the last name was determined and then tripled.  The final part of the script was to print tripled count value of the last name to the screen.  Here is a screen shot of how mine turned out. 


The script was tested using a number of different name strings as the initial variable and the final outcome always printed whatever the last name and triple the number of characters in that last name.  Again, this was all basic scripting to allow us to scratch the surface of script programming and get a feel for the basic elements of Python. 

Let me know what you think in the comments below!  I can also answer any questions on how to achieve this outcome efficiently as well.  

Wednesday, May 23, 2018

Introduction to Python

Here we are in our first week of GIS Programming.  This week focused on setting up our folders in our S:/ drive and getting a basic understanding of Python scripting and editors.  The editors we looked at were IDLE, PythonWin, and the Python editor which is in ArcMap 10.5.1.  Within these editors, a simple script was run to display the text "Hello World".  Nothing too fancy, just enough to show how different elements are highlighted and displayed in the different editors.  For the most part we will be using PythonWin for this course. 

To set up our folders for the course, we used a python script vice manually creating 3 folders for each of the 12 modules in the course.  The screenshot below shows the outcome of this script.


The overall process summary felt like an introduction to completing process summaries.  This one focused on showing where you store your data, asked some questions from the reading, and then had us explain how a certain step was completed in our own words.  This introduction to process summaries would be beneficial to first time students or those who have been away for a couple of semesters. 

Other than that, this was probably the only simple week I can hope to have during this course.  My coding/scripting skills are extremely rusty, but I am hoping it will all come back quickly and overall I am looking forward to this course.