![Page 1: Python: Beyond the Basics - Recent Proceedings...Python: Beyond the Basics Author Esri Subject 2017 Esri Federal GIS Conference--Presentation Keywords 2017 Esri Federal GIS Conference--Presentation,](https://reader030.vdocuments.us/reader030/viewer/2022011908/5f5ad95c2f81e3013b729bad/html5/thumbnails/1.jpg)
Python: Beyond the BasicsBrittney White, Esri
Katie Leaverton, Chesapeake Bay Foundation
![Page 2: Python: Beyond the Basics - Recent Proceedings...Python: Beyond the Basics Author Esri Subject 2017 Esri Federal GIS Conference--Presentation Keywords 2017 Esri Federal GIS Conference--Presentation,](https://reader030.vdocuments.us/reader030/viewer/2022011908/5f5ad95c2f81e3013b729bad/html5/thumbnails/2.jpg)
Topics Covered
• Cursors
• Geometry objects
• Leveraging other Python modules
• Python in ArcGIS Pro
![Page 3: Python: Beyond the Basics - Recent Proceedings...Python: Beyond the Basics Author Esri Subject 2017 Esri Federal GIS Conference--Presentation Keywords 2017 Esri Federal GIS Conference--Presentation,](https://reader030.vdocuments.us/reader030/viewer/2022011908/5f5ad95c2f81e3013b729bad/html5/thumbnails/3.jpg)
Cursors
• 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)
![Page 4: Python: Beyond the Basics - Recent Proceedings...Python: Beyond the Basics Author Esri Subject 2017 Esri Federal GIS Conference--Presentation Keywords 2017 Esri Federal GIS Conference--Presentation,](https://reader030.vdocuments.us/reader030/viewer/2022011908/5f5ad95c2f81e3013b729bad/html5/thumbnails/4.jpg)
Data Access Module Cursors
![Page 5: Python: Beyond the Basics - Recent Proceedings...Python: Beyond the Basics Author Esri Subject 2017 Esri Federal GIS Conference--Presentation Keywords 2017 Esri Federal GIS Conference--Presentation,](https://reader030.vdocuments.us/reader030/viewer/2022011908/5f5ad95c2f81e3013b729bad/html5/thumbnails/5.jpg)
Required Arguments
• 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”]
![Page 6: Python: Beyond the Basics - Recent Proceedings...Python: Beyond the Basics Author Esri Subject 2017 Esri Federal GIS Conference--Presentation Keywords 2017 Esri Federal GIS Conference--Presentation,](https://reader030.vdocuments.us/reader030/viewer/2022011908/5f5ad95c2f81e3013b729bad/html5/thumbnails/6.jpg)
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.
Used as shortcuts in place of field names
![Page 7: Python: Beyond the Basics - Recent Proceedings...Python: Beyond the Basics Author Esri Subject 2017 Esri Federal GIS Conference--Presentation Keywords 2017 Esri Federal GIS Conference--Presentation,](https://reader030.vdocuments.us/reader030/viewer/2022011908/5f5ad95c2f81e3013b729bad/html5/thumbnails/7.jpg)
arcpy.da.SearchCursor
arcpy.da.SearchCursor(in_table, field_names,
{where_clause}, {spatial_reference},
{explode_to_points}, {sql_clause})
![Page 8: Python: Beyond the Basics - Recent Proceedings...Python: Beyond the Basics Author Esri Subject 2017 Esri Federal GIS Conference--Presentation Keywords 2017 Esri Federal GIS Conference--Presentation,](https://reader030.vdocuments.us/reader030/viewer/2022011908/5f5ad95c2f81e3013b729bad/html5/thumbnails/8.jpg)
arcpy.da.SearchCursor
![Page 9: Python: Beyond the Basics - Recent Proceedings...Python: Beyond the Basics Author Esri Subject 2017 Esri Federal GIS Conference--Presentation Keywords 2017 Esri Federal GIS Conference--Presentation,](https://reader030.vdocuments.us/reader030/viewer/2022011908/5f5ad95c2f81e3013b729bad/html5/thumbnails/9.jpg)
arcpy.da.SearchCursor
![Page 10: Python: Beyond the Basics - Recent Proceedings...Python: Beyond the Basics Author Esri Subject 2017 Esri Federal GIS Conference--Presentation Keywords 2017 Esri Federal GIS Conference--Presentation,](https://reader030.vdocuments.us/reader030/viewer/2022011908/5f5ad95c2f81e3013b729bad/html5/thumbnails/10.jpg)
arcpy.da.SearchCursor
![Page 11: Python: Beyond the Basics - Recent Proceedings...Python: Beyond the Basics Author Esri Subject 2017 Esri Federal GIS Conference--Presentation Keywords 2017 Esri Federal GIS Conference--Presentation,](https://reader030.vdocuments.us/reader030/viewer/2022011908/5f5ad95c2f81e3013b729bad/html5/thumbnails/11.jpg)
Demo: Search
Cursor with
csv module
![Page 12: Python: Beyond the Basics - Recent Proceedings...Python: Beyond the Basics Author Esri Subject 2017 Esri Federal GIS Conference--Presentation Keywords 2017 Esri Federal GIS Conference--Presentation,](https://reader030.vdocuments.us/reader030/viewer/2022011908/5f5ad95c2f81e3013b729bad/html5/thumbnails/12.jpg)
![Page 13: Python: Beyond the Basics - Recent Proceedings...Python: Beyond the Basics Author Esri Subject 2017 Esri Federal GIS Conference--Presentation Keywords 2017 Esri Federal GIS Conference--Presentation,](https://reader030.vdocuments.us/reader030/viewer/2022011908/5f5ad95c2f81e3013b729bad/html5/thumbnails/13.jpg)
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.
![Page 14: Python: Beyond the Basics - Recent Proceedings...Python: Beyond the Basics Author Esri Subject 2017 Esri Federal GIS Conference--Presentation Keywords 2017 Esri Federal GIS Conference--Presentation,](https://reader030.vdocuments.us/reader030/viewer/2022011908/5f5ad95c2f81e3013b729bad/html5/thumbnails/14.jpg)
arcpy.da.UpdateCursor
![Page 15: Python: Beyond the Basics - Recent Proceedings...Python: Beyond the Basics Author Esri Subject 2017 Esri Federal GIS Conference--Presentation Keywords 2017 Esri Federal GIS Conference--Presentation,](https://reader030.vdocuments.us/reader030/viewer/2022011908/5f5ad95c2f81e3013b729bad/html5/thumbnails/15.jpg)
arcpy.da.UpdateCursor
![Page 16: Python: Beyond the Basics - Recent Proceedings...Python: Beyond the Basics Author Esri Subject 2017 Esri Federal GIS Conference--Presentation Keywords 2017 Esri Federal GIS Conference--Presentation,](https://reader030.vdocuments.us/reader030/viewer/2022011908/5f5ad95c2f81e3013b729bad/html5/thumbnails/16.jpg)
arcpy.da.UpdateCursor
![Page 17: Python: Beyond the Basics - Recent Proceedings...Python: Beyond the Basics Author Esri Subject 2017 Esri Federal GIS Conference--Presentation Keywords 2017 Esri Federal GIS Conference--Presentation,](https://reader030.vdocuments.us/reader030/viewer/2022011908/5f5ad95c2f81e3013b729bad/html5/thumbnails/17.jpg)
arcpy.da.InsertCursor
arcpy.da.InsertCursor(in_table, field_names)
Method Explanation
insertRow (row) Inserts a row into a table.
![Page 18: Python: Beyond the Basics - Recent Proceedings...Python: Beyond the Basics Author Esri Subject 2017 Esri Federal GIS Conference--Presentation Keywords 2017 Esri Federal GIS Conference--Presentation,](https://reader030.vdocuments.us/reader030/viewer/2022011908/5f5ad95c2f81e3013b729bad/html5/thumbnails/18.jpg)
arcpy.da.InsertCursor
![Page 19: Python: Beyond the Basics - Recent Proceedings...Python: Beyond the Basics Author Esri Subject 2017 Esri Federal GIS Conference--Presentation Keywords 2017 Esri Federal GIS Conference--Presentation,](https://reader030.vdocuments.us/reader030/viewer/2022011908/5f5ad95c2f81e3013b729bad/html5/thumbnails/19.jpg)
arcpy.da.InsertCursor
![Page 20: Python: Beyond the Basics - Recent Proceedings...Python: Beyond the Basics Author Esri Subject 2017 Esri Federal GIS Conference--Presentation Keywords 2017 Esri Federal GIS Conference--Presentation,](https://reader030.vdocuments.us/reader030/viewer/2022011908/5f5ad95c2f81e3013b729bad/html5/thumbnails/20.jpg)
arcpy.da.InsertCursor
![Page 21: Python: Beyond the Basics - Recent Proceedings...Python: Beyond the Basics Author Esri Subject 2017 Esri Federal GIS Conference--Presentation Keywords 2017 Esri Federal GIS Conference--Presentation,](https://reader030.vdocuments.us/reader030/viewer/2022011908/5f5ad95c2f81e3013b729bad/html5/thumbnails/21.jpg)
Working with Geometry Objects
• Reading geometries
- arcpy.da.SearchCursor
- arcpy.da.UpdateCursor
• Writing geometries
- arcpy.da.UpdateCursor
- arcpy.da.InsertCursor
• Work with geoprocessing tools
![Page 22: Python: Beyond the Basics - Recent Proceedings...Python: Beyond the Basics Author Esri Subject 2017 Esri Federal GIS Conference--Presentation Keywords 2017 Esri Federal GIS Conference--Presentation,](https://reader030.vdocuments.us/reader030/viewer/2022011908/5f5ad95c2f81e3013b729bad/html5/thumbnails/22.jpg)
Python in ArcGIS Pro
• Changes to functionality in arcpy
• Upgrade to Python 3
- print statement function
- print('This will print safely in Python 2 and 3')
• Conda
![Page 23: Python: Beyond the Basics - Recent Proceedings...Python: Beyond the Basics Author Esri Subject 2017 Esri Federal GIS Conference--Presentation Keywords 2017 Esri Federal GIS Conference--Presentation,](https://reader030.vdocuments.us/reader030/viewer/2022011908/5f5ad95c2f81e3013b729bad/html5/thumbnails/23.jpg)
Using GIS and Python to Save Time and Save the Bay
![Page 24: Python: Beyond the Basics - Recent Proceedings...Python: Beyond the Basics Author Esri Subject 2017 Esri Federal GIS Conference--Presentation Keywords 2017 Esri Federal GIS Conference--Presentation,](https://reader030.vdocuments.us/reader030/viewer/2022011908/5f5ad95c2f81e3013b729bad/html5/thumbnails/24.jpg)
![Page 25: Python: Beyond the Basics - Recent Proceedings...Python: Beyond the Basics Author Esri Subject 2017 Esri Federal GIS Conference--Presentation Keywords 2017 Esri Federal GIS Conference--Presentation,](https://reader030.vdocuments.us/reader030/viewer/2022011908/5f5ad95c2f81e3013b729bad/html5/thumbnails/25.jpg)
Collected Data
![Page 26: Python: Beyond the Basics - Recent Proceedings...Python: Beyond the Basics Author Esri Subject 2017 Esri Federal GIS Conference--Presentation Keywords 2017 Esri Federal GIS Conference--Presentation,](https://reader030.vdocuments.us/reader030/viewer/2022011908/5f5ad95c2f81e3013b729bad/html5/thumbnails/26.jpg)
Program: Baltimore Harbor Study Center
Vessels: Snowgoose
General Locations: Fort Carroll, Port of Baltimore, Patapsco River, Baltimore Chesapeake Bay
Collection Methods Used: Otter trawls and oyster hand scrapes
SPECIES NAME MARCH APRIL MAY JUNE JULY AUG SEPT OCT NOV DEC TOTAL
Alewife
American Eel
American Shad
Atlantic Menhaden
Bay Anchovy
Black Crappie
Black Drum
Black Sea Bass
Blenny
Blue Crab
Blue Crab(bushels)
Blue Fish
Blueback Herring
Bluegill
Bullhead
Butterfish
Carp
Reported Data
![Page 27: Python: Beyond the Basics - Recent Proceedings...Python: Beyond the Basics Author Esri Subject 2017 Esri Federal GIS Conference--Presentation Keywords 2017 Esri Federal GIS Conference--Presentation,](https://reader030.vdocuments.us/reader030/viewer/2022011908/5f5ad95c2f81e3013b729bad/html5/thumbnails/27.jpg)
Python and Model Builder
![Page 28: Python: Beyond the Basics - Recent Proceedings...Python: Beyond the Basics Author Esri Subject 2017 Esri Federal GIS Conference--Presentation Keywords 2017 Esri Federal GIS Conference--Presentation,](https://reader030.vdocuments.us/reader030/viewer/2022011908/5f5ad95c2f81e3013b729bad/html5/thumbnails/28.jpg)
Python Script 1: Combines and reformats tables for reporting1. Populate fields for month and year
2. Join the sample table to the species table
3. Pivot the table
4. Add fields for missing months
5. Summarize the total number of each species caught by month, year, and program
6. Calculate Simpsons Diversity Index
7. Create feature class showing where each species is collected
Python and Model Builder
![Page 29: Python: Beyond the Basics - Recent Proceedings...Python: Beyond the Basics Author Esri Subject 2017 Esri Federal GIS Conference--Presentation Keywords 2017 Esri Federal GIS Conference--Presentation,](https://reader030.vdocuments.us/reader030/viewer/2022011908/5f5ad95c2f81e3013b729bad/html5/thumbnails/29.jpg)
Python Script 1: Combines and reformats tables for reporting1. Populate fields for month and year
2. Join the sample table to the species table
3. Pivot the table
4. Add fields for missing months
5. Summarize the total number of each species caught by month, year, and program
6. Calculate Simpsons Diversity Index
7. Create feature class showing where each species is collected
Python and Model Builder
![Page 30: Python: Beyond the Basics - Recent Proceedings...Python: Beyond the Basics Author Esri Subject 2017 Esri Federal GIS Conference--Presentation Keywords 2017 Esri Federal GIS Conference--Presentation,](https://reader030.vdocuments.us/reader030/viewer/2022011908/5f5ad95c2f81e3013b729bad/html5/thumbnails/30.jpg)
Python Script 1: Combines and reformats tables for reporting
Python and Model Builder
1. Populate fields for month and year
2. Join the sample table to the species table
3. Pivot the table
4. Add fields for missing months
5. Summarize the total number of each species caught by month, year, and program
6. Calculate Simpsons Diversity Index
7. Create feature class showing where each species is collected
![Page 31: Python: Beyond the Basics - Recent Proceedings...Python: Beyond the Basics Author Esri Subject 2017 Esri Federal GIS Conference--Presentation Keywords 2017 Esri Federal GIS Conference--Presentation,](https://reader030.vdocuments.us/reader030/viewer/2022011908/5f5ad95c2f81e3013b729bad/html5/thumbnails/31.jpg)
Python Script 1: Combines and reformats tables for reporting1. Populate fields for month and year
2. Join the sample table to the species table
3. Pivot the table
4. Add fields for missing months
5. Summarize the total number of each species caught by month, year, and program
6. Calculate Simpsons Diversity Index
7. Create feature class showing where each species is collected
Python and Model Builder
![Page 32: Python: Beyond the Basics - Recent Proceedings...Python: Beyond the Basics Author Esri Subject 2017 Esri Federal GIS Conference--Presentation Keywords 2017 Esri Federal GIS Conference--Presentation,](https://reader030.vdocuments.us/reader030/viewer/2022011908/5f5ad95c2f81e3013b729bad/html5/thumbnails/32.jpg)
Python Script 1: Combines and reformats tables for reporting1. Populate fields for month and year
2. Join the sample table to the species table
3. Pivot the table
4. Add fields for missing months
5. Summarize the total number of each species caught by month, year, and program
6. Calculate Simpsons Diversity Index
7. Create feature class showing where each species is collected
Python and Model Builder
![Page 33: Python: Beyond the Basics - Recent Proceedings...Python: Beyond the Basics Author Esri Subject 2017 Esri Federal GIS Conference--Presentation Keywords 2017 Esri Federal GIS Conference--Presentation,](https://reader030.vdocuments.us/reader030/viewer/2022011908/5f5ad95c2f81e3013b729bad/html5/thumbnails/33.jpg)
Python Script 1: Combines and reformats tables for reporting1. Populate fields for month and year
2. Join the sample table to the species table
3. Pivot the table
4. Add fields for missing months
5. Summarize the total number of each species caught by month, year, and program
6. Calculate Simpsons Diversity Index
7. Create feature class showing where each species is collected
Python and Model Builder
![Page 34: Python: Beyond the Basics - Recent Proceedings...Python: Beyond the Basics Author Esri Subject 2017 Esri Federal GIS Conference--Presentation Keywords 2017 Esri Federal GIS Conference--Presentation,](https://reader030.vdocuments.us/reader030/viewer/2022011908/5f5ad95c2f81e3013b729bad/html5/thumbnails/34.jpg)
Python Script 1: Combines and reformats tables for reporting1. Populate fields for month and year
2. Join the sample table to the species table
3. Pivot the table
4. Add fields for missing months
5. Summarize the total number of each species caught by month, year, and program
6. Calculate Simpsons Diversity Index
7. Create feature class showing where each species is collected
Python and Model Builder
![Page 35: Python: Beyond the Basics - Recent Proceedings...Python: Beyond the Basics Author Esri Subject 2017 Esri Federal GIS Conference--Presentation Keywords 2017 Esri Federal GIS Conference--Presentation,](https://reader030.vdocuments.us/reader030/viewer/2022011908/5f5ad95c2f81e3013b729bad/html5/thumbnails/35.jpg)
Python Script 1: Combines and reformats tables for reporting1. Populate fields for month and year
2. Join the sample table to the species table
3. Pivot the table
4. Add fields for missing months
5. Summarize the total number of each species caught by month, year, and program
6. Calculate Simpsons Diversity Index
7. Create feature class showing where each species is collected
Python and Model Builder
D = Ʃ n(n-1)N(N-1)
n = total number of organisms of a particular speciesN = total number of organisms of all species
![Page 36: Python: Beyond the Basics - Recent Proceedings...Python: Beyond the Basics Author Esri Subject 2017 Esri Federal GIS Conference--Presentation Keywords 2017 Esri Federal GIS Conference--Presentation,](https://reader030.vdocuments.us/reader030/viewer/2022011908/5f5ad95c2f81e3013b729bad/html5/thumbnails/36.jpg)
Python Script 1: Combines and reformats tables for reporting1. Populate fields for month and year
2. Join the sample table to the species table
3. Pivot the table
4. Add fields for missing months
5. Summarize the total number of each species caught by month, year, and program
6. Calculate Simpsons Diversity Index
7. Create feature class showing where each species is collected
Python and Model Builder
![Page 37: Python: Beyond the Basics - Recent Proceedings...Python: Beyond the Basics Author Esri Subject 2017 Esri Federal GIS Conference--Presentation Keywords 2017 Esri Federal GIS Conference--Presentation,](https://reader030.vdocuments.us/reader030/viewer/2022011908/5f5ad95c2f81e3013b729bad/html5/thumbnails/37.jpg)
Python Script 2: Get filtered header information
Program: Baltimore Harbor Study Center
Vessels: Snowgoose
General Locations: Fort Carroll, Port of Baltimore, Patapsco River, Baltimore Chesapeake Bay
Collection Methods Used: Otter trawls and oyster hand scrapes
SPECIES NAME MARCH APRIL MAY JUNE JULY AUG SEPT OCT NOV DEC TOTAL
Alewife
American Eel
American Shad
Atlantic Menhaden
Bay Anchovy
Black Crappie
Black Drum
Black Sea Bass
Blenny
Blue Crab
Blue Crab(bushels)
Blue Fish
Blueback Herring
Bluegill
Bullhead
Butterfish
Python and Model Builder
![Page 38: Python: Beyond the Basics - Recent Proceedings...Python: Beyond the Basics Author Esri Subject 2017 Esri Federal GIS Conference--Presentation Keywords 2017 Esri Federal GIS Conference--Presentation,](https://reader030.vdocuments.us/reader030/viewer/2022011908/5f5ad95c2f81e3013b729bad/html5/thumbnails/38.jpg)
Python and Model Builder
![Page 39: Python: Beyond the Basics - Recent Proceedings...Python: Beyond the Basics Author Esri Subject 2017 Esri Federal GIS Conference--Presentation Keywords 2017 Esri Federal GIS Conference--Presentation,](https://reader030.vdocuments.us/reader030/viewer/2022011908/5f5ad95c2f81e3013b729bad/html5/thumbnails/39.jpg)
Python and Model Builder
![Page 40: Python: Beyond the Basics - Recent Proceedings...Python: Beyond the Basics Author Esri Subject 2017 Esri Federal GIS Conference--Presentation Keywords 2017 Esri Federal GIS Conference--Presentation,](https://reader030.vdocuments.us/reader030/viewer/2022011908/5f5ad95c2f81e3013b729bad/html5/thumbnails/40.jpg)
Web Application
![Page 42: Python: Beyond the Basics - Recent Proceedings...Python: Beyond the Basics Author Esri Subject 2017 Esri Federal GIS Conference--Presentation Keywords 2017 Esri Federal GIS Conference--Presentation,](https://reader030.vdocuments.us/reader030/viewer/2022011908/5f5ad95c2f81e3013b729bad/html5/thumbnails/42.jpg)
Additional Python Sessions
• Python: Building Geoprocessing Tools (146 B)
- Tuesday 4:00pm
• Advanced Map Automation with Python (146 B)
- Tuesday 5:15pm
![Page 43: Python: Beyond the Basics - Recent Proceedings...Python: Beyond the Basics Author Esri Subject 2017 Esri Federal GIS Conference--Presentation Keywords 2017 Esri Federal GIS Conference--Presentation,](https://reader030.vdocuments.us/reader030/viewer/2022011908/5f5ad95c2f81e3013b729bad/html5/thumbnails/43.jpg)
Please Take Our Survey on the Esri Events App!
Select the session
you attended
Scroll down to find
the survey
Complete Answers
and Select “Submit”
Download the Esri Events
app and find your event
![Page 44: Python: Beyond the Basics - Recent Proceedings...Python: Beyond the Basics Author Esri Subject 2017 Esri Federal GIS Conference--Presentation Keywords 2017 Esri Federal GIS Conference--Presentation,](https://reader030.vdocuments.us/reader030/viewer/2022011908/5f5ad95c2f81e3013b729bad/html5/thumbnails/44.jpg)
Print Your Certificate of Attendance
Print stations located in the 140 Concourse
Monday
12:30 PM – 6:30 PM
GIS Solutions Expo,
Hall B
5:15 PM– 6:30 PM
Expo Social,
Hall B
Tuesday
10:45 AM– 5:15 PM
GIS Solutions Expo,
Hall B
6:30 PM– 9:30 PM
Networking Reception,
Smithsonian National Air
and Space Museum
![Page 45: Python: Beyond the Basics - Recent Proceedings...Python: Beyond the Basics Author Esri Subject 2017 Esri Federal GIS Conference--Presentation Keywords 2017 Esri Federal GIS Conference--Presentation,](https://reader030.vdocuments.us/reader030/viewer/2022011908/5f5ad95c2f81e3013b729bad/html5/thumbnails/45.jpg)