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:

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.

  • Deal with layer, i.e. parts of Map Document and cursor will honour selection – FelixIP Apr 11 '16 at 1:31
  • 1
    Please do not use an image to present code -- Doing so forces anyone who would help you to retype the code. Instead please use the code block formatting ({} icon). – Vince Apr 11 '16 at 1:35
  • Felix, I'm a little unsure what you mean by "parts of Map Document", is creating Feature Layers as I've done different from what you mean? – DonPineapple Apr 11 '16 at 1:49
  • you haven't selected any features after creating the feature layer – Midavalo Apr 11 '16 at 1:55
  • 1
    He means that in order to have a selection, you need to open the layer in a map document first. You can't have a selection on a feature class on disk. However, having said that, you can use a where_clause to only search features which meet a criteria. Like you would in a selection. You can use search cursors on layers in a map document and they'll honor selections. – Fezter Apr 11 '16 at 1:56

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.