python: beyond the basics · python sessions at fedgis session tuesday wednesday python: an...
TRANSCRIPT
Derek J. Nelson
Python: Beyond the Basics
Topics Covered
• Cursors
• Geometry objects
• Leveraging other Python modules
• Used to:
- Iterate over the set of rows in a table
- Insert new rows into a table
• Two varieties:
- arcpy.da cursors (10.1 onwards; significantly faster performance)
- “Classic” cursors (provided only for continuing backward compatibility)
Cursors
Data Access Module Cursors
• Table
- The feature class, layer, table, or table view
• Fields
- Single field or list of field names
- Index position in fields parameter defines value access
# 0 1 2
fields = [“Name”, “Year”, “Count”]
Required Arguments
Used as shortcuts in place of field names
Tokens
• OID@ —The value of the ObjectID field.
• SHAPE@ —A geometry object for the feature.
• SHAPE@XY —A tuple of the feature's centroid x,y
coordinates.
• SHAPE@TRUECENTROID —A tuple of the feature's
true centroid x,y coordinates.
• SHAPE@X —A double of the feature's x-coordinate.
• SHAPE@Y —A double of the feature's y-coordinate.
• SHAPE@Z —A double of the feature's z-coordinate.
• SHAPE@M —A double of the feature's m-value.
• SHAPE@JSON — The esri JSON string representing
the geometry.
• SHAPE@WKB —The well-known binary (WKB)
representation for OGC geometry. It provides a
portable representation of a geometry value as a
contiguous stream of bytes.
• SHAPE@WKT —The well-known text (WKT)
representation for OGC geometry. It provides a
portable representation of a geometry value as a text
string.
• SHAPE@AREA —A double of the feature's area.
• SHAPE@LENGTH —A double of the feature's length.
arcpy.da.SearchCursor
arcpy.da.SearchCursor(in_table, field_names, {where_clause},
{spatial_reference}, {explode_to_points}, {sql_clause})
arcpy.da.SearchCursor – optional where clause
parameter
# Open a Search Cursor, print results
table = "Crime_Incidents_2016"
fields = ["Shift", "Offense", "Method", "Ward"]
qry = " Ward = '6' "
with arcpy.da.SearchCursor(table, fields, qry) as cursor:
for row in cursor:
print("Shift: {} Offense:{} Method: {} Ward: {}".format(row[0], row[1], row[2], row[3]))
Demo: Search
Cursor and Email
Update
arcpy.da.UpdateCursor
arcpy.da.UpdateCursor(in_table, field_names, {where_clause},
{spatial_reference}, {explode_to_points}, {sql_clause})
Method Explanation
deleteRow () Deletes the current row
updateRow (row) Updates the current row in the table.
arcpy.da.UpdateCursor
arcpy.da.UpdateCursor
arcpy.da.UpdateCursor
arcpy.da.InsertCursor
arcpy.da.InsertCursor(in_table, field_names)
Method Explanation
insertRow (row) Inserts a row into a table.
arcpy.da.InsertCursor
Working with Geometry Objects
• Reading geometries
- arcpy.da.SearchCursor
- arcpy.da.UpdateCursor
• Writing geometries
- arcpy.da.UpdateCursor
- arcpy.da.InsertCursor
• Work with geoprocessing tools
Using geometry as input with geoprocessing tools
Demo: Cursor to
Collector
Leveraging other Python modules
Installed with ArcGIS Pro
• Examples:
• arcgis
• scipy
• pandas
The Python Standard Library
• Examples:
• os
• urllib
• zipfile
Additional third-party Python
packages
• Examples:
• beautifulsoup
• tensorflow
• scikit-learn
Leveraging other Python modules
• Python Standard Library
- Read a csv with the csv module
- Unzip a .zip file with the zipfile module
- Download data from the internet with the urllib module
- Automatically open an output with the os module
- … many more
• Can install other pre-existing Python packages with ArcGIS Pro Python
Package Manager
Demo: Python
Package Manager
Create and Prep
Jesse Parker
Why did we build this?
❖Show enterprise GIS data from internal network to remote clients
❖Could not change/interrupt current data editing workflow/databases
❖Saves hours of manual project setup work and reduces human error
Recommendations❖ Documentation
❖ Always reference docs paying special attention to arguments (types) and returns
❖ Learn and use a proper IDE
❖ Autocomplete, debugging, refactoring, referencing, environments, version control
❖ Variable/function/class naming, comments
❖ It does not hurt to anything to use names that make sense and makes your code readable
❖ Comments should help the reader understand the logic
❖ Use PEP 8 (Style guide) (A good IDE will help with this)
❖ Python 3
❖ ArcPro/Enterprise required to use arcpy in python 3
❖ Use ArcPro as virtual environment manager
❖ Typing! (available in python 3)
❖ function/method return types
❖ Argument types
❖ Logging
❖ Use python logging and add filter to send messages to ArcPro
Print Your Certificate of Attendance
Print Stations Located in 150 Concourse Lobby
Tuesday12:30 pm – 6:30 pm
Expo
Hall B
5:15 pm – 6:30 pm
Expo Social
Hall B
Wednesday10:45 am – 5:15 pm
Expo
Hall B
6:30 pm – 9:30 pm
Networking Reception
Smithsonian National Museum
of Natural History
Download the Esri
Events app and find your event
Select the session
you attended
Scroll down to
“Survey”
Log in to access the
survey
Complete the survey
and select “Submit”
Please Share Your Feedback in the App
Python Sessions at FedGIS
Session Tuesday Wednesday
Python: An Introduction 1:45 pm – 2:45 pm in 146B 11:00 am – 12:00 pm in 146B
Python: Beyond the Basics 4:15 pm – 5:15 pm in 146B 1:30 pm – 2:30 pm in 146B
ArcGIS Pro: Analysis and Geoprocessing Overview 11:00 am – 12:00 pm in 147A
Python: Building Geoprocessing Tools 4:00 pm – 5:00 pm in 147B
Python: Map Automation in ArcGIS Pro 5:15 pm – 6:15 pm in 102A
Data Science in ArcGIS Using Python and R 1:45 pm – 2:45 pm in 150A 5:15 pm – 6:15 pm in 147B
Data Science Using ArcGIS Notebooks 3:00 pm – 4:00 pm in 150A 2:45 pm – 3:45 pm in 147B
Machine Learning in ArcGIS 3:00 pm – 4:00 pm in 146C 8:30 am – 9:30 am in 146C
Presenter Names
Presentation Title
Section Subhead
Section Header
Presenter(s)
Demo Title