This is a presentation that teaches about making single line statements with python to manipulate string variables with a focus on esri arcgis calculate field. This presentation was given at the February 2013 AvidGeo Meetup group hosted by CGA Harvard. For more about the AvidGeo group visit their website at http://avidgeo.com
Continuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
Avidgeo String Manipulation : Getting Started with Python and ArcGIS
1. String Ma nipulation
cGIS
wit h python and Ar
Getting started
Guido Stein
guido@avidgeo.com
AvidGeo Meetup 2/21/2013
2. G uido Stein
● GIS Analyst
Applied Geographics, Inc.
○ Parcel Editing
○ Extract, Transform, and Load
○ Workflows
■ Model Builder
■ Calculate Field
■ Python
3. Overview
● Municipal Data
● Variables
● Fields
● Strings As Lists
● String Functions
● More About String
● Concatenation
● Putting It Together
● Single Line Statement If/Else
● Next Time
4. It's Ra ining Data
Planimetric Data
○ Road
○ Building
○ Fire Hydrant
Cadastral Data
○ Municipal Boundaries
○ Real Estate
■ Parcel
■ Property
5. e Data Mor e Problems
Mor
Parcel Polygon Property Table
045-267 / 88-3.A 45-88-3-A
045-267/ 88-4 45-88-4
045-267 / 88-5 45-88-5
045- 267/88-6 45-88-6
145- 267/88-7A 145-88-7A
145- 267/88-7B 145-88-7B
6. , a name I c all myself
Me
String Variable Declaration
"guido" me = "guido"
Integer age = 35
35 height = 5.75
Float sounds_like =
5.75 ["Burrito","Dorito"]
List
["Burrito","Dorito"]
7. seltongue T ranslator
Par
VB Python
[parcel_id] !parcel_id!
trim([parcel_id]) !parcel_id!.strip()
"N" & [parcel_id] "N" + !parcel_id!
8. Cutting t he Cheese
cheese = "Gorgonzola" cheese[-4:]
"zola"
["G","o","r","g","o","n"," ● Like right() in VB
z","o","l","a"]
cheese[4:-4]
cheese[:4] "on"
"Gorg" ● Like mid() in VB
● Like left() in VB
9. Function al Design 1
● strip(), lstrip(), par[:8].strip()
rstrip() "045-267"
● like VB trim()
par = "045-267 / 88- par.lstrip("0")
3.A" "45-267 / 88-3.A"
par[:8] par.strip(" 0.3A-8/")
"045-267 " "45-267"
10. Function al Design 2
● split() ● replace()
par = "045-267 / 88-
3.A"
par.split('/') par.replace('.','-')
["045-267 "," 88-3.A"] "045-267 / 88-3-A"
par.split('/')[0]
"045-267 "
11. something completely
Now for
different
Escape If you use single quote
around string literal
New Line then double quotes
n don't need to be
escaped
single/double quote
' "
Vise versa
13. Putting it together
par = '045-267 / 88-3.A'
'%s-%s' % (
par[1:3],
par.split('/')[1].replace('.','-').strip()
)
'45-88-3-A'
14. g it togethe r for real
Puttin
'%s-%s' % (
!parcel_id![1:3], !parcel_id!.split('/')[1].
replace('.','-').strip()
)
15. e Data Mor e Problems
Mor
Parcel Polygon Property Table
045-267 / 88-3.A 45-88-3-A
045-267/ 88-4 45-88-4
045-267 / 88-5 45-88-5
045- 267/88-6 45-88-6
145- 267/88-7A 145-88-7A
145- 267/88-7B 145-88-7B
16. ogether for real, really
P utting it t
'%s-%s' % ( !parcel_id!.split('/')[0].strip(' 0'), !
parcel_id!.split('/')[1].replace('.','-').strip())
17. ther for re al, really,
Pu tting it toge
really
'%s-%s' % ( !parcel_id!.split('/')[0].strip(' 0').
split('-')[0], !parcel_id!.split('/')[1].replace
('.','-').strip())
18. One More thing
Null = None
It will crash your statement
alternate statement if field is None else
primary statement
'' if !parcel_id! is None else ('%s-%s' % ( !
parcel_id!.split('/')[0].strip(' 0').split('-')[0], !
parcel_id!.split('/')[1].replace('.','-').strip()))
19. N ext Time
Advanced Calculate
● building your own functions
● tabbing
● if,then, else, elif
● import libraries
○ re
● More string functions
● variable comparison
20. String Ma nipulation
cGIS
wit h python and Ar
Getting started
Guido Stein
guido@avidgeo.com
AvidGeo Meetup 2/21/2013