桌面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
执行过程:
效果图:
谢谢观看!