Download presentation
Presentation is loading. Please wait.
1
Using Python in the Field Calculator
ArcGIS Desktop
2
Seminar Overview Python overview Python string functions
Python numeric functions
3
Python Overview
4
Python What is Python? Programming language Object-oriented
Started in early 1990s by Guido van Rossum Named after Monty Python’s Flying Circus Why Do We Love It? Free Quick to learn Easy to code Easy to read Many users (lots of help)
5
Python and ArcGIS Desktop
Label Expression Field Calculator Python Window
6
Python Overview Python Syntax Basics: Python is case sensitive!
In python, surround column(field) names with !!
7
Simple python expressions may contain:
Column(field) names !Street_Name! Literal text “meters” or ‘meters’ Mathematical and string operators + - / * Functions .upper()
8
Working with Strings (alpha numeric values)
9
Python Syntax: Strings
Strings are Surrounded by quotes Double “” Single ‘’ Backslash (\) is a special character \n = new line \t = tab Strings can be defined with either double quotes or single quotes. Can mix and match double and single quotes in a script, however, a string that is started with a double quote must end with a double quote, and a string that is started with a single quote must end with a single quote.
10
String Operators String Concatenation +
Python checks the field type. If you are using a + with numbers, Python does math. If you are using a + with strings, the strings get concatenated. !STATUS! + “AS OF 2016” !STATUS! + \t + “2016” !VALUE! + 100 Cannot add strings to numbers, or vice versa.
11
Working with Functions
Lots of functions for manipulating data. column_name>.<function>(function_parameters) Some functions don’t have parameters !street_name!.upper()
12
String Functions Built-in Functions .upper() .lower() .title()
.capitalize .replace(<findstr>, <replacestr>) Many others… !Company!.upper()
13
String Functions !Field1!.replace(<old>,<new>)
“COUNTY_NAME” = REPLACE(“COUNTY_NAME”,’County’,’’) Replaces a portion of a string strings are case sensitive in expressions
14
Python Slicing Used to extract a portion of a text string
!Field1![start:num_characters] Zero-based “COMPANY_ABREVIATION” = !COMPANY_CODE![0:2] “UNIQUE_ID’ = !PARCEL_NUM![-5:] strings are case sensitive in expressions
15
Working with Numeric Values
16
Number Operators/Functions
Mathematic *, /, +, - Round round(x [,n]) x rounded to n decimal places Truncate math.trunc(x) Truncates number x to an integer For round, Python rounds away from zero. round(0.5) is 1.0 and round(-0.5) is -1.0.
17
Number Tricks EXPRESSION RESULT !Elevation_ft! * 0.3048 Feet Meters
!Elevation_m! * Meters Feet !Area_km2! * km2 Acres !Area_acres! * Acres km2 round(!PROD! * !Royalty!, 2) $ format !Part1! / !Total! % percentage
18
Getting Help Help buttons on tools Arc GIS Desktop help:
“SQL (Structured Query Language), reference” in Index “Fundamentals of Field Calculations”
19
Thanks for attending!
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.