使用python指令碼執行地理處理工具
阿新 • • 發佈:2019-12-02
桌面ArcGIS包含800多種可在Python指令碼中執行的地理處理工具。
通過Python指令碼來執行地理處理工具,可以處理複雜的工作和執行批處理任務。
案例一:使用指令碼執行地理處理工具(以裁剪為例)
檢視幫助文件模仿的程式碼:
1 # Name: Clip_Example2.py 2 3 # Description: Clip major roads that fall within the study area. 4 5 6 # Import system modules 7 8 import arcpy 9 10 from arcpy import env 11 12 13 # Set workspace 14 15 env.workspace = "F:\【the_path_of_grace】\ArcpyBook" 16 17 18 # Set local variables 19 20 in_features = "data/CityOfSanAntonio.gdb/Burglary" 21 22 clip_features = "Ch5/EdgewoodSD.shp" 23 24 out_feature_class = "Ch5/ClpBurglary.shp" 25 26 xy_tolerance = "" 27 28 29 # Execute Clip 30 31 arcpy.Clip_analysis(in_features, clip_features, out_feature_class, xy_tolerance)
工作原理:
在Python指令碼中,可以參照<toolname>_<toolboxalias>語法來呼叫工具。
注:toolboxalias是工具箱的別名
拓展:
效果圖:
案例二:將一個工具的輸出作為另一個工具的輸入
步驟:①緩衝區分析->②建立要素圖層->③按位置選擇圖層
檢視幫助文件模仿的程式碼:
1 # Import system modules 2 import arcpy 3 from arcpy import env 4 5 # Set workspace 6 env.workspace = "F:\【the_path_of_grace】\ArcpyBook\data\TravisCounty" 7 try: 8 # Buffer areas of impact around major roads 9 streams = "Streams.shp" 10 streamsBuffer = "StreamsBuffer" 11 distanceField = "2640 Feet" 12 sideType = "FULL" 13 endType = "ROUND" 14 dissolveType = "ALL" 15 schools2mile = "Schools.shp" 16 schoolsLyrFile = 'Schools2Mile_lyr' 17 18 arcpy.Buffer_analysis(streams, streamsBuffer, distanceField,sideType,endType,dissolveType) 19 20 # First, make a layer from the feature class 21 arcpy.MakeFeatureLayer_management(schools2mile, schoolsLyrFile) 22 23 # Then add a selection to the layer based on location to features in another feature class 24 arcpy.SelectLayerByLocation_management (schoolsLyrFile, "intersect", streamsBuffer) 25 except Exception as e: 26 print e.message
執行過程:
效果圖:
謝謝觀看!
&n