210 likes | 474 Views
Using Python in the Field Calculator in ArcGIS Desktop. Seminar Overview. Python overview Python string functions Python numeric functions. Python Overview. Python. What is Python?. Programming language Object-oriented Started in early 1990s by Guido van Rossum
E N D
Using Python in the Field Calculator in ArcGIS Desktop
Seminar Overview • Python overview • Python string functions • Python numeric functions
Python What is Python? • Programming language • Object-oriented • Started in early 1990s by Guido van Rossum • Named after Monty Python’s Flying Circus • Free • Quick to learn • Easy to code • Easy to read • Many users (lots of help) Why Do We Love It?
Python and ArcGIS Desktop Label Expression Field Calculator Python Window
Python Overview • Python Syntax Basics: • Python is case sensitive! • In python, surround column(field) names with !!
Simple python expressions may contain: • Column(field) names !Street_Name! • Literal text “meters” or ‘meters’ • Mathematical and string operators + - / * • Functions .upper()
Python Syntax: Strings • Strings are Surrounded by quotes • Double “” • Single ‘’ • Backslash (\) is a special character \n = new line \t = tab
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
Working with Functions • Lots of functions for manipulating data. column_name>.<function>(function_parameters) • Some functions don’t have parameters !street_name!.upper()
String Functions • Built-in Functions • .upper() • .lower() • .title() • .capitalize • .replace(<findstr>, <replacestr>) • Many others… !Company!.upper()
String Functions • !Field1!.replace(<old>,<new>) “COUNTY_NAME” = REPLACE(“COUNTY_NAME”,’County’,’’) Replaces a portion of a string
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:]
Number Operators/Functions • Mathematic • *, /, +, - • Round • round(x [,n]) • x rounded to n decimal places • Truncate • math.trunc(x) • Truncates number x to an integer
Getting Help • Help buttons on tools • Arc GIS Desktop help: • “SQL (Structured Query Language), reference” in Index • “Fundamentals of Field Calculations” • http://support.esri.com