[GIS] Having arcpy.da.SearchCursor access only selected row in attribute table

arcgis-10.2arcmaparcpycursor

In ArcMap 10.2, I'm attempting to build a tool that, when run, will iterate through a network of pipes to locate an outfall point. My network is composed of polyline data representing pipe, with input points at the junctions between polyline sections. Pipes have an Upstream and Downstream field, corresponding to Input points and Outfall points (at pipe terminus). My thinking is to have a user select a point in the network, at which point a search cursor in the input table would pull the ID value for said point, and feed it to a search cursor in the Upstream field of the pipe table. From there I would have two search cursors (one Upstream, one Downstream) iterate through until an Outfall value is reached in the Downstream field (all Outfall ID values begin with "OF"). All ID field values are strings.

I'm having trouble getting my initial search cursor to return only the value of the selected row for the input table. Here is my code so far:

import arcpy
arcpy.env.workspace = "C:\\Users\\Don\\Desktop\\Key\\Thesis\\Storm_Sewer"
arcpy.MakeFeatureLayer_management("GISDATA_STORM_INPUT_COMB", "inpLayer")
arcpy.MakeFeatureLayer_management("GISDATA_STORM_PIPE", "pipLayer")

fc = "inpLayer"
ds = "pipLayer"
pfield = "US_strip"
field = "ID_COMB"

bCursor = arcpy.da.SearchCursor(fc,field)
pCursor = arcpy.da.SearchCursor(ds,pfield)

for row in bCursor:
    print(row[0])

Is it possible to have the searchcursor only act on the selected row? I had thought that, so long as the cursor was acting on a feature layer, this would occur automatically. Instead, this code feeds me back all the values in the field.

Best Answer

I am afraid you are missing the selection part with the feature layers. This is the workflow you are following:

  1. User opens a map document with some layers or add a feature class into the map as a layer.
  2. User selects a feature in a map layer.
  3. User runs your script.
  4. User gets the attributes of the selected feature printed.

Here is where the problem is: when user has made the selection in the map layer (i.e., the feature layer), you don't access this particular feature layer user has selections in. Instead, you create a new feature layer that comes from a geodatabase feature class. You are essentially never accessing the layer in the map.

This is what you need to do instead:

import arcpy

fields = ["Name","State_Name"]

mxd = arcpy.mapping.MapDocument("CURRENT")
lyr = arcpy.mapping.Layer("counties")

bCursor = arcpy.da.SearchCursor(lyr,fields)

for row in bCursor:
    arcpy.AddMessage("County name: {0}; state name: {1}".format(row[0],row[1]))

The output in ArcMap session:

enter image description here

You use the Layer object that does contain the information on selections and its data source is the feature class where the actual attributes are retrieved. I suggest reviewing arcpy.mapping concepts at this Esri Help page.

Related Question