Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 10 Accessing tools and environment setting in Scripts

Similar presentations


Presentation on theme: "Lecture 10 Accessing tools and environment setting in Scripts"— Presentation transcript:

1 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() 2018/11/16 Jun Liang, UNC

2 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" 2018/11/16 Jun Liang, UNC

3 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 2018/11/16 Jun Liang, UNC

4 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(…) 2018/11/16 Jun Liang, UNC

5 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) 2018/11/16 Jun Liang, UNC

6 Jun Liang, Geography @ UNC
Accessing Toolboxes For non-system toolboxes: gp.AddToolBox(“d:\Arctools\MarketTools.tbx”) gp.MarketDelineation() gp.RemoveToolbox(“d:\Arctools\MarketTools.tbx”) 2018/11/16 Jun Liang, UNC

7 Accessing Toolboxes (Cont.)
2018/11/16 Jun Liang, UNC

8 Accessing Toolboxes (Cont.)
To find help for a geoprocessor method/property: - Writing geoprocessing scripts->Scripting Object->IGPDispatch->A particular Method 2018/11/16 Jun Liang, UNC

9 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. 2018/11/16 Jun Liang, UNC

10 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. 2018/11/16 Jun Liang, UNC

11 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. 2018/11/16 Jun Liang, UNC

12 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. 2018/11/16 Jun Liang, UNC

13 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 …… 2018/11/16 Jun Liang, UNC

14 Jun Liang, Geography @ UNC
2018/11/16 Jun Liang, UNC

15 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: 2018/11/16 Jun Liang, UNC

16 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”: 2018/11/16 Jun Liang, UNC


Download ppt "Lecture 10 Accessing tools and environment setting in Scripts"

Similar presentations


Ads by Google