首页 资讯 社群 我的社区 搜索

使用python脚本执行地理处理工具

yimo~
2019-12-02 14:19:20

桌面ArcGIS包含800多种可在Python脚本中运行的地理处理工具。

通过Python脚本来运行地理处理工具,可以处理复杂的工作和执行批处理任务。

案例一:使用脚本执行地理处理工具(以裁剪为例)

查看帮助文档模仿的代码:

# Name: Clip_Example2.py

# Description: Clip major roads that fall within the study area.


# Import system modules

import arcpy

from arcpy import env


# Set workspace

env.workspace = "F:【the_path_of_grace】ArcpyBook"

 
# Set local variables

in_features = "data/CityOfSanAntonio.gdb/Burglary"

clip_features = "Ch5/EdgewoodSD.shp"

out_feature_class = "Ch5/ClpBurglary.shp"

xy_tolerance = ""


# Execute Clip

arcpy.Clip_analysis(in_features, clip_features, out_feature_class, xy_tolerance)

工作原理:

在Python脚本中,可以参照<toolname>_<toolboxalias>语法来调用工具。

注:toolboxalias是工具箱的别名

拓展:

效果图:

案例二:将一个工具的输出作为另一个工具的输入

步骤:①缓冲区分析->②创建要素图层->③按位置选择图层

查看帮助文档模仿的代码:

# Import system modules
import arcpy
from arcpy import env

# Set workspace
env.workspace = "F:【the_path_of_grace】ArcpyBookdataTravisCounty"
try:
# Buffer areas of impact around major roads
    streams = "Streams.shp"
    streamsBuffer = "StreamsBuffer"
    distanceField = "2640 Feet"
    sideType = "FULL"
    endType = "ROUND"
    dissolveType = "ALL"
    schools2mile = "Schools.shp"
    schoolsLyrFile = 'Schools2Mile_lyr'
    
    arcpy.Buffer_analysis(streams, streamsBuffer, distanceField,sideType,endType,dissolveType)

# First, make a layer from the feature class
    arcpy.MakeFeatureLayer_management(schools2mile, schoolsLyrFile)

# Then add a selection to the layer based on location to features in another feature class 
    arcpy.SelectLayerByLocation_management (schoolsLyrFile, "intersect", streamsBuffer)
except Exception as e:
    print e.message

 执行过程:

效果图:

 

用户评论