Monday, July 18, 2016

Exercise 4

Goals

The goals of this exercise were to become more familiar with adding a field, calculating a field and applying an SQL statement to tables in Python. This exercise uses the same data from exercise three and has us take some of the exercise three outputs and add fields to them along with making calculations in the new fields.

Methods

The first step in exercise four was to Set up the script and import the modules. This is almost always the first step in writing python scripts. This included importing arcpy, os, time, datetime, and importing env from arcpy. The next thing that was added to the script was the statement that allows files of the same name to overwrite older ones. Once the environments were set and everything was imported, variables were created. These variables were imported using os.path.join to take them directly from the exercise three geodatabase. Once variables were created for dissolved fcs and the intersected fcs, a field was added to the dissolved feature class. This was done using the arcpy.AddField_management tool. This tool allows you to input a feature class that you want to add a field to, name the new field and define the datatype. The next step was to calculate the newly created field. This was done using the arcpy.CalculateField_management tool. In this tool, you enter the feature class and the field name that you need to calculate. Then you are allowed to enter an SQL statement in order to do the calculations. The next step in the exercise was to use the select tool to select polygons with an area greater than 2 square kilometers. This was testing the field that had just been created which was area. The arcpy.Select_analysis tool was used to select the polygons with an area greater than 2 square kilometers. We repeated all of these steps on the selection that we just made. Another field was added that defined the compactness of the polygon. The field was used by calculating the Area divided by the length times the length. Once this step was complete, the script was done. 

Results

The results of this script came out correct and both of the newly created fields had the correct values in them. The first time the script was run, there were errors. The errors that came up were due to spelling errors. Once the spelling errors were taken care of, the script ran correctly. Below is the final script for exercise four. 



Conclusion

This exercise didn't take very long to complete, but it did give more practice with some of the things that we can do with feature classes. The further we get into the exercises, the more I am starting to see the freedom that python scripts can bring. Adding fields and calculating fields will be a very important thing to understand when writing scripts. 


No comments:

Post a Comment