ArcGIS Desktop

  • ArcGIS Pro
  • ArcMap

  • My Profile
  • 帮助
  • Sign Out
ArcGIS Desktop

ArcGIS Online

专为贵组织打造的制图平台

ArcGIS Desktop

全面的专业性 GIS

ArcGIS Enterprise

面向企业的 GIS

ArcGIS Developers

用于构建位置感知应用程序的工具

ArcGIS Solutions

适用于行业的免费模板地图和应用程序

ArcGIS Marketplace

获取适用于组织的应用程序和数据

  • 文档
  • 支持
Esri
  • 登录
user
  • 我的个人资料
  • 登出

ArcMap

  • 主页
  • 入门
  • 地图
  • 分析
  • 管理数据
  • 工具
  • 扩展模块

使用 If-Then-Else 逻辑进行分支

  • 在 ModelBuider 中使用 if-then-else 逻辑
  • if-then-else 逻辑示例

If-then-else 逻辑看似简单,功能却十分强大,它可以根据不同条件执行不同的操作。If-then-else 逻辑可理解为:如果某些条件为真,则执行操作;如果条件为假,则执行其他操作。

在 ModelBuider 中使用 if-then-else 逻辑

在 ModelBuider 中,可通过编写脚本工具的方式来实现 if-then-else 逻辑,该工具针对特定条件进行检验并随后输出两个用于描述 true 和 false 条件的布尔变量,然后将此脚本工具加入到模型中。除了编写脚本工具这种方式外,您也可以使用计算值工具对条件进行检验并输出一个布尔值。

下面的模型加入了一个称为 Check Coordinate System 的脚本工具来使用分支逻辑。此脚本工具将对输入数据集进行评估,并指明数据集使用的是已投影的美国国家平面坐标系还是未知坐标系。在模型中,如果发现输入数据集使用的是已投影的美国国家平面坐标系,将不执行任何操作。但如果发现输入数据集使用的是未知坐标系,模型将会定义一个投影系统并对输入数据进行投影。在 ModelBuider 中使用分支逻辑的关键步骤之一就是将其中某个条件输出设置为进行进一步处理的前提条件。

If-Then-Else 示例

if-then-else 逻辑示例

以下示例代码显示了如何在上面提到的 Check Coordinate System 脚本工具中实现 if-then-else 分支。脚本将输出两个变量,一个表示 if (true) 条件,另一个表示 else (false) 条件。

检查坐标系示例

此示例将检查输入数据使用的是美国国家平面坐标系、未使用投影坐标系还是使用美国国家平面坐标系以外的其他坐标系。

# Import modules
import arcpy
import sys
import traceback
# Set local variables
prj = "" 
indata = "C:/ToolData/well.shp" 
dsc = arcpy.Describe(indata) 
sr = dsc.spatialReference 
prj = sr.name.lower()
try:
 
   # check if indata is in StatePlane, has no PRJ, or one other than StatePlane
   if prj.find("_stateplane_") > -1:
       # Set the Is Unknown parameter to FALSE, and the Is StatePlane parameter to TRUE
       arcpy.SetParameterAsText(1,"false") #The first parameter refers to the "Is Unknown" variable
       arcpy.SetParameterAsText(2,"true") #The second parameter refers to the "Is StatePlane" variable
       arcpy.AddMessage("Coordinate system is StatePlane") 
   elif prj == "unknown": 
       # Set the Is Unknown parameter to TRUE, and the Is StatePlane parameter to FALSE
       arcpy.SetParameterAsText(1,"true") 
       arcpy.SetParameterAsText(2,"false") 
       arcpy.AddMessage("To continue, first define a coordinate system!") 
   else:
       # Set the Is Unknown parameter to FALSE, and the Is StatePlane parameter to FALSE
       arcpy.SetParameterAsText(1,"false") 
       arcpy.SetParameterAsText(2,"false") 
       arcpy.AddMessage("Coordinate system is not StatePlane or Unknown") 
except Exception as e:
    AddPrintMessage(e[0], 2)

ArcGIS Desktop

  • 主页
  • 文档
  • 支持

ArcGIS

  • ArcGIS Online
  • ArcGIS Desktop
  • ArcGIS Enterprise
  • ArcGIS
  • ArcGIS Developer
  • ArcGIS Solutions
  • ArcGIS Marketplace

关于 Esri

  • 关于我们
  • 招贤纳士
  • Esri 博客
  • 用户大会
  • 开发者峰会
Esri
分享您的想法。
Copyright © 2021 Esri. | 隐私政策 | 法律声明