zoukankan      html  css  js  c++  java
  • 使用 If-Then-Else 逻辑进行分支

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

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

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

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

    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)
    

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

  • 相关阅读:
    文件的基本操作
    ps工作界面
    HDU 6300
    HDU 6298
    HDU 2037
    HDU 2036
    Tesseract OCR
    What is the difference between position: static,relative,absolute,fixed
    How to Call a synchronize function asynchronizly in C#
    WCF note1
  • 原文地址:https://www.cnblogs.com/gisoracle/p/11483160.html
Copyright © 2011-2022 走看看