200 likes | 465 Views
Introduction to ArcPy. Topics. What is ArcPy ? Accessing geoprocessing tools using ArcPy Writing scripts using ArcPy. What is ArcPy ?. ArcPy was introduced with ArcGIS10 It’s Python site package With ArcPy , you’ve access to the Geoprocessing tools Extensions
E N D
Topics • What is ArcPy? • Accessing geoprocessing tools using ArcPy • Writing scripts using ArcPy
What is ArcPy? • ArcPy was introduced with ArcGIS10 • It’s Python site package • With ArcPy, you’ve access to the • Geoprocessing tools • Extensions • Functions & classes
ArcPy Functions • Functions perform a specific task as a part of larger program • All geoprocessing tools are functions -- Union, Buffer, Clip etc. • But not all functions are geoprocessing tools -- List datasets, describe dataset, create cursor
ArcPy Classes • Classes provide a framework for creating object - referred to as an instance • Examples of classes - SpatialReference, Extent etc. • Classes have methods & properties --Spatialreference.name -Spatialreference.type
ArcPy Modules • Modules are Python libraries containing functions and classes • 3 modules --Mapping modules: arcpy.mapping -Spatial Analyst module: arcpy.sa - Geostatistical module: arcpy.ga • Example: --arcpy.sa.Slope is same as using the Slope tool from Spatial Analyst toolbox
How to use ArcPy • You have to import the ArcPy package before using it • Simply type “import arcpy” in Python Window in ArcGIS
How to use a tool? Two ways to call a tool Example: arcpy.Buffer_analysis() Example: arcpy.analysis.Buffer()
Tool Syntax Each tool needs specific parameters to run correctly Buffer_analysis (in_features, out_feature_class, buffer_distance_or_field, {line_side}, {line_end_type}, {dissolve_option}, {dissolve_field}) Buffer tool needs 3 required parameters: Input features Out feature class Buffer distance There are a number of optional parameters too!!
Environment Setting Set your workspace pathname for reading input & writing output file: arcpy.env.workspace = “C:\\GIS222” # Read and write files from GIS222 folder arcpy.env.workspace = “C:\\Test.gdb” # Read and write files from Test.gdb arcpy.env.overwriteOutput = 1 # Will overwrite the output file
Let’s try it! Run buffer in Python Window Write a script to run buffer in PythinWin
Topics • How to get a list of data layers? • Iterate through the list
Getting a list Getting the list of available data is often the first step of many tasks ArcPy provides functions for getting • Lists of fields • Lists of datasets • List of feature classes • List of Files • List of Tables • And more
Important Note YOU MUST set the workspace before calling any list function import arcpy arcpy.env.workspace = "C:\\temp" fcs = arcpy.ListFeatureClasses()
Iterating through the list Python ‘for’ loop can be used to loop through your list import arcpy arcpy.env.workspace= “C:\\Auburn.gdb” for fc in arcpy.ListFeatureClasses(): print fc
Other List Methods • ListDatasets • Lists all datasets from a workspace • Feature, TIN, Raster, or CAD • ListRasters • Lists all rasters in a workspace • PNG, TIFF, JPG, BMP, GIF, IMG, GRID, others • ListFields • Lists the fields in a feature class, shapefile, or table in a specified dataset • ListTables • Lists all tables in the workspace • ListWorkspaces • Lists workspaces within a workspace • Folders, file geodatabases, personal geodatabases