Python in ArcGIS Pro 1
1 Calculate Field
Probably, one of the most useful Python features in ArcGIS Pro for regular users.
Still, you need to know a little bit of Python.
When do you want to use it?
- A field can have multiple different values depending on other fields.
- Manual way? Select a subset of features, assign a field value to them, and repeat for other subsets.
- Why Python? More efficient because you can achieve what you want in one run with no manual selections.
1.1 Let’s try Calculate Field
- Download georgia-wind-50m.zip from GIS data and add the Shapefile to a new map.
- Can you find the Calculate Field tool?
- There are seven categories of the wind speed for the state of Georgia in the
GRIDCODE
field. - We want to convert these codes to the ranges of miles per hour (
WindMph
).- Check Georgia 50m WPC Map.pdf for the ranges.
- In the text box next to “=”, type
get_wind_mph(!GRIDCODE!)
. - In the code block text box, type the following code and hit Run.
# a hash sign (#) starts comments that are ignored by Python; define a function # called get_wind_mph that takes one argument called gridcode; a function is a # group of statements that do some tasks to achieve a common goal def get_wind_mph(gridcode): # you need to indent any code blocks consistently # you're inside the function code block if gridcode == 1: # you're inside an if code block; use double equal signs for equal # comparisons and a single equal sign for assignments; if gridcode # is 1, return "0.0-12.5" and exit the function return "0.0-12.5" # double quotes create a string (text) elif gridcode == 2: # else if gridcode is 2, return "12.5-14.3" return "12.5-14.3" elif gridcode == 3: return "14.3-15.7" elif gridcode == 4: return "15.7-16.8" elif gridcode == 5: return "16.8-17.9" elif gridcode == 6: return "17.9-19.7" else: # else return ">19.7" return ">19.7"
2 Homework: Try Calculate Field yourself
Find any vector data and try the field calculator yourself to populate a field using a code block just like in the previous slide. Please explain what data you used and take a screenshot of each step. Show me your Python expression in your report. Submit your report in FirstLastname_CalculateField.pdf. There will be penalties for filename and format violations (-2 points for each). Don’t repeat my code.
3 Homework: Classification of Georgia counties using Calculate Field
- Download Counties_Georgia.zip from GIS data and add the Shapefile to a new map.
- Classify Georgia counties into low, medium, and high population densities.
- Add a new field
PopDens
and populate it with the population density in $\text{people}/\text{km}^2$ using thetotpop10
andSq_Miles
fields. - Add a new field
PopDensCat
and populate it with eitherLow
,Medium
, orHigh
using the mean $\pm$ standard deviation of the population density for the range of the medium category.
- Add a new field
- Compress your final Shapefile and PythonCode.txt with your assignment expression and Python code block into FirstLastname_CalculateField.zip.
``` Population Density
your assignment here calling the function below your function here
Population Density Classification
your assignment here calling the function below your function here ```
- Submit it in FirstLastname_CalculateField.zip.