If else statement

3736
63
Jump to solution
08-28-2013 04:39 AM
JamesSmith7
New 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
New Contributor
Yes, but it only worked because you selected one feature.  If you select 2 features you will get nothing.  That is why you have to use the IN operator to work with a list of 0 or more items.


You are correct that the script only works on one record at a time.  But, currently that is all I need.
0 Kudos
JamesSmith7
New Contributor
Can you export a small portion of the feature class you are using, zip it, and upload it here?


I am working with a feature class.  But, exported the data as a shapefile.  I do not know how to zip one feature class within a file geodatabase.
0 Kudos
JamesSmith7
New Contributor
I guess I could have created a new file geodatabase, imported the feature class, and compressed the file geodatabase.
0 Kudos
JakeSkinner
Esri Esteemed Contributor
The problem is that the field is a String.  The empty value for the ARCLENGTH field contains a space.  The below code should work:

mxd = arcpy.mapping.MapDocument("CURRENT")
for lyr in arcpy.mapping.ListLayers(mxd):
    dsc = arcpy.Describe(lyr)
    sel_set = dsc.fidSet
    if dsc.shapeType == "Polyline":
        rows = arcpy.SearchCursor(lyr, "OBJECTID = " + sel_set)
        for row in rows:
            arcLength = row.ARCLENGTH
            shapeLength = row.shape.length
        if len(arcLength) > 1:
            print arcLength
        else:
            print shapeLength
    del row, rows
0 Kudos
JamesSmith7
New Contributor
The problem is that the field is a String.  The empty value for the ARCLENGTH field contains a space.  The below code should work:

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


Jake,

The script works using the Python Window within ArcMap.  But, not as a Script within a Toolbox.  The script runs, but I probably need to replace the print statements with AddMessages.
0 Kudos
MichaelVolz
Esteemed Contributor
Dan:

How do you call your printMessages.py script from other scripts?

Do you need to add this code directly to other python scripts?

Do you import printMessages into your python script?  If so, where does the printMessages.py need to reside with respect to the installed version of python (e.g. C:\Python26\python.exe)?
0 Kudos
JamesSmith7
New Contributor
Why would a Search Cursor not work to return the ARCLENGTH or Shape_Length value?

cursor = arcpy.da.SearchCursor("Lot_Lines", ["ARCLENGTH", "Shape_Length"])
 for row in cursor:
  row [0] = arcpy.AddMessage(str(row[0]))
  del row
  del cursor
0 Kudos
DanPatterson_Retired
MVP Emeritus
Michael
you can add the code directly to your scripts, or you can import the script...save it in the same folder as the main script
0 Kudos
JamesSmith7
New Contributor
Dan,

Using your suggestion, would you suggest replacing print arcLength with the following:

def printmessage():
   print 'ARCLENGTH'
if __name__ == '__main__':
   printmessage()


Then repeat for Shape_Length?

Thanks
0 Kudos