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

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


Monday, May 23, 2022

GIS 4102 - Module 1 - Python basics

 For the summer semester of 2022 I have enrolled in GIS 4102 to better understand the ins and outs of programming in GIS. We began interacting with python code in the IDE spyder. This software allows us to visualize and edit the code directly. Our first task was to run a provided script that creates all of the folders that we will be using throughout the semester directly into the designated S drive. After establishing the folders the assignment could begin. Opening the module template revealed some provided code. The first step of the assignment involved creating a string that involved our full name and then splitting that string into individual names. Using indexing I used a print function to display only my last name. The next step of the assignment was to correct two errors present in the provided dice game code. This was where I ran into my first hurdle. The first error was simple, correct the case of the x variable, the second error was tricky. I finally figured out that string values can only be added to other string values and not integers + strings which was the situation I had going on.The next steps involved creating a random generated list of twenty integers, 0-10, that when generated detects for an unlucky number. If this unlucky number is detected the number is removed and a print statement indicates how many times the unlucky number was removed. If the unlucky number is not detected a print statement indicates that the unlucky number was not in the list. These steps of the module took the longest. I ran into another issue involving correct parentheses usage that was solved after thorough contemplation and help from the UWF GIS youtube channel. Going forward this module has equipped me with the basic knowledge to read, understand, and interact with python coding. Paying close attention to syntax is key to achieving success in programming. 

Here is a screenshot of the full script ran from the sypder console.


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