SearchCursor dependent on selected items?

954
2
Jump to solution
09-23-2016 09:19 AM
IrvLustig
New Contributor

Using ArcMap 10.2.2, I created a simple Network Analyst Vehicle Routing problem with 8 points, and 1 depot, and solved it.  I then saved the layer to a ".lyr" file and used the program below to output information about the points I had added:

dirpath = "c:/temp"
env.workspace = dirpath
thelayer = "mike4.lyr"
na_layer = arcpy.mapping.Layer(os.path.join(dirpath, thelayer))
# Get the names of all the feature classes within the VRP layer.
sub_layer_names = arcpy.na.GetNAClassNames(na_layer)

# Stores the layer names that we will use later
orders_layer_name = sub_layer_names['Orders']

# Get the orders sublayer
orders_sublayer = arcpy.mapping.ListLayers(na_layer, orders_layer_name)[0]

# Get the orders
with arcpy.da.SearchCursor(in_table=orders_sublayer,
        field_names=('Name','Description',
                     'RouteName','Sequence',
                     'SHAPE@XY')) as cursor:
    for row in cursor:
        print "Name ", row[0], "RouteName ", row[2], "Sequence ",row[3],"shape ",row[4]
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

The problem I was having is that it was only printing ONE of my 8 points I had created.  I then discovered that when I saved the layer file in ArcMap, I had selected that single point.  If I selected 4 of the 8 points, and saved, it only printed those 4 points.

So my question is, what is the right way to alter the above code so that it will print all the Orders independent of whether the layer file was saved and had some of the orders selected or not?

0 Kudos
1 Solution

Accepted Solutions
JakeSkinner
Esri Esteemed Contributor

Hi Irv,

You can either clear the selection and recreate the layer file, or you can add the following to your code to do this:

na_layer = arcpy.mapping.Layer(os.path.join(dirpath, thelayer))
arcpy.MakeFeatureLayer_management(na_lyr, "na_lyr")

arcpy.SelectLayerByAttribute_management("na_lyr","CLEAR_SELECTION")

View solution in original post

2 Replies
JakeSkinner
Esri Esteemed Contributor

Hi Irv,

You can either clear the selection and recreate the layer file, or you can add the following to your code to do this:

na_layer = arcpy.mapping.Layer(os.path.join(dirpath, thelayer))
arcpy.MakeFeatureLayer_management(na_lyr, "na_lyr")

arcpy.SelectLayerByAttribute_management("na_lyr","CLEAR_SELECTION")
IrvLustig
New Contributor

Thanks.  Is that documented anywhere that SearchCursor only returns the selected features?

0 Kudos