Tuesday, June 28, 2022

GIS 4102 - Module 6 - Working with Rasters

The final module for GIS programming has come to a conclusion. Module 6 had us working with rasters in order to produce an ouput that reflected map algebra calculations involving slope and aspect. The requirements for the final output were to create a raster that incorporated these elements:
• Forest landcover (classifications 41, 42, and 43)
• Slope between 5° and 20°
• Aspect between 150° and 270°

Here is a screenshot of the final raster.


And here is a screenshot of the Spyder console successfully running the script.

Here is the psuedocode for my script.
START
Import modules
Enable overwrite output setting
Set workspace to Elevation fGDB
Set output env
Create mod6 fGDB
Print mod6 fGDB completion statement
Create if conditional statement for script to run only if spatial analyst is available
     Check out spatial analyst extension
     Remap value function to assign landcover classifications 41, 42 and 43 all as new value of 1
     Reclassify tool on landcover raster using Remap variable
     Save reclassify raster
     Print reclassify completion statement
     Assign elevation raster variable
     Create variable for slope based on elevation, and percent rise field
     Create variable for aspect based on elevation field
     Create temp variable for slope1 condition >= 5
     Create temp variable for slope2 condition <= 20
     Create temp variable for aspect1 condition >= 150
     Create temp variable for aspect2 condition <= 270
     Combine 5 temp rasters into 1 final raster using boolean and map algebra operator (&)
     Save function direct final_raster.tiff
     Print combination completion statement
Create else condition if spatial analyst is not available
     Print Spatial Analyst is not available statement
END

Some problems I encountered over the course of this module involved correctly importing the spatial analyst module, syntax errors, and assigning variables to conditions involved in the slope and aspect functions. For the spatial analyst module I opted to use the import arcpy.sa code to import the spatial analyst module and for every sa function I included arcpy.sa in the script such as this example for the reclassify tool:  arcpy.sa.Reclassify. General syntax errors I ran into during this module mainly stemmed from when I had to copy bits of code from previous modules. I have learned over the course of these modules that observation of correct filepathing is essential to create a functioning script. Updating filepathing from mod5 to mod6 was necessary for correcting a filepathing error for the file GDB creation that I kept running into. The last major problem I had to deal with was assigning the variables in order to perform slope and aspect calculations using map algebra. The instructions called for the final value to be between two values thus two temp variables were needed: one greater than low value and one less than high value. This involved the creation of two variables for slope (s1 & s2) and two variables for aspect (a1 & a2). The values were determined via values obtained from the instructions. The result of this calculation was five temp rasters that were combined into one final raster using the Boolean And map algebra operator (&). The final raster was saved via the .save function. 

Over the course of this final assignment I found myself referencing past modules to grab sections of code needed for this module. In my future GIS programming endeavors I plan to create a script template that incorporates elements that have been enforced over the course of this class such as including  comment information such as name, date, description, etc and also including common modules imported. 


Tuesday, June 21, 2022

GIS 4102 - Module 5 - Geometries

The second to last module for my GIS programming course has been completed and I can safely say that I now have a better understanding of how to reference geometric data in python. This weeks module 5 tasked us with creating a script that could write 5 specified values for each reference point of the rivers.shp shapefile. The following is my psuedo script that walks you through the processes of each line of code. 

START
Import modules
Set workplace
Set output location
Enable overwrite output setting
Define feature class variable
Open .txt file and enable writing
Create search cursor to call on OID, SHAPE, and NAME fields
Start a for loop to iterate through each row in the cursor
        Define variable to act as vertex ID number
        Print row being referenced as “Feature #:”
        Start nested for loop to iterate each part being referenced
                  Print part being referenced as “Part #:”
                  Start nested for loop to iterate each point in part being referenced
                            Print row information pertaining to X and Y coordinate data
                            Add one value to vertex ID
                   Write method to add the five values:
Feature OID, Vertex ID, X coordinate, Y coordinate, Name of
                     the river feature
to .txt file
                    Print the five values:
Feature OID, Vertex ID, X coordinate, Y coordinate, Name of the river
                     feature
Close the .txt file
Delete the row variables outside of the for loops
Delete the cursor variables outside of the for loops
END

Here is a screenshot of the rivers_atg6.txt file after the write function had been completed, along with the console successfully showing the script ran in spyder.


Some issues I encountered over the course of this module involved referencing field values, indenting nested for loops, and referencing rows. The first problem I ran into was a simple fix but took quite some time to identify. While I was using correct syntax to create the search cursor I did not realize that the NAME field did not require an @ symbol due to it being a text related field and due to it not containing any geometry to be referenced the @ symbol was not necessary. Once this issue was sorted and all of the fields or columns were being referenced correctly I ran into my second and third problems, indenting my nested for loops and referencing rows correctly. After reviewing the module instructions and exercise information I opted to nest my for loops in a way that iterates the rows being referenced and defines the point variables for X and Y coordinates allowing the write and print functions to work correctly how I have them written. The result was print statements in the spyder console for each feature being referenced (OID), each part of the feature being referenced (vertexID), each X and Y coordinate for the feature (X,Y), and the feature name (NAME).


Tuesday, June 14, 2022

GIS 4102 - Module 4 - Exploring & Manipulating Spatial Data using Python

 Module 4 for GIS programming involved creating a script in Spyder using Python. The script created a geodatabase called atg6.gdb, copied features from a data folder to the fGDB, used a search cursor to find the Name, Population, and Feature of all cities listed as county seat in the cities shapefile and created a dictionary that populated with the name and populations of each 'County Seat' city. The following is a screen shot of the console running the script with completion print messages.

Some problems that I encountered involved creating the file geodatabase. My issues were resolved by correctly setting the output environment. I corrected this through fixing syntax errors in the following line of code: arcpy.CreateFileGDB_management(outputEnv, "atg6.gdb")

Creating the for loop to copy files from the data folder to the fGDB was another problem I encountered. I corrected this error by adding a description statement that allowed all items in the fc list to be described by their base names. This allowed the script to search the features without file extensions and allowed them to be copied to the fGDB. Getting the search cursor to work correctly was another problem encountered. I ultimately reconciled this portion of the assignment by separating each field into individual variables field = “NAME”, field2 = “FEATURE”, field3 = “POP_2000”.  The feature class portion of the script was also not detecting the cities shapefile in the fGDB. I made it recognize the file by adding the .shp file extension to the end of the cities feature class so the line of code looks like this: fc = “cities.shp”.

Something I found useful used in this module and past modules was the arcpy.Getmessages function and I will have to remember these functions for future projects in order to display a “Succeeded at <date and time> (Elapsed time: <____> seconds)” message.
Beginning
count = arcpy.GetMessageCount()
After each print statement process
print (arcpy.GetMessage(count-1))


Sunday, June 5, 2022

GIS 4102 - Module 3 - Geoprocessing

 Geoprocessing is a framework and set of tools for processing geographic and related data. 

In this weeks module 3 for GIS programming we utilized python in conjunction with arcGIS pro in order to perform geoprocessing operations. We began by designing a model in arcGIS pro using the model viewer window. This model was designed in order to clip all soils to a basin layer, select all soils that are designated "not prime farmland", and erase the selected polygons from the clipped soil layer. The following is a screenshot of the model viewer displaying the workflow of the model process.

The result of this model was a shapefile named soil_clip_erase1.shp that displayed all soils within the basin area that are suitable for prime farming. The following is a screenshot of the output shapefile.


The next portion of the module involved designing a script that would add XY coordinates and a 1000m buffer to hospitals.shp while printing success messages, start time, date, and elapsed time. I was able to accomplish this once the workspace and the overwriting of geoprocessing options was established. Adding XY coordinates was a matter of running the acrpy functions copy_managment and addXYmanagment. Print statements were added to display correct success messages, start time, date, and elapsed time. Here is a screenshot of the console after successfully running the script.
And here is a screenshot of the hospitals_buffer.shp displaying the 1000m buffer around hospital locations. 

 
Some tricks I learned over the course of this module involved proper file referencing in both python and ArcGIS pro. Model building involved an understanding of geoprocessing tools such as clip, select and erase. The key to building a correct model was referencing the correct input and output shapefiles.When designing the script for python, proper file referencing and function placement was key in ensuring a proper functioning script. I opted to add elements such as a “/n” spacing command at the end of the getmessages command along with print statements referencing the shapefile being used as the workspace. These elements allowed for a better visualization of the processes occurring in python.

Spring 2023 semester wrap up

 The spring 2023 semester at UWF has been an eventful one in which I finalized the requirements for my bachelors of science in natural scien...