Return the value of field in a selected feature

2657
3
01-31-2018 11:56 AM
JoeBorgione
MVP Esteemed Contributor

I have a set of points that I need to cycle through and for each point, spatially select the polygon in which that particular point falls.  I then need to compare a couple values in the point to the value of a field in the selected polygon.

Stepping through the points is easily accomplished with an arcpy.da.SearchCursor; I can make the spatial selection easily too.  But how do I get the value of a particular field in the selected polygon? (It's my understanding that nesting another search cursor after Line 4 is bad practice.)

fields = ["StreetDir", "HouseDir"]
with arcpy.da.SearchCursor("points",fields) as cursor:
   for row in cursor:
      arcpy.SelectLayerByLocation_management("quads","CONTAINS","points")
‍‍‍‍

StreetDir and HouseDir have values of N,S, E or W.

The quads feature layer has a field called DirValues in the form of a tuple: ("S", "W") or ("S", "E") etc.  I need to see if row[0] and row[1] are in the selected tuple.

Is there a way to tease values out with describe?  Or is there some "get-selected-feature-field-value" method I haven't found with google?

That should just about do it....
0 Kudos
3 Replies
DanPatterson_Retired
MVP Esteemed Contributor

Joe... you will have the selection, from which you will have to get the values from that field for the table that is being queried.  You should then be able to query for the whole record based on the OBJECTID value and if you already know what the field is called, then you are done.  So, the select by location isolates the features, from which you should do a selectbyattributes 'from the selected records' (like a numpy 'where')

0 Kudos
JoeBorgione
MVP Esteemed Contributor

Not sure I follow, Dan.  I don't know the OID of the selected polygon; the polygon attributes are what I'm after.  

Here is one direction I've taken.  Mixing a little python lists with arcpy da search cursor:

fields = ["OBJECTID", "StreetDir", "HouseDir"]
pointsList = []
with arcpy.da.SearchCursor("points",fields) as cursor:
    for row in cursor:
    pointsList.append(row)
## provides a list of tuples in the form of (oid, sd, hd)
##[(61464, u'W', u'N'), (45273, u'W', u'N'), (45278, u'E', u'S')(etc)]

for i in range(len(pointsList)):
    oid = pointsList[i][0]
    sd = pointsList[i][1]
    hd = pointsList[i][2]
    selectPoints = "OBJECTID = {}".format(oid)
    arcpy.SelectLayerByAttribute_management("points","NEW_SELECTION",selectPoints)
    # now I have one point selected
    arcpy.SelectLayerByLocation_management("quads","CONTAINS","points")
    # and now I have the polygon in which that selected point is within
    fc = "quads"
    field = "DirValues"
    with arcpy.da.SearchCursor(fc,field) as cursor:
        for row in cursor:
            list = '{}'.format(row[0])
# now I can compare the selected point directionals (sd and hd) and the directionals from the
# selected 'quads' polygon‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍; list is in the form of ('N','W')....
# with something like:
# if sd and in list:
#   pass
#  elif:
#   "flag as an error"
  
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
That should just about do it....
0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

Joe, I think we are on the same page(ish) You have the selected polygon(s) via selectbylocation, then you just need to query the OBJECTID field... that will give you the id (if there are more than one per chance), and since you know the other field names, you can just extract the attributes as needed

0 Kudos