python: beyond the basics - esri · python: beyond the basics author: esri subject: 2019 esri...

28
Python: Beyond the Basics Brittney White

Upload: others

Post on 19-Jul-2020

42 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Python: Beyond the Basics - Esri · Python: Beyond the Basics Author: Esri Subject: 2019 Esri Federal GIS Conference Presentation Keywords: 2019 Esri Federal GIS Conference – Presentation,

Python: Beyond the BasicsBrittney White

Page 2: Python: Beyond the Basics - Esri · Python: Beyond the Basics Author: Esri Subject: 2019 Esri Federal GIS Conference Presentation Keywords: 2019 Esri Federal GIS Conference – Presentation,

Topics Covered

• Cursors• Geometry objects• Leveraging other Python modules

Page 3: Python: Beyond the Basics - Esri · Python: Beyond the Basics Author: Esri Subject: 2019 Esri Federal GIS Conference Presentation Keywords: 2019 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 - Esri · Python: Beyond the Basics Author: Esri Subject: 2019 Esri Federal GIS Conference Presentation Keywords: 2019 Esri Federal GIS Conference – Presentation,

Data Access Module Cursors

Page 5: Python: Beyond the Basics - Esri · Python: Beyond the Basics Author: Esri Subject: 2019 Esri Federal GIS Conference Presentation Keywords: 2019 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 - Esri · Python: Beyond the Basics Author: Esri Subject: 2019 Esri Federal GIS Conference Presentation Keywords: 2019 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,ycoordinates.

• 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 - Esri · Python: Beyond the Basics Author: Esri Subject: 2019 Esri Federal GIS Conference Presentation Keywords: 2019 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 - Esri · Python: Beyond the Basics Author: Esri Subject: 2019 Esri Federal GIS Conference Presentation Keywords: 2019 Esri Federal GIS Conference – Presentation,

arcpy.da.SearchCursor – optional where clause parameter

Page 9: Python: Beyond the Basics - Esri · Python: Beyond the Basics Author: Esri Subject: 2019 Esri Federal GIS Conference Presentation Keywords: 2019 Esri Federal GIS Conference – Presentation,

# Open a Search Cursor, print resultstable = "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]))

Page 10: Python: Beyond the Basics - Esri · Python: Beyond the Basics Author: Esri Subject: 2019 Esri Federal GIS Conference Presentation Keywords: 2019 Esri Federal GIS Conference – Presentation,

# Open a Search Cursor, print resultstable = "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]))

Page 11: Python: Beyond the Basics - Esri · Python: Beyond the Basics Author: Esri Subject: 2019 Esri Federal GIS Conference Presentation Keywords: 2019 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 ExplanationdeleteRow () Deletes the current rowupdateRow (row) Updates the current row in the table.

Page 12: Python: Beyond the Basics - Esri · Python: Beyond the Basics Author: Esri Subject: 2019 Esri Federal GIS Conference Presentation Keywords: 2019 Esri Federal GIS Conference – Presentation,

arcpy.da.UpdateCursor

Page 13: Python: Beyond the Basics - Esri · Python: Beyond the Basics Author: Esri Subject: 2019 Esri Federal GIS Conference Presentation Keywords: 2019 Esri Federal GIS Conference – Presentation,

arcpy.da.UpdateCursor

Page 14: Python: Beyond the Basics - Esri · Python: Beyond the Basics Author: Esri Subject: 2019 Esri Federal GIS Conference Presentation Keywords: 2019 Esri Federal GIS Conference – Presentation,

arcpy.da.UpdateCursor

Page 15: Python: Beyond the Basics - Esri · Python: Beyond the Basics Author: Esri Subject: 2019 Esri Federal GIS Conference Presentation Keywords: 2019 Esri Federal GIS Conference – Presentation,

arcpy.da.InsertCursor

arcpy.da.InsertCursor(in_table, field_names)

Method ExplanationinsertRow (row) Inserts a row into a table.

Page 16: Python: Beyond the Basics - Esri · Python: Beyond the Basics Author: Esri Subject: 2019 Esri Federal GIS Conference Presentation Keywords: 2019 Esri Federal GIS Conference – Presentation,

arcpy.da.InsertCursor

Page 17: Python: Beyond the Basics - Esri · Python: Beyond the Basics Author: Esri Subject: 2019 Esri Federal GIS Conference Presentation Keywords: 2019 Esri Federal GIS Conference – Presentation,

arcpy.da.InsertCursor

Page 18: Python: Beyond the Basics - Esri · Python: Beyond the Basics Author: Esri Subject: 2019 Esri Federal GIS Conference Presentation Keywords: 2019 Esri Federal GIS Conference – Presentation,

arcpy.da.InsertCursor

Page 19: Python: Beyond the Basics - Esri · Python: Beyond the Basics Author: Esri Subject: 2019 Esri Federal GIS Conference Presentation Keywords: 2019 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 20: Python: Beyond the Basics - Esri · Python: Beyond the Basics Author: Esri Subject: 2019 Esri Federal GIS Conference Presentation Keywords: 2019 Esri Federal GIS Conference – Presentation,

Using geometry as input with geoprocessing tools

Page 21: Python: Beyond the Basics - Esri · Python: Beyond the Basics Author: Esri Subject: 2019 Esri Federal GIS Conference Presentation Keywords: 2019 Esri Federal GIS Conference – Presentation,

Demo: Which buffers overlap?

Page 22: Python: Beyond the Basics - Esri · Python: Beyond the Basics Author: Esri Subject: 2019 Esri Federal GIS Conference Presentation Keywords: 2019 Esri Federal GIS Conference – Presentation,

Which buffers overlap?

Page 23: Python: Beyond the Basics - Esri · Python: Beyond the Basics Author: Esri Subject: 2019 Esri Federal GIS Conference Presentation Keywords: 2019 Esri Federal GIS Conference – Presentation,

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

Page 24: Python: Beyond the Basics - Esri · Python: Beyond the Basics Author: Esri Subject: 2019 Esri Federal GIS Conference Presentation Keywords: 2019 Esri Federal GIS Conference – Presentation,

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

Page 25: Python: Beyond the Basics - Esri · Python: Beyond the Basics Author: Esri Subject: 2019 Esri Federal GIS Conference Presentation Keywords: 2019 Esri Federal GIS Conference – Presentation,

Demo: Python Package Manager

Page 26: Python: Beyond the Basics - Esri · Python: Beyond the Basics Author: Esri Subject: 2019 Esri Federal GIS Conference Presentation Keywords: 2019 Esri Federal GIS Conference – Presentation,

Print Your Certificate of AttendancePrint Stations Located at L Street Bridge

Tuesday Wednesday12:30 pm – 6:30 pm GIS Solutions Expo Hall D

5:15 pm – 6:30 pm GIS Solutions Expo SocialHall D

10:45 am – 5:15 pm GIS Solutions Expo Hall D

6:30 pm – 9:00 pm Networking ReceptionNational Building Museum

Page 27: Python: Beyond the Basics - Esri · Python: Beyond the Basics Author: Esri Subject: 2019 Esri Federal GIS Conference Presentation Keywords: 2019 Esri Federal GIS Conference – Presentation,

Please Take Our Survey on the AppDownload the Esri Events app and find your event

Select the session you attended

Scroll down to find the feedback section

Complete answersand select “Submit”

Page 28: Python: Beyond the Basics - Esri · Python: Beyond the Basics Author: Esri Subject: 2019 Esri Federal GIS Conference Presentation Keywords: 2019 Esri Federal GIS Conference – Presentation,

Python Sessions at #FedGIS

Session Tuesday WednesdayPython: An Introduction 1:45 pm – 2:45 pm in 202B 8:30 am – 9:30 am in 202BPython: Beyond the Basics 4:15 pm – 5:15 pm in 208AB 11:00 am – 12:00 pm in 202BUsing Python in ArcGIS Enterprise

8:30 am – 9:30 am in 201

Python: Building Geoprocessing Tools

5:15 pm – 6:15 pm in 201

Data Science in ArcGIS Using Python and R

4:00 pm – 5:00 pm in 202B

Machine Learning in ArcGIS 4:15 pm – 5:15 pm in 202 A 8:30 am – 9:30 am in 202A