python: beyond the basics · python sessions at fedgis session tuesday wednesday python: an...

37
Derek J. Nelson Python: Beyond the Basics

Upload: others

Post on 13-Aug-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Python: Beyond the Basics · 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

Derek J. Nelson

Python: Beyond the Basics

Page 2: Python: Beyond the Basics · 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

Topics Covered

• Cursors

• Geometry objects

• Leveraging other Python modules

Page 3: Python: Beyond the Basics · 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

• 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

Page 4: Python: Beyond the Basics · 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

Data Access Module Cursors

Page 5: Python: Beyond the Basics · 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

• 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

Page 6: Python: Beyond the Basics · 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

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.

Page 7: Python: Beyond the Basics · 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

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

arcpy.da.SearchCursor – optional where clause

parameter

Page 9: Python: Beyond the Basics · 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

# 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]))

Page 10: Python: Beyond the Basics · 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

Demo: Search

Cursor and Email

Update

Page 11: Python: Beyond the Basics · 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

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 12: Python: Beyond the Basics · 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

arcpy.da.UpdateCursor

Page 13: Python: Beyond the Basics · 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

arcpy.da.UpdateCursor

Page 14: Python: Beyond the Basics · 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

arcpy.da.UpdateCursor

Page 15: Python: Beyond the Basics · 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

arcpy.da.InsertCursor

arcpy.da.InsertCursor(in_table, field_names)

Method Explanation

insertRow (row) Inserts a row into a table.

Page 16: Python: Beyond the Basics · 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

arcpy.da.InsertCursor

Page 17: Python: Beyond the Basics · 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
Page 18: Python: Beyond the Basics · 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
Page 19: Python: Beyond the Basics · 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

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

Using geometry as input with geoprocessing tools

Page 21: Python: Beyond the Basics · 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

Demo: Cursor to

Collector

Page 22: Python: Beyond the Basics · 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

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 23: Python: Beyond the Basics · 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

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 24: Python: Beyond the Basics · 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

Demo: Python

Package Manager

Page 25: Python: Beyond the Basics · 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
Page 26: Python: Beyond the Basics · 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

Create and Prep

Jesse Parker

Page 27: Python: Beyond the Basics · 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

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

Page 28: Python: Beyond the Basics · 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
Page 29: Python: Beyond the Basics · 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

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

Page 30: Python: Beyond the Basics · 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

Questions?

Jesse Parker

[email protected]

Page 31: Python: Beyond the Basics · 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

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

Page 32: Python: Beyond the Basics · 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

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

Page 33: Python: Beyond the Basics · 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

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

Page 34: Python: Beyond the Basics · 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

Presenter Names

Presentation Title

Page 35: Python: Beyond the Basics · 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
Page 36: Python: Beyond the Basics · 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

Section Subhead

Section Header

Page 37: Python: Beyond the Basics · 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

Presenter(s)

Demo Title