In this lab we are learning about how to use python and the model builder function to program our own functions within ArcGIS. Specifically, we tried taking the existing function for the splice feature, as created in Mode builder and making our own amendments to loop a for function across a series of image tiles. Specifically to Python, we were introduced to the basics of the programming syntax, how to make objects, lists, loops, what some of the branches of logic are, and how to open and write a file.
Here we got source imagery from the United States Geological SUrvey (USGS) for the GloVis image collection. WIthin this environment, we downloaded and requisitioned imagery for our area. Opening up ArcGIS we started by creating a .mxd file for the project. The data from the downloaded link was added to the data frame as a requisitioned Landsat TM image. The from the downloaded file, each image represented a band within the requisitioned LANDSAT image. For instance, this example had 7 different image .TIFF files.
Once adding the first band image, we added in a base-map so we could see where the landscape tile existing somewhere in Northern Quebec, towards James Bay. This differs from the rectification lab as this tif already has spatial information associated with it, and did not need to be rectified or altered as to give it spatial information.
Figure 1. Landsat band 1 tile mapped over Northern Quebec basemap, the light green image tile is the product of the ""time" analysis in the subsequent steps.
Now we start preparing the model builder environment. To avoid a last lab’s mistake, we are opening ArcMap 10.8.1 instead of 10.7.1 (the version with model-builder intact). The model builder was parameterized to the extent of the image. We then opened up the spatial analyst toolbox. Using the times feature in the spatial toolbox, we multiply the source image by a constant of 1. The result is a smaller tile within the larger tile, as mentioned in figure 1.
Figure 2: Screenshot of ArcMap interface with the corresponding mapped Landsat band, and the model builder open.
Once the output was mapped, we then exported the script for the function. As model builder is a visual interface, for every drop-and-drag of a function it is concurrently writing code in Python for what functions are to occur. However, to isolate and write specific functions outside of the scope of standard ArcMap functions, we have to download and modify the code ourselves. Within model builder, we exported the script for the times function to python following model > export >to python script feature.
The python script was saved and then opened within IDLE.
The result is below:
Figure 4: Exported Python Script from the ModelBuilder in ArcMap.
We learned the basics in this section of the programming language, including syntax and creating variables, the basic “Hello World” that we learn in every coding language, creating list objects, loops in python, branches in logic, and lastly how to open and write a file. After learning the different functions in the python editor we moved on to the application section to an existing set of code.
variables are created through stipulated the new object name, followed by an operator (i.e., =) and what the values will be associated with.
The in-lab example was to give different letters numeric values, and to add them together. Following assigning values to variables, we created a new variable which value was a composite of the two, and printed it to the screen. An example here with multiplication would be as follows:
>>> x = 2
>>> y = 5
>>> z = x * y
>>> print z
10
We learned the product of a print function is called a carriage return, where when calling the value for one variable prints, on the next line, the value in question. We also learned how to do this with strings, and did the "Hello World" coding introduction where we assign a variable to that string, and then printing that variable name, and the carriage return is the phrase, "Hello World".
We then learned about lists, which is the ability to store multiple values in a single variable/object. These can be both strings or numerical, the important delimitation being that lists use square brackets, and each list item is comma seperated. The same is for whether it is to make a list with items, or to specify bands in a remote sensing image. A cheeky example with a joke would be as follows:
>>> whatDoesaNarcissistcallTheirfriends = ["me","myself", "I"]
>>> print whatDoesaNarcissistcallTheirfriends
me, myself, I
We then learned how to make a loop in python, where a code is executed for all items in a list iteratively. This is done with a "for" function. The idea of a loop function is that when called, it preforms the set of details on the first item in the list, then the second, and so on and so forth so that it is iterative and non-conflicting. We use loops later to run a function across a series of bands. This is the ability for iteration, which is one of the main reasons it's used in ArcGIS instead of clunky "batch" functions in the user interface.
We also learned about branches in logic, which tells us how different operators work. the example with 3 being smaller than 4 allows the function to print, but when reversed (assuming 3 is larger than 4) the function does not work.
lastly, we learned how to open and write a file in python. After learning the different functions in the python editor we moved on to the application section to an existing set of code.
In this section we went through and cleaned up variable names within the script, and taking our existing model builder code, we modified it to overwrite the existing band file through the times function, reentered it into the python module in ArcMap and ran the module. Subsequently, in the challenge section we brought the slice function into model builder and used that as the basis of the challenge function with iterative functions. Using the slice function, we changed the output zones to 5, and the slice method to natural breaks. We used the same input rasters from the Landsat dataset for this challenge.
One thing I really struggled with in this lab is the for functions and creating loops in the lab. As Dr. Cardille explained it. I understand that the function variable becomes a container variable, sequentially, as it processes through all list items in an object, however the concept is not fully clear to me yet as to how to create loops.
In this example, I created two objects ‘newvar1’ and ‘newvar2’,. Newvar1 was to represent the function for the list out output bands, as a list, that the slic function could be computed on. Likewise, newvar2 was used to loop the list of band inputs. These are both criteria for the slice function per the arc module which relies on 5 variable inputs:
The output raster file name
The input raster file name
The number of Output Zones
The break classification method (i.e., Jenks, Natural breaks, Quartiles)
The multiplier for the raster DN to be multiplied-by.
One thing I also learned about in this lab is the other data libraries that had to be imported to conduct the function. In this case, i imported the NumPy data library so that i could calculated iterative calculations for the slice function, using functions as objects (“newvar1” and “newvar2”).
The result of the slice function looped is seen in the catalogue pane of the lab here.
Figure 5: Result of Slice function to compute slice over all images in the stack.
The next step was to calculate band statistics of each of the functions in ArcMap. The code to do that was retrieved from the GetRasterProperties pane.
Figure 6: Help pane from the ArcMap Desktop for GetRasterProperties
Subsequently, we were to calculate the mean and standard deviations of the layers and then print it to a .csv. Unfortunately after this step i had trouble with lab computers to be able to run the summary stats. So, i wrote the code that i would run over the image collection to do so.
I could not figure out a “for function” to calculate the band statistics, and my thought was that I thought I could run them individually on the slice outputs. Alternatively, could use the “newvar1” object (which is the list for the bands), then include in as within the function so it can run iteratively. The print function would be altered to look like this:
Overall, in this lab we learned how to work with the Modelbuilder function and cater functions that are standard in ArcMap toolboxes to specific operations, such as looping functions that are more performative than batch functions. We also learned the fundamentals of python syntax in objects, lists, functions, and branches in logic.