Python: An Introduction · Python Sessions at FedGIS Session Tuesday Wednesday Python: An...

34
Derek J. Nelson Python: An Introduction

Transcript of Python: An Introduction · Python Sessions at FedGIS Session Tuesday Wednesday Python: An...

Page 1: Python: An Introduction · 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

Derek J. Nelson

Python: An Introduction

Page 2: Python: An Introduction · 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

Questions we’ll address

• What is Python?

• Why use Python with ArcGIS?

• What is ArcPy?

• Where do I write Python code?

• How do I run a geoprocessing tool?

• How do I perform batch processing?

• How do I use data properties to control the flow of a script?

Page 3: Python: An Introduction · 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

What is Python?

• Simple and easy to learn

• Free and open source

• Cross platform

• Scripting language of ArcGIS

Page 4: Python: An Introduction · 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

Why use Python with ArcGIS?

• Automate repetitive tasks

- Data management

- Spatial analysis

- Map automation

• Make your own geoprocessing tools

Page 5: Python: An Introduction · 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

What is ArcPy?

• Access point to ArcGIS desktop functionality through Python

1. Geoprocessing tools

2. Functions like ListFeatureClasses, Describe

3. Classes like Polygon, SpatialReference

4. Modules like arcpy.da, arcpy.mp

Page 6: Python: An Introduction · 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

Demo: ArcGIS Pro

Python Reference

Page 7: Python: An Introduction · 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

Where do I write Python code?

• Python window in ArcGIS

• ArcGIS Notebooks

• Integrated Development Environment (IDE)

- PyCharm, Spyder, Wing IDE, Python IDLE,…

Page 8: Python: An Introduction · 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

Demo: Python

window, ArcGIS

Notebooks, & IDE

Page 9: Python: An Introduction · 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

Where do I write Python code?

Development environment

Times you plan to run code

Graphic userinterface

See immediate results in map

Advantages

IDE Multiple No No Debugging

Python window Once No Yes Integrated help

ArcGIS Notebook Multiple No YesData science integration

Script tool Multiple Yes Yes Validation

Page 10: Python: An Introduction · 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

How do I run a geoprocessing tool?

• Tool syntax

- arcpy.toolname_toolboxalias(parameters)

- arcpy.toolboxalias.toolname(parameters)

• Where do I find the syntax?

- Tool help page

- help(arcpy.Buffer_analysis)

- Copy Python command

- Send to Python window

Page 11: Python: An Introduction · 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

Demo:

Geoprocessing

tool syntax

Page 12: Python: An Introduction · 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

How do I perform batch processing?

• arcpy.ListFeatureClasses({wild_card}, {feature_type},

{feature_dataset})

- Returns a list of feature classes in the workspace

• for loops

Page 13: Python: An Introduction · 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

Demo: Using for

loops with lists

Page 14: Python: An Introduction · 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

How do I access data properties?

• arcpy.Describe()

- Returns a describe object

- Describe object has dynamic properties

Page 15: Python: An Introduction · 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

How do I access data properties?• …to control the flow the script?

Page 16: Python: An Introduction · 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

Demo:

Prepare shapefiles

for analysis

Goal ArcPy

List the shapefiles

in a folder

List function -arcpy.ListFeatureClasses()

Check the

coordinate system

of each shapefile

Describe function - arcpy.Describe()

Project the

shapefiles into the

local coordinate

system if needed

Geoprocessing tool -arcpy.Project_management()

Move shapefiles

into a file

geodatabase

Geoprocessing tool -arcpy.CopyFeatures_management()

Page 17: Python: An Introduction · 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
Page 18: Python: An Introduction · 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

Python Problem SolvingBY: AMANDA WAGGONER

Page 19: Python: An Introduction · 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

Disaster Management/FEMA

FEMA, the Federal Emergency Management Agency, is agency ran by the Department of Homeland Security that assists in emergency and disaster preparedness/management.

Many of these issues affect the federal government’s process in solving disaster problems throughout the country.

Satellite imagery and GIS is used to help locate vulnerable sites, areas that need the most support and refuge, and demographic information.

Page 20: Python: An Introduction · 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

Satellite Imagery, GIS, FEMA

FEMA utilizes GIS services and organizes them by disaster type in order to analyze affected areas.

These categories are labeled as severe thunderstorms, earthquakes, hurricanes, etc.

Satellite imagery, shapefiles, KMZ files and other elements of GIS can be used for analysis.

Page 21: Python: An Introduction · 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

Python Solutions

Python in GIS can be used to simplify issues in GIS.

For example, when organizing a large-scale project that contains several facets of GIS analysis,

Python can be used to do several tasks at once.

From data management to structuring a geodatabase, Python can automate procedures in project

management.

Page 22: Python: An Introduction · 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

Python Code Rasters

Page 23: Python: An Introduction · 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
Page 24: Python: An Introduction · 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

2010 Emergency in Rhode Island

As an example, even though my initial project was for academia, I translated that code into a

sample project that is related to the federal government.

Let’s propose that FEMA wanted to analyze a declared emergency from Rhode Island in 2010.

This project would contain raster data that needed to be properly analyzed in order for successful

completion. Let’s propose that FEMA wanted to verify that the satellite imagery that was being

used for this project was in the same spatial reference as the shapefile data collected by FEMA.

Page 25: Python: An Introduction · 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

Rhode Island

Page 26: Python: An Introduction · 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
Page 27: Python: An Introduction · 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
Page 28: Python: An Introduction · 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

Exported Raster into Same Spatial Reference…

How can we double-check that the

rasters that were created are in

the correct spatial reference?

That’s where the power of coding

comes in…

Page 29: Python: An Introduction · 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

What happened?

We know now that this script is still displaying

“Transverse Mercator” which is a projection, and the

emergency areas shapefile is not projected.

Because of this script, we can now make sure to change

either the projection of the raster file, or re-project the

shapefile to match the coordinate system.

Page 30: Python: An Introduction · 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

The Power of Scripting…

If this code was used for a larger-scale project for FEMA,

and there were several states that needed to be analyzed

at once but were placed in the wrong spatial reference as

the parent data… how would this be helpful?

Mitigates going through multiple rasters one-by-one

Mitigates time being spent on a lesser important part of

a larger project

Allows for users to see and track their specific errors

Python allows for an overall better functionality and

workflow across projects that contain several layers of

data

Page 31: Python: An Introduction · 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

QUESTIONS?

Page 32: Python: An Introduction · 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

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 33: Python: An Introduction · 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

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 34: Python: An Introduction · 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

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