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

在模型构建器中使用 if-then-else 逻辑

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

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

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)

请参阅下列在模型中使用 if-then-else 逻辑的博客中的示例:

最新文章

  1. 磁盘IO
  2. mybatis 使用记录(二) 动态拼接查询条件
  3. 关于Task的线程窃取
  4. Spring Data JPA进阶——Specifications和Querydsl
  5. ubuntu用终端卸载软件
  6. Linux中的模式转换
  7. 微信开发系列——微信订阅号前端开发利器:WeUI
  8. MQ队列管理器搭建(一)
  9. nsq理解
  10. 介绍一款自动给添加不同浏览器CSS3前缀的插件~Autoprefixer(附其他前端开发插件)
  11. 对于Dubbo的理解
  12. sap快捷登录
  13. ubuntu google chrome 全屏显示命令
  14. Vue+Webpack构建移动端京东金融(一、开发前准备)
  15. MapGIS DataStore
  16. K8S集群 NOT READY的解决办法 1.13 错误信息:cni config uninitialized
  17. PHPWord插件详解
  18. HDU 1050(搬椅子 数学)
  19. Linux 第五天
  20. 处于ESTABLISHED 状态的socket 却没有进程信息

热门文章

  1. Linux开机自动启动服务
  2. mycat使用--schema配置
  3. Linux/Aix日常报错整理
  4. c# 接口使用
  5. 《Python编程:从入门到实践》第三章 列表简介 习题答案
  6. Educational Codeforces Round 41 (Rated for Div. 2) D. Pair Of Lines (几何,随机)
  7. 字符编码,python解释器------总结
  8. maven-pom文件的scope作用域
  9. YARN构建--解决cypress下载慢问题
  10. 2.设计模式-Abstract Factory 抽象工厂模式