python: beyond the basics - recent proceedings...python: beyond the basics author esri subject 2017...
TRANSCRIPT
Python: Beyond the BasicsBrittney White, Esri
Katie Leaverton, Chesapeake Bay Foundation
Topics Covered
• Cursors
• Geometry objects
• Leveraging other Python modules
• Python in ArcGIS Pro
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)
Data Access Module Cursors
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”]
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
arcpy.da.SearchCursor
arcpy.da.SearchCursor(in_table, field_names,
{where_clause}, {spatial_reference},
{explode_to_points}, {sql_clause})
arcpy.da.SearchCursor
arcpy.da.SearchCursor
arcpy.da.SearchCursor
Demo: Search
Cursor with
csv module
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
arcpy.da.InsertCursor
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
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
Using GIS and Python to Save Time and Save the Bay
Collected Data
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
Python and Model Builder
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
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
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
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
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
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
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
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
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
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
Python and Model Builder
Python and Model Builder
Web Application
Additional Python Sessions
• Python: Building Geoprocessing Tools (146 B)
- Tuesday 4:00pm
• Advanced Map Automation with Python (146 B)
- Tuesday 5:15pm
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
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