Python in ArcGIS Pro 1

Dr. Huidae Cho
Institute for Environmental and Spatial Analysis...University of North Georgia

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

  1. Download from GIS data and add the Shapefile to a new map.
  2. Can you find the Calculate Field tool?
  3. There are seven categories of the wind speed for the state of Georgia in the GRIDCODE field.
  4. We want to convert these codes to the ranges of miles per hour (WindMph).
    • Check Georgia 50m WPC Map.pdf for the ranges.
  5. In the text box next to “=”, type get_wind_mph(!GRIDCODE!).
  6. 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

  1. Download from GIS data and add the Shapefile to a new map.
  2. Classify Georgia counties into low, medium, and high population densities.
    1. Add a new field PopDens and populate it with the population density in $\text{people}/\text{km}^2$ using the totpop10 and Sq_Miles fields.
    2. Add a new field PopDensCat and populate it with either Low, Medium, or High using the mean $\pm$ standard deviation of the population density for the range of the medium category.
  3. Compress your final Shapefile and PythonCode.txt with your assignment expression and Python code block into
    # Assignment expression: Textbox just above the Code Block in Calculate Field
    your assignment here calling the function below
    # Code Block: Code Block in Calculate Field
    your function here
    # you have to show me your mean and standard deviation of the population density in code
  4. Submit it in