IntrotoPython Session1 Slides
IntrotoPython Session1 Slides
1
AhandsonworkshopofferedbytheGeospatialCentroidandCSULibraries
SarahE.Reed,PhD
AssociateConservationScientist
WildlifeConservationSociety
FacultyAffiliate
DepartmentofFish,Wildlife,andConservationBiology
Session1: November8,2013
Introduction
Introduction
WhatisPython?
Pythonsyntax
Runningscripts
Writingscripts
Preview
Introductions:
1) Nameandaffiliation
2) ExperienceusingPythonorotherprogramming
languages
3) HowdoyouplantousePythoninyourownGIS
researchorwork?
Introduction
WhatisPython?
Pythonsyntax
Runningscripts
Writingscripts
Preview
Goalsofworkshop:
1) IntroducethePythonscriptinglanguageandits
applicationinArcGIS10.1;
2) Becomefamiliarwithseveralmethodsforwriting,editing,
andrunninggeoprocessingscriptsusingPython;and
3) ApplyPythonscriptstoautomateaGISworkflow,
determinethepropertiesofaspatialdataset,and
searchandupdateattributeinformationinArcGIS.
Introduction
WhatisPython?
Pythonsyntax
Runningscripts
Writingscripts
Preview
Workshopoutline:
Session1
Introduction
Session2
1) Introduction
7) Review
2) WhatisPython?
8) Automatingworkflows
3) Pythonsyntax
9) Describeobject
4) Runningscripts
10) Cursorobject
5) Writingscripts
11) Troubleshooting
6) Preview
12) Othertopics
WhatisPython?
Pythonsyntax
Runningscripts
Writingscripts
Preview
Demonstration1:
SPreADGIS
(orwhyIlearnedPython)
Introduction
WhatisPython?
Pythonsyntax
Runningscripts
Writingscripts
Preview
Introduction
WhatisPython?
Pythonsyntax
Runningscripts
Writingscripts
Preview
Introduction
WhatisPython?
Pythonsyntax
Runningscripts
Writingscripts
Preview
Introduction
WhatisPython?
Pythonsyntax
Runningscripts
Writingscripts
Preview
Introduction
WhatisPython?
Pythonsyntax
Runningscripts
Writingscripts
Preview
10
Introduction
WhatisPython?
Pythonsyntax
Runningscripts
Writingscripts
Preview
11
a sphericalspreadingloss
b atmosphericabsorption
c foliageandgroundcover loss
d upwindanddownwindloss
e terraineffects
f ambientsoundconditions
Introduction
WhatisPython?
Pythonsyntax
Runningscripts
Writingscripts
Preview
12
cumulativenoisefrom
multiplesources
Introduction
WhatisPython?
Pythonsyntax
Runningscripts
Writingscripts
Preview
13
acousticeffectsforspecies
withvariableauditory
sensitivity
Introduction
WhatisPython?
Pythonsyntax
Runningscripts
Writingscripts
Preview
14
WhatisPython?
Introduction
WhatisPython?
Pythonsyntax
Runningscripts
Writingscripts
Preview
15
ApplicationsofPythoninArcGIS:
1) Automateworkflows
2) Batchprocessdata
3) Manipulatedatatables,geometry,andmapdocs
4) Usefunctionsaccessibleonlybyscripts
Introduction
WhatisPython?
Pythonsyntax
Runningscripts
Writingscripts
Preview
16
AdvantagesofPython:
1) Opensource
2) Crossplatform
3) Objectoriented
Adatastructurethatcombinesdatawithasetofmethods
foraccessingandmanagingthosedata
Introduction
WhatisPython?
Pythonsyntax
Runningscripts
Writingscripts
Preview
17
Pythoneditors:
IntegratedDevelopmentEnvironment(IDE):
Asoftwareapplicationforprogrammingandsoftwaredevelopment
Sourcecodeeditor:
Atexteditorforsoftwarecode,withfeaturesspeciallydesignedto
simplifyandspeedupwritingandeditingofcode
SuggestedPythoneditors:
1) IDLE
2) PythonWin(sourceforge.net)
3) PyScripter(code.google.com/p/pyscripter)
4) Others(wiki.python.org/moin/PythonEditors)
Introduction
WhatisPython?
Pythonsyntax
Runningscripts
Writingscripts
Preview
18
Introduction
WhatisPython?
Pythonsyntax
Runningscripts
Writingscripts
Preview
19
Userresources:
1) Books
2) Websites
www.python.org
forums.arcgis.com
3) ArcPysitepackage(online)
Introduction
WhatisPython?
Pythonsyntax
Runningscripts
Writingscripts
Preview
20
Userresources:
4) Webcourses
PythonforEveryone (free)
BasicsofPython(forArcGIS10) ($32)
UsingPythoninArcGISDesktop10 (free)
PythonScriptingforGeoprocessingWorkflows ($32)
CreatingPythonToolboxesUsingArcGIS10.1 (free)
PythonScriptingforMapAutomationinArcGIS10($32)
5)Instructorledcourses
IntroductiontoGeoprocessingScriptsUsingPython($1515)
Introduction
WhatisPython?
Pythonsyntax
Runningscripts
Writingscripts
Preview
21
Exercise1:
Downloadworkshopdataand
verifysoftwareinstallation
Introduction
WhatisPython?
Pythonsyntax
Runningscripts
Writingscripts
Preview
22
Pythonstructureandsyntax
Introduction
WhatisPython?
Pythonsyntax
Runningscripts
Writingscripts
Preview
23
Pythonstructure:
1) Programsarecomposedofmodules
2) Modulescontainstatements
3) Statementscontainexpressions
4) Expressionscreateandprocessobjects
Introduction
WhatisPython?
Pythonsyntax
Runningscripts
Writingscripts
Preview
24
Object:
Apieceofmemory,withvaluesandassociated
operations;alsoknownasvariables
Typesofobjects:
Numbers
Strings
Lists
Files
Introduction
WhatisPython?
Pythonsyntax
Runningscripts
Writingscripts
Preview
25
Expression:
Processesanobject:x*2
Statement:
Performsatask,viaanexpression:y=x*2
Typesofstatements:
Introduction
Assignment: x=5
Call: open(DataFile)
if/elif/else
import
for,while
WhatisPython?
Pythonsyntax
Runningscripts
Writingscripts
Preview
26
Module:
Alibraryoftools;permanentfileofcode,composedof
statements
Typesofmodules:
Standardlibrarymodules:os,sys,string(moduleindex)
Specializedmodulesorsitepackages:arcpy (sitepackage)
Yourscript
Modules
b.py
Standard
library
modules
a.py
c.py
Introduction
WhatisPython?
Pythonsyntax
Runningscripts
Writingscripts
Preview
27
Pythonsyntax:
Casesensitivity(DataFiledatafile)
Indentation
Filepaths(/,\\ orrstring)
Quotationmarks(,)
Commenting (#)
Introduction
WhatisPython?
Pythonsyntax
Runningscripts
Writingscripts
Preview
28
Introduction
WhatisPython?
Pythonsyntax
Runningscripts
Writingscripts
Preview
29
Pythonstructure:
1) Programsarecomposedofmodules
2) Modulescontainstatements
3) Statementscontainexpressions
4) Expressionscreateandprocessobjects
Introduction
WhatisPython?
Pythonsyntax
Runningscripts
Writingscripts
Preview
30
Demonstration2:
Writingabasicscript
Introduction
WhatisPython?
Pythonsyntax
Runningscripts
Writingscripts
Preview
31
Exercise2:
Writingabasicscript
Introduction
WhatisPython?
Pythonsyntax
Runningscripts
Writingscripts
Preview
32
RunningaPythonscriptinArcGIS
Introduction
WhatisPython?
Pythonsyntax
Runningscripts
Writingscripts
Preview
33
TheArcPysitepackage:
1) ProvidesPythonaccesstoallgeoprocessing
toolsandextensionsinArcGIS
a. AllgeoprocessingtoolsinArcMapareprovided
asfunctionsinArcPy
b. ArcPyalsoincludesseveralfunctionsnot
availableastoolsinArcMap
2) ArcPyhasseveralsubmoduleswithrelated
setsoffunctions(e.g.,spatialanalyst,mapping)
Introduction
WhatisPython?
Pythonsyntax
Runningscripts
Writingscripts
Preview
34
Accessing theArcPysitepackage
Introduction
WhatisPython?
Pythonsyntax
Runningscripts
Writingscripts
Preview
35
APythonscriptfor
ArcGISshould:
Introduction
WhatisPython?
Pythonsyntax
Runningscripts
Writingscripts
Preview
36
APythonscriptfor
ArcGISshould:
1) Includeaheader
Introduction
WhatisPython?
Pythonsyntax
Runningscripts
Writingscripts
Preview
37
APythonscriptfor
ArcGISshould:
1) Includeaheader
2) Importmodules
Introduction
WhatisPython?
Pythonsyntax
Runningscripts
Writingscripts
Preview
38
APythonscriptfor
ArcGISshould:
1) Includeaheader
2) Importmodules
3) Specifyenvironment
settings
Introduction
WhatisPython?
Pythonsyntax
Runningscripts
Writingscripts
Preview
39
APythonscriptfor
ArcGISshould:
1) Includeaheader
2) Importmodules
3) Specifyenvironment
settings
4) Definevariables
Introduction
WhatisPython?
Pythonsyntax
Runningscripts
Writingscripts
Preview
40
APythonscriptfor
ArcGISshould:
1) Includeaheader
2) Importmodules
3) Specifyenvironment
settings
4) Definevariables
5) Rungeoprocessing
tools
Introduction
WhatisPython?
Pythonsyntax
Runningscripts
Writingscripts
Preview
41
3waystorunaPythonscriptforArcGIS:
1) InaPythoneditor(e.g.,PythonWin)
2) InthePythonwindowinArcMap
3) AsascripttoolinArcToolbox
Introduction
WhatisPython?
Pythonsyntax
Runningscripts
Writingscripts
Preview
42
Demonstration3:
Threewaystorunascript
Introduction
WhatisPython?
Pythonsyntax
Runningscripts
Writingscripts
Preview
43
Codingaworkspacefilepath:
1) Environmentsettings(arcpy.env.workspace)
2) Hardcode(C:/workspace/exercise/filename.shp)
3) Createaworkspacevariable
(workspace+os.sep +filename.shp)
Introduction
WhatisPython?
Pythonsyntax
Runningscripts
Writingscripts
Preview
44
Exercise3:
Threewaystorunascript
Introduction
WhatisPython?
Pythonsyntax
Runningscripts
Writingscripts
Preview
45
WritingaPythonscriptforArcGIS
Introduction
WhatisPython?
Pythonsyntax
Runningscripts
Writingscripts
Preview
46
3waystowriteaPythonscriptforArcGIS:
1) Editanexistingscript
2) ExportascriptfromModelBuilder
3) BuildascriptinthePythonwindow
Introduction
WhatisPython?
Pythonsyntax
Runningscripts
Writingscripts
Preview
47
Generaltips:
1) Pythoniscasesensitive
2) Pythonissensitivetoindentation
3) Filepaths usesingleforwardslash(/),doublebackslash(\\),
orrawstringsupression (rfilepath)
4) Mayneedtohardcodefilepaths (workspace+os.sep +
filename)
5) Savescriptswiththe.py fileextension
6) Commentoutsection:Alt+3(PythonWin andIDLE)
7) Uncommentsection:Alt+4(PythonWin andIDLE)
8) Avoidschemalock:removedatasetsfromArcMap
Introduction
WhatisPython?
Pythonsyntax
Runningscripts
Writingscripts
Preview
48
Demonstration4:
Threewaystowriteascript
Introduction
WhatisPython?
Pythonsyntax
Runningscripts
Writingscripts
Preview
49
Exercise4:
Threewaystowriteascript
Introduction
WhatisPython?
Pythonsyntax
Runningscripts
Writingscripts
Preview
50
Preview
Introduction
WhatisPython?
Pythonsyntax
Runningscripts
Writingscripts
Preview
51
Workshopoutline:
Session1
Introduction
Session2
1) Introduction
7) Review
2) WhatisPython?
8) Automatingworkflows
3) Pythonsyntax
9) Describeobject
4) Runningscripts
10) Cursorobject
5) Writingscripts
11) Troubleshooting
6) Preview
12) Othertopics
WhatisPython?
Pythonsyntax
Runningscripts
Writingscripts
Preview
52
Challenge:
UsePython!
Introduction
WhatisPython?
Pythonsyntax
Runningscripts
Writingscripts
Preview
53