Wednesday, August 9, 2017

Module 11: Sharing Tools

This GIS Programming class was the first I'd ever been introduced to coding, and I feel I've learned a good deal over the semester.  The most important thing I can take away from this class is how to use Python to run Geoprocessing tools in ArcMap.  Of the topics covered this semester, this is at the top of my list because Geoprocessing is what makes ArcGIS so useful when analyzing large amounts of data and turning it into a visual form.  Using Python can expedite processes such as selecting data by attribute or clipping multiple feature classes.  This allows the user to do things with data in ArcGIS that they usually wouldn’t do because of the amount of time it would normally take.  I’m also thrilled to now understand the basic syntax of Python, and how to do things such as run for loops.  I believe understanding the basic syntax of Python will allow me to at least half way understand other coding languages. 


One more very important thing I’ve learned while working through these modules is to take breaks if things aren’t working out.  At times when I would get error message after error message, I simply had to stop for the night and go walk or run to clear my head.  Usually I would come back the next day and I would be able to identify my error rather quickly.  

This final module focused on Sharing tools.  During this exercise and lab we learned the three ways to share tools, how to add python file type to ArcCatalog, and how to create tool documentation. During the lab assignment we were given a toolbox containing a script and script tool.  We were instructed to fill in six parameters in the tool and then edit the script to ensure the variables corresponded to the parameters.  During this process, we were introduced to to the sys.arg[] expression which allows the script to talk to the parameter in the tool instead of using a hard coded file path.  Finally we edited the tool dialouge to make it more user friendly, imported the script to make it easier to share, and set a password for security reasons.  Below is a screenshot of the tool result on a map and the tool dialog box.  

Well this is it GIS Programming, it's been real.

Wednesday, July 26, 2017

Module 10: Creating Custom Tools

This week we learned how to turn Python scripts into custom tools.  This is important because it allows us to integrate scripts into ArcGIS and easily share scripts and their functionality with others, even if they're not familiar with Python.  This was a really useful lab because it showed me how I may use scripting in the real world.  It really tied together the knowledge I've obtained throughout the semester and made me realize how powerful coding can be.  I like that I can now easily share scripts with others.

Here is a simplified 5 step process for creating a tool out of a stand alone script:

1.  Open ArcMap and create a toolbox.
2.  Add a script tool to the toolbox and link the desired stand alone script.
3.  Add script parameters by filling in "Display Name" and "Data Type" fields.  (Input, Output,                   number of features)
4.  Using the GetParameter function, edit the script code to read parameters.
5.  Open ArcMap and double click the script tool to run it.

Below is a screenshot of my script tool window after the parameters had been set as well as a screenshot of the results window after the tool had been successfully run in ArcMap.



Monday, July 24, 2017

Avalanches in Norway! (PA#2)

For the second participation assignment, I chose to summarize the scientific article titled "GIS-aided avalanche warning in Norway" by Christian Jaedicke, Egil Syre, and Kjetil Sverdrup-Thygeson.  A link to the article can be found here: LINK  
Avalanche warnings can be issued at different scales, varying from large regions down to specific avalanche paths. In Norway, avalanches are forecasted on a national five level scale.  Recently the warning sizes greatly decreased in order to accommodate specific clientele needs.  Today, the Norwegian Geotechnical Institute (NGI) issues detailed client based warnings with the help of GIS based avalanche warning maps.  The three main types of data which go into forecasting avalanches are state of snowpack, meteorological conditions, and terrain.  Because data is collected from so many different sources, GIS provides a useful platform for compiling data into one system.  
Today GIS is used to create and manage terrain models, avalanche release areas and paths, and infrastructure such as roads and houses.  A national terrain model allows for accurate slope and aspect calculations to be performed on all of Norway's mountain areas at risk for avalanches.  The most active and critically important avalanche terrain paths were added to the GIS based map and data such as fall height, size of release, physical characteristics, and wind direction at which the path is exposed was added to the GIS database.  Meteorological data and snowpack information is input into the GIS system by using weather stations and field observations from many different sources.  This data was used to create a real time interactive avalanche map which highlights areas most at risk for an avalanche.  When a single avalanche path is identified as high risk, it is highlighted in red and the forecaster can click on it and obtain real world data such as height fall or infrastructure in the path which may be impacted.  This allows avalanche forecasters at NGI to efficiently issue the most detailed and accurate warnings as possible.  
GIS has helped revolutionize Norwegian avalanche forecasting because it compiles data from many different places all into one database.  Forecasters can now work more efficiently because they don't have to click around to multiple web pages to obtain all the data needed to issue a warning.  This system is just the beginning and improvements will continue to be made as technology advances and meteorological data transfer from stations becomes more accurate. 

Sunday, July 23, 2017

Module 9: Working with Rasters

This week in GIS Programming, we used Python coding to work with Raster files.  We learned how to list and describe rasters as well as how to use raster objects in geoprocessing.  We also learned how to use map algebra in operators and work with classes to define raster tool parameters.

Specifically in this lab, we used Python to create a raster output that identifies areas with a particular set of parameters.  By using the Spatial Analysis module and enabling the Spatial Analyst extension, I was able to create a final raster output of a file with the following parameters highlighted:

  1. Forest landcover classifications 41, 42, and 43 
  2. Slope between 5-20°
  3. Aspect between 150-270°
My steps to complete the script are summarized below:

To begin with, I imported the usual modules and classes needed to begin a script then enabled the overwriteOutput setting.  I then chose to not create a new geodatabase and decided to save the script directly in my "Results" folder.  Next I created a new raster to meet the three criteria listed above.  To do this I wrote a conditional statement to ensure the code runs only if the spatial analyst extension is available.  Next I "checked out" the spatial analyst extension.  I then reclassified the landcover map to only show forested land and did the slope and aspect calculations for the above specifications.  These calculations produced 5 temporary raster objects which I combined using ambersands and I used the save method to make them permanent.  Below is a screenshot of the final output.  


Wednesday, July 19, 2017

Module 8: Working with Geometries

This week in Module 8, we worked with the geometries of objects and multipart features and learned how to read and write geometries.  I can see how this will be useful because it allows us to access the rows in a shapefile and convert the geometry data such as x,y coordinates to a text file.  I can think of many situations in my environmental sector of work where this would come in handy.  For example, if I had a shapefile showing the locations of monitoring wells, I could access the coordinate locations and quickly print them in a spreadsheet.

This weeks actual lab assignment had us create a txt file from a rivers shapefile.  We were asked to create a search cursor to access the geometry of the shapefile by using geometry tokens.  Using a series of for loops and a nested for loop, we iterated through the rows of the feature, then also the individual points/vertices of the array.  Using the .write() method, I added the following text to to my txt file which I had previously opened:  1. Feature Object ID # (OID) 2. Vertex ID 3. X coordinate 4. Y coordinate 5. River name.

Overall this was a very challenging lab for me.  As I learned in previous labs, my error messages when running the script usually boil down to a small error such as and indent or extra line of code that is not needed.  My biggest hurdle here was writing the x,y coordinates to my txt file.  I finally realized I had created a point variable that did not need to be included and was therefore disrupting my (point.X) and (point.Y) lines of code.  The biggest lesson I learned from this lab is don't try to code when upset.  Take a walk and come back to it later.  Below is a screenshot of my script result.


Wednesday, July 12, 2017

Module 7 - Exploring and Manipulating Spatial Data

Wow this was a challenging lab!  After much anguish, I do feel like I accomplished it and learned quite a few scripting/debugging skills in the process.  To start out this lab, we explored the different ways to work with spatial data including checking for the existence of, describing, and listing data. We got a re fresher on how to use for loops and learned the difference between lists, tuples and dictionaries.

After learning how to explore spatial data, we then learned how to manipulate the data using three different types of cursors (Search, Update, Insert) and also how to used Structured Query Language (SQL) in python.

The actual lab started out by writing a python script to create a geodatabase and copy our feature classes from the set workspace to the new geodatabase using the basename property from the arcpy.Describe() function.  In the next step, I created a search cursor to access three fields (city name, feature, population) from my cities layer.  The last part of the lab involved creating and populating a dictionary with the cities in New Mexico which are designated as "county seats." This part was by far the most challenging and time consuming part of the lab for me.  We were required to populate our empty dictionary with these city names by the use of a for loop. While trying to piece together the for loop, I realized I had a for loop after my search cursor which did not need to be there.  After deleting this for loop and reading through chapters 6/7 and the exercise, I realized I needed to treat the cursor row as a variable and set my key and and value variables.  I set those variables as follows:

  • key = row[0]
  • value = row[2]
I then used the update function to add city and population pairs to my dictionary.  I thought I was home free at this point, but my trouble was just beginning.  I ran the script and only one city/population pair was populated into the dictionary.  This led me to believe I had a problem with my variables.  I spent hours and hours changing my variables and trying not to punch my computer. With two hours to spare before the lab was due, I finally realized the problem.  I had not put my update function into the for loop.  After I did this, all 32 cities were populated.  In hindsight I cannot believe it took me so long to see the issue, but live and learn.  Below are screenshots of my script results.

Wednesday, June 28, 2017

Module 6 - Geoprocessing with Python

The concept of this week's module was to teach us how to perform geoprocessing tasks using Python. As opposed to last week where we ran geoprocessing tools using ArcMap then exported to Python, this week we performed these tasks directly through Python.  A big thing that stood out to me this week was how to use the statement "import arcpy."  I learned that this line of code is known as a site package and allows us to import all of arcpy's modules and functions.  If we're using the Python window in ArcMap, it is not necessary to start our code with "import arcpy" but if we're using Python this is a must.

During this weeks lab, we were assigned the task of writing a Python script that performed the following three geoprocessing functions.

  1. Add XY coordinates to the hospitals.shp layer.
  2. Create a 1000 meter buffer around hospital features.
  3. Dissolve these buffers into a separate layer.  
To accomplish this task I first imported arcpy because I was using PythonWin.  I then wrote lines of code to enable overwriting and set the workspace to my data file path.  I then ran the three tools listed above and wrote a line of code to print the results after each tool was run.  It was really cool to see how using scripting with Python can accomplish tasks in ArcMap.  Below is a flowchart of my scripting process and a screenshot of my script results in the interactive window. The most frustrating part of this lab was unsuccessfully attempting to convert my flowchart to a picture. I'll get that process down eventually but not today.  Just click on the flowchart picture to make it bigger.