160 likes | 332 Views
Lecture 10 Accessing tools and environment setting in Scripts. Accessing the Geoprocessor from Python Geoprocessor can be used in many languages: Perl, VBScript, Jscript, Python, VBA, VB, C#, and so on – any COM compliant language. Recall how to use python to access geoprocessor:
E N D
Lecture 10 Accessing tools and environment setting in Scripts Accessing the Geoprocessor from Python • Geoprocessor can be used in many languages: Perl, VBScript, Jscript, Python, VBA, VB, C#, and so on – any COM compliant language. • Recall how to use python to access geoprocessor: import arcgisscripting gp = arcgisscripting.create() Jun Liang, Geography @ UNC
Previous win32.com compatibility test Following code is from Tim. It can be used to test if win32com object exist or not, then initialize gp accordingly. Alternatively, win32com extension can be found and installed from python.org. With this extension, both win32com scripts and arcgisscripting scripts should work. try: #Using ArcGIS v9.2 import arcgisscripting gp = arcgisscripting.create() print "Using arcgisscripting module" except ImportError: #Using older version that 9.2 import win32com.client gp = win32com.client.Dispatch("esriGeoprocessing.GpDispatch.1") print "Using win32com.client module" Jun Liang, Geography @ UNC
Syntax for properties and Methods • To assign a value to a property: Object.Property = Value gp.workspace = “C:\” • To get the value of a property Object.Property • To use a method Object.Method(arg1, arg2,…) Note: Please use ArcGIS Desktop Help to find syntax for methods – Contents->Geoprocessing->Geoprocessing tool reference Jun Liang, Geography @ UNC
Toolbox aliases • Since different toolset may contain similar methods with same name, you will need to use suffix to label each method, or define a toolset before you call that method. gp.Buffer_analysis() gp.Select_analysis(…) gp.Clip_analysis(…) Jun Liang, Geography @ UNC
More tool examples • Clip_management(in_rast, recangle, out_raster) • Union_analysis(in_features, out_features, join_attributes, cluster_tolerance, gaps) • Select_analysis(in_features,out_features,whereclause) Jun Liang, Geography @ UNC
Accessing Toolboxes For non-system toolboxes: gp.AddToolBox(“d:\Arctools\MarketTools.tbx”) gp.MarketDelineation() gp.RemoveToolbox(“d:\Arctools\MarketTools.tbx”) Jun Liang, Geography @ UNC
Accessing Toolboxes (Cont.) Jun Liang, Geography @ UNC
Accessing Toolboxes (Cont.) To find help for a geoprocessor method/property: - Writing geoprocessing scripts->Scripting Object->IGPDispatch->A particular Method Jun Liang, Geography @ UNC
Accessing Toolboxes (Cont.) Exercises: • Find the usage of “gp.Exists” and use it to test if a dataset is available in your storage space. • Check the usage of “addxy” under Data Management Toolbox and apply it to point feature class. Jun Liang, Geography @ UNC
Geoprocessor Programming Model From Geoprocessor you can get other objects – similar to pxDocument, from which you can get map, layer, etc. Each box represents an object. Jun Liang, Geography @ UNC
The Describe Object To get help for describe object, you can find it in two places – • geoprocessor.pdf • ArcGIS online help The describe method for gp (geoprocessor object) will return an object, based on the input value. And data type of the output object can be used for script flow control. Jun Liang, Geography @ UNC
The Describe Object (Cont.) From ArcGIS Online Help, We can get: Describe Method Similar to the ArcINFO Describe command, it details such properties as type, name, spatial reference, domains, versioning, etc., for DataElements such as FeatureClasses, Tables, GeoDatasets, CoverageFeatureClasses, Datasets, RelationshipClasses, Files, Layers, Workspaces, as well as GP objects such as TableViews, and FeatureLayers. Syntax object.Describe(inputValue) as Object Part Description Object The instantiated geoprocessing object. inputValue The name of the DataElement, or GPObject, to be described. If the workspace is not set you must include the path as well as the name. Jun Liang, Geography @ UNC
The Describe Object (Cont.) Objects FeatureClass, Coverage FeatureClass,Layer,Table , Dataset ,TableView , Workspace, Coverage, Relationship Class ,Raster Catalog , Raster Dataset Raster Band Examples import arcgisscripting gp = arcgisscripting.create() gp.workspace = “s:/data/python/mdb/Tongass.mdb" fc = “stands” dsc = gp.describe(fc) print "Describing:", fc print "FeatureType:" print dsc.FeatureType print "TopologyName:" print dsc.TopologyName …… Jun Liang, Geography @ UNC
The Describe Object (Cont.) All describe objects can use the Describe Object Properties. It can be used to check its datatype and then perform datatype related process: if dscfc1.datatype == “FeatureClass”” … elif dscfc1.datatype == “”Table” … else: … Jun Liang, Geography @ UNC
The Describe Object (Cont.) Dataset properties Import arcgisscripting gp = arcgisscripting.create() gp.workspace = "k:/data/python/mdb/Tongass.mdb" fc1 = “stands” Dscfc1 = gp.Describe(fc1) Print Dscfc1.DatasetType fc2 = “NestsF” Dscfc2 = gp.Describe(fc2) Print Dscf2.DatasetType If dscfc2.DatasetType == “FeatureDataset”: Jun Liang, Geography @ UNC