If else statement

6788
63
Jump to solution
08-28-2013 04:39 AM
JamesSmith7
Emerging Contributor
I am modifying a script suggested in a prior thread.  http://forums.arcgis.com/threads/90242-Polygon-Centroid?highlight=jsmith0705

I want to be able to select a line, determine if the ARCLENGTH field has a value greater than zero, return that value if greater than zero, if not return the value from the Shape_Length field.

The script runs with no errors or results. 

import arcpy  mxd = arcpy.mapping.MapDocument ("CURRENT") df = arcpy.mapping.ListDataFrames (mxd)[0]  lyr = arcpy.mapping.ListLayers(mxd, "Lot_Lines", df)[0]  for lyr in arcpy.mapping.ListLayers(mxd):  tlyr = lyr  dsc = arcpy.Describe(tlyr)  sel_set = dsc.FIDSet  if dsc.shapeType == "Line":   if len(sel_set) > 0: #If ARCLENGTH value > 0           arcpy.AddMessage(str(ARCLENGTH)) #Return ARCLENGTH value   else:    arcpy.AddMessage(str(Shape_Length)) #Return Shape_Length value
Tags (2)
0 Kudos
63 Replies
JamesSmith7
Emerging Contributor
Rereading my last post, I realize my statement maybe clear as mud.

I need to add arcLength + arcLength and shapeLength + shapeLength, as two separate expressions.
0 Kudos
RichardFairhurst
MVP Alum
Rereading my last post, I realize my statement maybe clear as mud.

I need to add arcLength + arcLength and shapeLength + shapeLength, as two separate expressions.


Reread my post.  After you posted this I made some edits that should be clearer.  In the case of ShapeLength, did you want it to add even when ArcLength is greater than '0' or just when arcLength is less than or equal to '0'?  I wrote it to do the latter.  If you wanted it to do the former the code within the cursor rotuine would be

   if arcLength > '0':
    arcLengthTot += float(arcLength)
    shapeLengthTot += shapeLength
    arcpy.AddMessage("ArcLength = " + arcLength)
   else:
    shapeLengthTot += shapeLength
    arcpy.AddMessage("ShapeLength = " + str(shapeLength))


Edit:  Fixed another indent issue.
0 Kudos
JamesSmith7
Emerging Contributor
Reread my post.  After you posted this I made some edits that should be clearer.  In the case of ShapeLength, did you want it to add even when ArcLength is greater than '0' or just when arcLength is less than or equal to '0'?  I wrote it to do the latter.  If you wanted it to do the former the code within the cursor rotuine would be

   if arcLength > '0':
    arcLengthTot += float(arcLength)
    shapeLengthTot += shapeLength
    arcpy.AddMessage("ArcLength = " + arcLength)
   else:
    shapeLengthTot += shapeLength
    arcpy.AddMessage("ShapeLength = " + str(shapeLength))


Edit:  Fixed another indent issue.


Only add ArcLength when it is greater than '0'.  Otherwise, the script should return shapeLength values and add those together.
0 Kudos
RichardFairhurst
MVP Alum
Only add ArcLength when it is greater than '0'.  Otherwise, the script should return shapeLength values and add those together.


Then just use the code as written in my post before that.  Here is how to make the message only report the totals if they are above 0.

for lyr in arcpy.mapping.ListLayers(mxd):
 dsc = arcpy.Describe(lyr)
 sel_set = dsc.fidSet.replace(";",",")
 if dsc.shapeType == "Polyline" and dsc.fidSet != "":
  cursor = arcpy.da.SearchCursor(lyr, ["OID@", "ARCLENGTH", "Shape_Length"], "OBJECTID IN (" + sel_set + ")")
  arcLengthTot = 0
  shapeLengthTot = 0
  for row in cursor:
   arcLength = row[1]
   shapeLength = row[2]
   if arcLength > '0':
    arcLengthTot += float(arcLength)
    arcpy.AddMessage("ArcLength = " + arcLength)
   else:
    shapeLengthTot += shapeLength
    arcpy.AddMessage("ShapeLength = " + str(shapeLength))
   if arcLengthTot > 0:
    arcpy.AddMessage("ArcLengthTot = " + str(arcLengthTot))
   if shapeLengthTot > 0:
    arcpy.AddMessage("ShapeLengthTot = " + str(shapeLengthTot))
  del row
  del cursor


Both totals messages could print for a given selection is you selected a mix of the two kinds of arcs/shapes as a group.

You could now optionally comment out the individual reported lengths if you want just the totals or leave both individual segment and totals messages.  For a single feature the messages would print two times and repeat the same number unless some more logic is added about the sel_set count.
0 Kudos