python script in modelbuilder get field value return boolean expressions

3378
2
04-26-2013 05:59 AM
NicolasMühlich
New Contributor
I have problems integrating a python script in a modelbuilder tool. I'm a absolute beginner with python, any help is much appreciated. Maybe there's also an easier way to solve this problem in modelbuilder, but I haven't found it yet

so here's my problem:

my modelbuilder tool's flow of control needs to evaluate a situation and decide on one of two courses of action: If the value in the field "Building" in my feature class is 1, it should follow one path, if the value is greater than one it should follow another path.

Here's my python code that I tried so far

# Load the arcpy module
import sys, os, arcpy

# Get the input feature class from the model
InputFC = arcpy.GetParameterAsText(0)
field = "Building"
cursor = arcpy.SearchCursor(InputFC)

# check if one or more than one building footprint exist
for row in cursor:
    Building =(row.getValue(field))
    if Building == 1:
        arcpy.SetParameterAsText(1, "True")
        arcpy.SetParameterAsText(1, "False")
    else:
        arcpy.SetParameterAsText(1, "False")
        arcpy.SetParameterAsText(2, "True")


regards
nicolas
Tags (2)
0 Kudos
2 Replies
CharlesShore
New Contributor
Try this:

with arcpy.da.SearchCursor(InputFC, field) as cursor:
     for row in cursor:
          if (row[0] == 1):


I have problems integrating a python script in a modelbuilder tool. I'm a absolute beginner with python, any help is much appreciated. Maybe there's also an easier way to solve this problem in modelbuilder, but I haven't found it yet

so here's my problem:

my modelbuilder tool's flow of control needs to evaluate a situation and decide on one of two courses of action: If the value in the field "Building" in my feature class is 1, it should follow one path, if the value is greater than one it should follow another path.

Here's my python code that I tried so far

# Load the arcpy module
import sys, os, arcpy

# Get the input feature class from the model
InputFC = arcpy.GetParameterAsText(0)
field = "Building"
cursor = arcpy.SearchCursor(InputFC)

# check if one or more than one building footprint exist
for row in cursor:
    Building =(row.getValue(field))
    if Building == 1:
        arcpy.SetParameterAsText(1, "True")
        arcpy.SetParameterAsText(1, "False")
    else:
        arcpy.SetParameterAsText(1, "False")
        arcpy.SetParameterAsText(2, "True")


regards
nicolas
0 Kudos
curtvprice
MVP Esteemed Contributor

Your script as it stands is only affected by the last row.

Here's a suggest approach: set the default (which remains if building never = 1) and only change it if you find one:

val1, val2 = False, True
for row in cursor:
    Building = row.getValue(field))
    if Building == 1:
        val1, val2 = True, False
        break
arcpy.SetParameterAsText(1, val1)
arcpy.SetParameterAsText(2, val2)

if there are lot of records, arcpy.da.SearchCursor (10.1 and later) maybe worth using - it may be 10-100x faster:

val1, val2 = False, True
with arcpy.da.SearchCursor(InputFC, "Building") as rows:
    for row in rows:
        if row[0] == 1:
            val1, val2 = True, False
            break
0 Kudos