Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 9 Using Python for Geoprocessing

Similar presentations


Presentation on theme: "Lecture 9 Using Python for Geoprocessing"— Presentation transcript:

1 Lecture 9 Using Python for Geoprocessing
ESRI chose Python as the support language for geoprocessing because: • Python is easy to learn because of its clean syntax and simple, clear concepts. • Python supports object-oriented programming in an easy-to-understand manner. • Documenting Python is easier because it has readable code. • Complicated data structures are easy to work with in Python. • Python is simple to integrate with C++ and Fortran. • Python can be seamlessly integrated with Java. • Python is free from the Web and has a widespread community. 2018/11/10 Jun Liang, UNC

2 Jun Liang, Geography @ UNC
Python and ArcGIS 9.2 Pythonwin was installed with Arc8.3, 9.0 and 9.1, but not with ArcGIS 9.2. Only IDLE editor is installed with ArcGIS 9.2 Pythonwin was required to run win32com scripts, so your existing scripts may not work in ArcGIS 9.2 ArcGIS 9.2 is hard wired to Python2.4.1, and Python2.5 or higher may not work with 9.2 and also is not supported by ESRI To take advantage of new geoprocessing scripting functionality, such as the arcgisscripting module and cross platform support, the win32com module cannot be used. For more information about python compatibility, check 2018/11/10 Jun Liang, UNC

3 Scripting Functionalities
2018/11/10 Jun Liang, UNC

4 Scripting Functionalities (Cont.)
Review frequently used data types and control structures in geoprocessing: Import math Import sys, os If…elif…else for…in while fcList=[‘river.shp’, ‘county.shp’, ‘city.shp’, ‘street.shp’] 2018/11/10 Jun Liang, UNC

5 Tools and environment settings
Tools up to 455 • Union, Buffer, Project, Import from CAD, Delete, Geocode Addresses, Hillshade, Create TIN, Feature Outline Masks, Create Routes, Mean Center, Build, and much more... Environment settings: 22 • Workspace, Cell Size, Output Coordinate System, Compression, Tile Size, Output Extent, and much more... Example in pythonwin with win32com.client import win32com .client gp • win32com.c1ient.Dispatch(”esriGeoprocesing.GpDispatch.1”) gp .Workspace = “c :\\Data\\Canada .mdb” gp.Buffer(”Streams”, “NoBuildZone”, 100) gp.Erase(”LandUse”, “NoBuildone”, “BuildZone”) 2018/11/10 Jun Liang, UNC

6 Pythonwin compatibility issue
win32com require PythonWin win32com module is part of PythonWin To run or edit previous pythonwin scripts that use the win32com module, PythonWin needs to be installed. 2018/11/10 Jun Liang, UNC

7 Jun Liang, Geography @ UNC
Code without win32.com import arcgisscripting #Create the Geoprocessor object gp = arcgisscripting.create() gp.overwriteoutput = 1 gp.Workspace = "S:\\data\\GPS\\CNTrail.mdb" gp.Buffer("NLTrail", "trail10", 10) print "done" For gp.overwriteoutput: 2018/11/10 Jun Liang, UNC

8 Geoprocessor Programming Model Diagram
The Geoprocessor Programming Model is not based on the Unified Modeling Language (UML) notation used in the ArcObjects object model diagrams. The arrows on the diagram indicate instantiation. GpDispatch is the COM name for the geoprocessor IDispatch object. Any scripting language that can instantiate a COM IDispatch object may create the geoprocessor. In Python, the Dispatch method on the win32com module’s client object is used to create the geoprocessor, while Visual Basic and VBScript use the CreateObject function. The geoprocessor is the only ArcGIS scripting object that can be directly created using these methods. All other ArcGIS scripting objects are created by a method on the geoprocessor, such as CreateObject, or as a property of an object, such as the fields of a feature class. 2018/11/10 Jun Liang, UNC

9 Geoprocessor Programming Model Diagram
The different colors are meant to show logical relationships of objects. Some properties, such as Field in a feature class, are colored to indicate they are an object, which, in this case, is the fields object that is purple. The GpDispatch object supports the execution of tools as dynamic methods and the use of environment settings as dynamic properties. The diagram does not show these dynamic members of the geoprocessor, as the tools and environments that are accessible depend on what is installed on the desktop machine being used and what toolboxes are being referenced. The geoprocessor object simply refers to tools and environments as generic methods and properties. 2018/11/10 Jun Liang, UNC

10 Accessing the Geoprocessor from Python
A typical start of a python script: #Import standard library modules import arcgisscripting, sys, os gp = arcgisscripting.create() This imports the system, operating system, and arcgisscripting modules to the script. The sys module refers to the Python system and will be used to access user-specified inputs. The os module provides easy access to the most fundamental tools of the operating system. Some of the os module’s filename manipulation tools are used in this script. Gp can be used to access all geoprocessing functionality through this variable. 2018/11/10 Jun Liang, UNC

11 Accessing the Geoprocessor from Python (Cont.)
#Set the input workspace GP.workspace = sys.argv[1] print “Current workspace: ”, GP.workspace #Set the clip featureclass clipFeatures = sys.argv[2] #Set the output workspace outWorkspace = sys.argv[3] #Set the cluster tolerance clusterTolerance = sys.argv[4] try: #Get a list of the featureclasses in the input folder fcs = GP.ListFeatureClasses() 2018/11/10 Jun Liang, UNC

12 Accessing the Geoprocessor from Python (Cont.)
The try statement defines the beginning of a block of code that will be handled by its associated exception handler, or except statement. It is good practice to use exception handling in any script using the geoprocessor so its error messages can be propagated back to the user. This also allows the script to exit gracefully and return informative messages instead of simply causing a system error. The geoprocessor’s ListFeatureClasses method returns an enumeration of feature class names in the current workspace. The workspace defines the location of your data and where all new data will be created unless a full path is specified. The workspace has already been set to the first argument’s value. 2018/11/10 Jun Liang, UNC

13 Accessing the Geoprocessor from Python (Cont.)
Add the following code to get the first value of the enumeration: #Loop through the list of feature classes fcs.Reset() fc = fcs.Next() while fc: #Validate the new feature class name for the output workspace. outFeatureClass = outWorkspace + "/" + GP.ValidateTableName(fc, outWorkspace) #Clip each feature class in the list with the clip feature class. #Do not clip the clipFeatures, it may be in the same workspace. if str(fc) != str(os.path.split(clipFeatures)[1]): GP.Clip(fc, clipFeatures, outFeatureClass, clusterTolerance) except: GP.AddMessage(GP.GetMessages(2)) print GP.GetMessages(2) 2018/11/10 Jun Liang, UNC

14 Jun Liang, Geography @ UNC
Customize ArcTools ArcToolbox includes many tools (more than 400), and you can access them from ArcToolbox – also, you can use them in your python script. Set up workspace, get workspace information from users’ input, or retrieve workspace information from environment parameters. Set inputs for ArcToolbox. 2018/11/10 Jun Liang, UNC

15 Customize ArcTools (Cont.)
Examples of customizing tools: …. # You may also set input parameters from users’ input # Local variables... test_shp = “S:/liangj/test.shp" CITIES_shp = “S:/data/USA/CITIES.shp" orange_shp = “S:/data/orange.shp" # Process: Clip... gp.Clip_analysis(CITIES_shp, orange_shp, test_shp, "") 2018/11/10 Jun Liang, UNC

16 Customize ArcTools (Cont.)
Another example: #Import standard library modules import arcgisscripting, sys, os gp = arcgisscripting.create() gp.Workspace = r"D:\GTKArcGIS\Data\Tongass.mdb" print "current workspace is " + gp.Workspace gp.Workspace = r"D:\GTKArcGIS\shapefiles" bufferFC = sys.argv[1] outFeature = sys.argv[2] gp.Buffer_analysis(bufferFC,outFeature,100) 2018/11/10 Jun Liang, UNC

17 Customize ArcTools (Cont.)
Customize selection tool: ….. # Local variables... COUNTIES_Select_shp = "K:/data/USA/COUNTIES_Select.shp" COUNTIES_shp = "K:/data/USA/COUNTIES.shp" # Process: Select... gp.Select_analysis(COUNTIES_shp, COUNTIES_Select_shp, ""NAME" ="Orange" OR "NAME" ="Durham"") 2018/11/10 Jun Liang, UNC

18 Customize ArcTools (Cont.)
Find out usage for a script: gp.Usage(“Buffer_analysis”) gp.Usage(“Select_analysis”) gp.Usage(“Clip_analysis”) Or, you can use ArcGIS Desktop help. You may also output the corresponding model to a python script and check how parameters are defined. 2018/11/10 Jun Liang, UNC


Download ppt "Lecture 9 Using Python for Geoprocessing"

Similar presentations


Ads by Google