Python: An Introduction · Python Sessions at FedGIS Session Tuesday Wednesday Python: An...
Transcript of Python: An Introduction · Python Sessions at FedGIS Session Tuesday Wednesday Python: An...
Derek J. Nelson
Python: An Introduction
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?
What is Python?
• Simple and easy to learn
• Free and open source
• Cross platform
• Scripting language of ArcGIS
Why use Python with ArcGIS?
• Automate repetitive tasks
- Data management
- Spatial analysis
- Map automation
• Make your own geoprocessing tools
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
Demo: ArcGIS Pro
Python Reference
Where do I write Python code?
• Python window in ArcGIS
• ArcGIS Notebooks
• Integrated Development Environment (IDE)
- PyCharm, Spyder, Wing IDE, Python IDLE,…
Demo: Python
window, ArcGIS
Notebooks, & IDE
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
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
Demo:
Geoprocessing
tool syntax
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
Demo: Using for
loops with lists
How do I access data properties?
• arcpy.Describe()
- Returns a describe object
- Describe object has dynamic properties
How do I access data properties?• …to control the flow the script?
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()
Python Problem SolvingBY: AMANDA WAGGONER
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.
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.
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.
Python Code Rasters
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.
Rhode Island
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…
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.
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
QUESTIONS?
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
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
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