IntrotoPython Session1 Slides

  • View
    214

  • Download
    0

Embed Size (px)

DESCRIPTION

Pyton

Transcript

  • Session1: November8,2013

    IntroductiontoPythonforArcGIS10.1AhandsonworkshopofferedbytheGeospatialCentroidandCSULibraries

    SarahE.Reed,PhD

    AssociateConservationScientistWildlifeConservationSociety

    FacultyAffiliateDepartmentofFish,Wildlife,andConservationBiology

  • Introduction WhatisPython? Pythonsyntax Runningscripts Writingscripts Preview 2

    Introduction

  • Introductions:

    1) Nameandaffiliation

    2) ExperienceusingPythonorotherprogramminglanguages

    3) HowdoyouplantousePythoninyourownGISresearchorwork?

    Introduction WhatisPython? Pythonsyntax Runningscripts Writingscripts Preview 3

  • Goalsofworkshop:

    1) IntroducethePythonscriptinglanguageanditsapplicationinArcGIS10.1;

    2) Becomefamiliarwithseveralmethodsforwriting,editing,andrunninggeoprocessingscriptsusingPython;and

    3) ApplyPythonscriptstoautomateaGISworkflow,determinethepropertiesofaspatialdataset,andsearchandupdateattributeinformationinArcGIS.

    Introduction WhatisPython? Pythonsyntax Runningscripts Writingscripts Preview 4

  • Workshopoutline:

    Session1 Session2

    1) Introduction

    2) WhatisPython?

    3) Pythonsyntax

    4) Runningscripts

    5) Writingscripts

    6) Preview

    7) Review

    8) Automatingworkflows

    9) Describeobject

    10) Cursorobject

    11) Troubleshooting

    12) Othertopics

    Introduction WhatisPython? Pythonsyntax Runningscripts Writingscripts Preview 5

  • Demonstration1:

    SPreADGIS(orwhyIlearnedPython)

    Introduction WhatisPython? Pythonsyntax Runningscripts Writingscripts Preview 6

  • Introduction WhatisPython? Pythonsyntax Runningscripts Writingscripts Preview 7

  • Introduction WhatisPython? Pythonsyntax Runningscripts Writingscripts Preview 8

  • Introduction WhatisPython? Pythonsyntax Runningscripts Writingscripts Preview 9

  • 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

  • cumulativenoisefrommultiplesources

    Introduction WhatisPython? Pythonsyntax Runningscripts Writingscripts Preview 13

  • acousticeffectsforspecieswithvariableauditorysensitivity

    Introduction WhatisPython? Pythonsyntax Runningscripts Writingscripts Preview 14

  • Introduction WhatisPython? Pythonsyntax Runningscripts Writingscripts Preview 15

    WhatisPython?

  • 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

    Adatastructurethatcombinesdatawithasetofmethodsforaccessingandmanagingthosedata

    Introduction WhatisPython? Pythonsyntax Runningscripts Writingscripts Preview 17

  • Pythoneditors:

    IntegratedDevelopmentEnvironment(IDE):Asoftwareapplicationforprogrammingandsoftwaredevelopment

    Sourcecodeeditor:Atexteditorforsoftwarecode,withfeaturesspeciallydesignedtosimplifyandspeedupwritingandeditingofcode

    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) Websiteswww.python.org

    forums.arcgis.com

    3) ArcPysitepackage(online)

    Introduction WhatisPython? Pythonsyntax Runningscripts Writingscripts Preview 20

  • Userresources:

    4) WebcoursesPythonforEveryone (free)

    BasicsofPython(forArcGIS10) ($32)

    UsingPythoninArcGISDesktop10 (free)

    PythonScriptingforGeoprocessingWorkflows ($32)

    CreatingPythonToolboxesUsingArcGIS10.1 (free)

    PythonScriptingforMapAutomationinArcGIS10($32)

    5)InstructorledcoursesIntroductiontoGeoprocessingScriptsUsingPython($1515)

    Introduction WhatisPython? Pythonsyntax Runningscripts Writingscripts Preview 21

  • Exercise1:

    Downloadworkshopdataandverifysoftwareinstallation

    Introduction WhatisPython? Pythonsyntax Runningscripts Writingscripts Preview 22

  • Introduction WhatisPython? Pythonsyntax Runningscripts Writingscripts Preview 23

    Pythonstructureandsyntax

  • Pythonstructure:

    1) Programsarecomposedofmodules

    2) Modulescontainstatements

    3) Statementscontainexpressions

    4) Expressionscreateandprocessobjects

    Introduction WhatisPython? Pythonsyntax Runningscripts Writingscripts Preview 24

  • Object:Apieceofmemory,withvaluesandassociatedoperations;alsoknownasvariables

    Typesofobjects: Numbers

    Strings

    Lists

    Files

    Introduction WhatisPython? Pythonsyntax Runningscripts Writingscripts Preview 25

  • Expression:Processesanobject:x*2

    Statement:Performsatask,viaanexpression:y=x*2

    Typesofstatements: Assignment: x=5

    Call: open(DataFile)

    import

    print

    if/elif/else

    for,while

    Introduction WhatisPython? Pythonsyntax Runningscripts Writingscripts Preview 26

  • Module:Alibraryoftools;permanentfileofcode,composedofstatements

    Typesofmodules: Standardlibrarymodules:os,sys,string(moduleindex)

    Specializedmodulesorsitepackages:arcpy (sitepackage)

    a.py

    b.py

    c.py

    Standardlibrarymodules

    Yourscript Modules

    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

  • Introduction WhatisPython? Pythonsyntax Runningscripts Writingscripts Preview 33

    RunningaPythonscriptinArcGIS

  • TheArcPysitepackage:

    1) ProvidesPythonaccesstoallgeoprocessingtoolsandextensionsinArcGIS

    a. AllgeoprocessingtoolsinArcMapareprovidedasfunctionsinArcPy

    b. ArcPyalsoincludesseveralfunctionsnotavailableastoolsinArcMap

    2) ArcPyhasseveralsubmoduleswithrelatedsetsoffunctions(e.g.,spatialanalyst,mapping)

    Introduction WhatisPython? Pythonsyntax Runningscripts Writingscripts Preview 34

  • Accessing theArcPysitepackage

    Introduction WhatisPython? Pythonsyntax Runningscripts Writingscripts Preview 35

  • APythonscriptforArcGISshould:

    Introduction WhatisPython? Pythonsyntax Runningscripts Writingscripts Preview 36

  • APythonscriptforArcGISshould:

    1) Includeaheader

    Introduction WhatisPython? Pythonsyntax Runningscripts Writingscripts Preview 37

  • APythonscriptforArcGISshould:

    1) Includeaheader

    2) Importmodules

    Introduction WhatisPython? Pythonsyntax Runningscripts Writingscripts Preview 38

  • APythonscriptforArcGISshould:

    1) Includeaheader

    2) Importmodules

    3) Specifyenvironmentsettings

    Introduction WhatisPython? Pythonsyntax Runningscripts Writingscripts Preview 39

  • APythonscriptforArcGISshould:

    1) Includeaheader

    2) Importmodules

    3) Specifyenvironmentsettings

    4) Definevariables

    Introduction WhatisPython? Pythonsyntax Runningscripts Writingscripts Preview 40

  • APythonscriptforArcGISshould:

    1) Includeaheader

    2) Importmodules

    3) Specifyenvironmentsettings

    4) Definevariables

    5) Rungeoprocessingtools

    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