python: beyond the basics - recent proceedings...python: beyond the basics author esri subject 2017...

45
Python: Beyond the Basics Brittney White, Esri Katie Leaverton, Chesapeake Bay Foundation

Upload: others

Post on 19-Jul-2020

22 views

Category:

Documents


0 download

TRANSCRIPT

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,
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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,
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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

Python and Model Builder

Page 41: 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,

Resources for getting started

•http://esriurl.com/FedGISPython

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,

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,

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,

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,