Python: An IntroductionBrittney White, Esri
Katie Leaverton, Chesapeake Bay Foundation
What is Python?
• Simple and easy to learn
• Free and open source
• Cross platform
• Scripting language of ArcGIS
Why use Python and ArcGIS?
• Automate repetitive tasks
- Data management
- Spatial analysis
- Map automation
• Make your own geoprocessing tools
What is ArcPy?
• Access point to ArcGIS functionality
through Python
1. Geoprocessing tools
2. Functions like ListFeatureClasses,
Describe
3. Classes like Polygon,
SpatialReference
4. Modules
Where do I write Python code?
• IDE
- PyCharm, Spyder, Wing IDE, Python IDLE,…
• Python window in ArcGIS
Demo: Python
Window
Goal
• List the shapefiles in folder
• Check the coordinate system of
each shapefile
• Project the shapefiles into NAD
1983 State Plane Maryland FIPS
1900 coordinate system
• Migrate shapefiles into a project file
geodatabase
Demo: Prepare
shapefiles for
analysis
Using GIS and Python to Save Time and Save the Bay
Reporting RequirementsProgram: 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
Old Process
• Write in field notebook
• Add to spreadsheet
• Send to Education Operations Coordinator
• Combine spreadsheets and summarize for reporting
Old Process
• Inaccurate
• Time consuming
• Inefficient
• Provided limited use of the data
Problems
Goals
1. Collect species data in field across programs accurately
2. Automate the data processing for reporting
3. Provide the data back to the educators in a useful way
The Platform
• SQL Server
• ArcGIS Desktop
• ArcGIS Server
• ArcGIS Online
• Collector for ArcGIS
• Python
• Model Builder
• Web app Builder
Our Solution
Step 1: Designing the SQL Server Database
• SQL server database
• 2 related tables (Samples and Species) with a one to many relationship
• Published to ArcGIS Server and shared through ArcGIS Online
SQL Server and ArcMap
Step 1: Designing the SQL Server Database
• SQL server database
• 2 related tables (Samples and Species) with a one to many relationship
• Published to ArcGIS Server and shared through ArcGIS Online
SQL Server and ArcMap
Step 2: Publish Tables and Collect Data
ArcGIS Server, ArcGIS Online, and Collector
Goals
1. Collect species data in field across programs accurately
2. Automate the data processing for reporting
3. Provide the data back to the educators in a useful way
Step 3: Automate Reporting
Python and Model Builder
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
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
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
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
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
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
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
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
Python and Model Builder
Python Script 2: Get 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
Model
Python and Model Builder
Web Application
Additional Python Sessions
• Python: Beyond the Basics (146 B)
- Tuesday 2:45pm
• Python: Building Geoprocessing Tools (146 B)
- Tuesday 4:00pm
• Advanced Map Automation with Python (146 B)
- Tuesday 5:15pm
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
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