Field Calculator and Python Script
A new field will be created in an attribute table and an expression calculated using the values contained within other fields of the table. The values placed in the calculated field will be done using a Python script; there are other methods this task could be accomplished without using Python. A method to complete the task using a Python script will be demonstrated, this might not be the most efficient method, depending on the amount of data. The user in the production environment must determine the most efficient method to complete the required tasks.
The script will be written directly in the field calculator but could have been written in an IDE, copied and pasted into the field calculator window. It is important to remember that the attribute table is displaying the information contained in a database and not a spreadsheet. In a spreadsheet, the equation is stored in each cell and a new calculation is completed every time the data is utilized. In a database, a calculation is done once and the new value is written into the cell. The equation is not stored as part of the cell content and thus a new calculation not completed each time the information is accessed.
In this example, a data set of U.S. colleges will be used. The file should be loaded into Esri ArcMap, and then open the attribute table. The process requires that a new field be manually created. Make sure no special characters are used in the field name. The field calculator should be opened for the newly created field. In this example, a looping statement will be used. Remember that a colon is placed at the end of the statement, in the IDE, the colon causes an indentation of the next line of code, but in the scripting window the indentation must be handled manually, always use the same number of spaces for the same level of indentation.
The script will be written directly in the field calculator but could have been written in an IDE, copied and pasted into the field calculator window. It is important to remember that the attribute table is displaying the information contained in a database and not a spreadsheet. In a spreadsheet, the equation is stored in each cell and a new calculation is completed every time the data is utilized. In a database, a calculation is done once and the new value is written into the cell. The equation is not stored as part of the cell content and thus a new calculation not completed each time the information is accessed.
In this example, a data set of U.S. colleges will be used. The file should be loaded into Esri ArcMap, and then open the attribute table. The process requires that a new field be manually created. Make sure no special characters are used in the field name. The field calculator should be opened for the newly created field. In this example, a looping statement will be used. Remember that a colon is placed at the end of the statement, in the IDE, the colon causes an indentation of the next line of code, but in the scripting window the indentation must be handled manually, always use the same number of spaces for the same level of indentation.
Example
In this example, a classification will be performed on the offering of geospatial technology classes and the type of institution. The value placed in the new field will depend on the values of other attributes in the table. Nested if statements will be used to modify the content of the field. While this process could be completed manually it would take extensive time, which could be impacted by typographical errors, the code will create results that are always identical for each cell with the same parameter. The new field, that was manually created, is a text field and has been given the name Test. Once the Field Calculator Window is open, select the Python Parser and select the check box Show Codeblock. See Figure 6, for details, ignore the code, it will be discussed in the next steps.
|
In the lower box seen in Figure 8 a function will be defined called fnInformation. Inside the parenthesis are different field names. It is important that the field names be inputted by double clicking on the field name in the Fields box in the upper left corner of the Field Calculator window. The name for the fields were not properly titled when this file was created. Make sure a comma is placed between each of the field names. In the Pre-Logic Script, fnInformation is defined. The Pre-Logic script must have a one to one correspondence with the function (each having the same number of elements) that was defined. See Figure 7, for the definition of the Pre-Logic Script.
|
The first element represents the type of institution such as community or university. The next element defines if the institution offers GIS courses. The full script for the process is shown in the table. Nested if statements were used to solve this problem, it could have been created in a different way and only a single if statement used. The Return statements will populate the field test with what is enclosed inside of the quotes. It is important when the field test was created that sufficient characters were allowed so that the complete statement can be saved.
|
Points to Remember
- When a define statement is used a colon must be placed at the end of the statement and each line after the statement must be indented. The IDE will auto indent, but when typing in the script window, the user must manually create the indention. If nested statements are used each level of indention must have different spacing.
- The first if statement is establishing if the institution is a 2-year college. Providing it is true the logical path will be to the next if statement (note it is indented more than the first if statement), if it is false it will drop to the else statement at the end of the code with the same indention level as the original if statement. The data set only has two types of institutions; two-year college and four year institution.
- When the first if statement is true it will drop to the next if statement which is using a logical ‘and’ function. For the if statement to return a true result all members must be yes. If this is true then the 2-year college offering classes, certificates and degrees would be place into the cell for the field Test. If not true, it would drop to the first elif statement and continue the process.
- Since two year colleges do not offer bachelor or graduate degrees they are not included in the script logic, but are part of the file.