If else statement

3578
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
JacobDrvar
New Contributor II
James:

Can you create a feature class with a small subset of features (say 10) and then print out the fidset?

It appears from Richard's last post that the semi-colon delimited string will need to be converted to a comma separated list.  You shouldbe able to find python methods to easily perform that process.


print "Data type: " +dsc.dataType
Data type: FeatureLayer
print "File path: " + dsc.path
File path: C:\User\County\Personalworkspace.gdb
print "Catalog path: " + dsc.catalogpath
Catalog path: C:\User\County\Personalworkspace.gdb\Lot_Lines
print "File name: " +dsc.file
File name: Lot_Lines
print "Base name: " + dsc.baseName
Base name: Lot_Lines
print "Name: " + dsc.name
Name: Lot_Lines
print dsc.shapeType
Polyline

Those all work, but when trying to run: print dsc.FidSet, no results are returned.  Is that what you were meaning, Michael?

I am now going to work on creating the comma separated list.
0 Kudos
JamesSmith7
New Contributor
James:

Can you create a feature class with a small subset of features (say 10) and then print out the fidset?

It appears from Richard's last post that the semi-colon delimited string will need to be converted to a comma separated list.  You shouldbe able to find python methods to easily perform that process.


print "Data type: " +dsc.dataType
Data type: FeatureLayer
print "File path: " + dsc.path
File path: C:\User\County\Personalworkspace.gdb
print "Catalog path: " + dsc.catalogpath
Catalog path: C:\User\County\Personalworkspace.gdb\Lot_Lines
print "File name: " +dsc.file
File name: Lot_Lines
print "Base name: " + dsc.baseName
Base name: Lot_Lines
print "Name: " + dsc.name
Name: Lot_Lines
print dsc.shapeType
Polyline

Those all work, but when trying to run: print dsc.FidSet, no results are returned. Is that what you were meaning, Michael?

I am now going to work on creating the comma separated list.
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Python is case sensitive.  Try dsc.fidSet
0 Kudos
JamesSmith7
New Contributor
Python is case sensitive.  Try dsc.fidSet


The result is the same whether I use dsc.fidSet, dsc.FidSet, or dsc.FIDSet.
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Attached is some data you can test with.  Add the feature class to ArcMap, select a single line, then execute the following:

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 arcLength > 0:
            print arcLength
        else:
            print shapeLength
    del row, rows
0 Kudos
RichardFairhurst
MVP Honored Contributor
The result is the same whether I use dsc.fidSet, dsc.FidSet, or dsc.FIDSet.


Are you positive your layer has a feature selection already applied?  If nothing is selected, no FID values should be returned.
0 Kudos
JamesSmith7
New Contributor
Attached is some data you can test with.  Add the feature class to ArcMap, select a single line, then execute the following:

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 arcLength > 0:
            print arcLength
        else:
            print shapeLength
    del row, rows


Jake, the script works with the feature class you provided, using the Python window within ArcMap.
0 Kudos
JamesSmith7
New Contributor
Are you positive your layer has a feature selection already applied?  If nothing is selected, no FID values should be returned.


Richard,

I am using the select by rectangle to select one Polyline before running the script.
0 Kudos
RichardFairhurst
MVP Honored Contributor
Jake, the script works with the feature class you provided, using the Python window within ArcMap.


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.

OBJECTID = 12345; 12346

is an invalid selection statement (not sure how Python resolves the list into the SQL statement, but = cannot be used with a list).

OBJECTID IN (12345, 12346)

is valid and will return both records.

OBJECTID IN (12345; 12346)

Returns nothing since a semicolon is invalid in an SQL list.
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Can you export a small portion of the feature class you are using, zip it, and upload it here?
0 Kudos