If else statement

4949
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
1 Solution

Accepted Solutions
RichardFairhurst
MVP Honored Contributor
Why does changing the number 0 to a string '0', change the selection from arcLength to shapeLength?  For instance, if len(arcLength) > 0: returns arcLength and if len(arcLength) > '0': returns shapeLength?  The ARCLENGTH field is a string and the Shape_Length field is a double.


I didn't write if len(arcLength) > '0':.  That is a meaningless piece of code and should return an error, since a it is comparing a number with a string.  The alternative you wrote is also wrong.  if len(arcLength) > 0: should return arclength even if it is ' ', which you don't want.  The version James wrote was if len(arcLength) > 1: and it will not return ' ' through '9', which is also wrong.

I wrote if arcLength > '0':, which compares the string with a string and will return arcLength in all cases except where it is equal to ' ' or '0'.  It will return '1' - '9'.  Use the code I wrote and you will get the most correct results.  (It still could fail if you had values like ' 0' or '00', but I considered that to be too unlikely to add more tests to evaluate.)

View solution in original post

0 Kudos
63 Replies
MichaelVolz
Esteemed Contributor
Try adding some print statements to understand what is actually occurring with your script.

print("The describe statement is returning " + dsc) - You mau need to convert dsc to a string if it is not by default a string

If that returns an expected item then I would continue to add print statements at each critical step to ensure you are getting the expected results.

Please provide results of your print statements so I can further assist you.
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Hi James,

If this is a feature class, try the following below.  If this is a shapefile, you will have to update the SearchCursor to use FID instead of OBJECTID.

for lyr in arcpy.mapping.ListLayers(mxd):
    tlyr = lyr
    dsc = arcpy.Describe(tlyr)
    sel_set = dsc.fidSet
    if dsc.shapeType == "Polyline":
        rows = arcpy.SearchCursor(tlyr, "OBJECTID = " + sel_set)
        for row in rows:
            arcLength = row.ARCLENGTH
            shapeLength = row.shape.length
        if arcLength > 0:
            print arcLength
        else:
            print shapeLength
        del row, rows
0 Kudos
JamesSmith7
Emerging Contributor
Hi James,

If this is a feature class, try the following below.  If this is a shapefile, you will have to update the SearchCursor to use FID instead of OBJECTID.

for lyr in arcpy.mapping.ListLayers(mxd):
    tlyr = lyr
    dsc = arcpy.Describe(tlyr)
    sel_set = dsc.fidSet
    if dsc.shapeType == "Polyline":
        rows = arcpy.SearchCursor(tlyr, "OBJECTID = " + sel_set)
        for row in rows:
            arcLength = row.ARCLENGTH
            shapeLength = row.shape.length
        if arcLength > 0:
            print arcLength
        else:
            print shapeLength
        del row, rows


Jake and Michael,

I am using a feature class.  The script runs with no errors or results.  I am going to try and adjust the SearchCursor and see what happens.
0 Kudos
JamesSmith7
Emerging Contributor
Why would changing the SearchCursor to arcpy.da.SearchCursor return an SQL error, whereas, arcpy.SearchCursor does not?  I thought the only difference was arcpy.da.SearchCursor was more efficient?
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Take a look at the help links.  The arcpy.da.SearchCursor is formatted differently.

arcpy.SearchCursor
http://resources.arcgis.com/en/help/main/10.2/index.html#//018v00000050000000

arcpy.da.SearchCursor
http://resources.arcgis.com/en/help/main/10.2/index.html#//018w00000011000000

Can you post the exact code you're using in ArcMap?
0 Kudos
JamesSmith7
Emerging Contributor
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 == "Polyline":
  rows = arcpy.da.SearchCursor(tlyr, "OBJECTID = " + sel_set)
  for row in rows:
   arcLength = row.ARCLENGTH
   shapeLength = row.Shape_Length
  if arcLength > 0:
   print arcLength
  else:
   print shapeLength
  del row
  del rows


The error is in line 32, for row in rows: RuntimeError: An invalid SQL statement was used.  I know the select Object ID works, because it changes values depending on the feature selected.
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Check the name of your OBJECTID field (right-click on layer > Properties > Fields tab).  It may be OID, or OBJECTID1.
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Also, change arcpy.da.SearchCursor, to arcpy.SearchCursor.
0 Kudos
JamesSmith7
Emerging Contributor
Check the name of your OBJECTID field (right-click on layer > Properties > Fields tab).  It may be OID, or OBJECTID1.


The field is named OBJECTID.  Changing the cursor back to arcpy.SearchCursor eliminates the SQL Error.  But, the script runs with no results.
0 Kudos