Select to view content in your preferred language

How do List and get field values of Currently Selected Features in a layer

963
2
Jump to solution
10-24-2013 01:34 PM
ShaneLim
Deactivated User
I have an add in that selects all the points of a specific layer within the currently zoomed in extents on a button click event.  Now I need to populate a combo box with the label field of all these features.  Does anybody know of a way to do this?  The search cursor wants the dataset which will not get just the currently selected.

Thanks for your time.

Shane
def onClick(self):         self.mxd = arcpy.mapping.MapDocument('current')         layers = arcpy.mapping.ListLayers(self.mxd)         self.items = []         for layer in layers:             if layer.name.lower() == "hydrantm":                 hydLayer = layer         print hydLayer         df = arcpy.mapping.ListDataFrames(self.mxd)[0]         extent = str(df.extent)         print extent         dfAsFeature = arcpy.Polygon(arcpy.Array([df.extent.lowerLeft, df.extent.lowerRight, df.extent.upperRight, df.extent.upperLeft]),                             df.spatialReference)         arcpy.SelectLayerByLocation_management(hydLayer, "INTERSECT", dfAsFeature, "", "NEW_SELECTION") 
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JakeSkinner
Esri Esteemed Contributor
Hi Shane,

The SearchCursor will work on the selection created from the Select by Location.  Try the following:

def onClick(self):         self.mxd = arcpy.mapping.MapDocument('current')         layers = arcpy.mapping.ListLayers(self.mxd)         self.items = []         for layer in layers:             if layer.name.lower() == "parks":                 hydLayer = layer         print hydLayer         df = arcpy.mapping.ListDataFrames(self.mxd)[0]         extent = str(df.extent)         print extent         dfAsFeature = arcpy.Polygon(arcpy.Array([df.extent.lowerLeft, df.extent.lowerRight, df.extent.upperRight, df.extent.upperLeft]),                             df.spatialReference)         arcpy.SelectLayerByLocation_management(hydLayer, "INTERSECT", dfAsFeature, "", "NEW_SELECTION")         global labels         labels = []         with arcpy.da.SearchCursor(hydLayer, ["NAME"]) as cursor:             for row in cursor:                 labels.append(row[0])         del cursor, row 


You will need to change "NAME" to the label field.  Then, you can then pass the global labels variable to the 'onFocus' function of the combo box:

def onFocus(self, focused):         self.items = labels


Note:  the arcpy.da.SearchCursor can only be used for 10.1 and beyond.  If you are using 10.0 you will need to use:

rows = arcpy.SearchCursor(hydLayer, "", "", "NAME") for row in rows:     labels.append(row.NAME) del row, rows

View solution in original post

0 Kudos
2 Replies
JakeSkinner
Esri Esteemed Contributor
Hi Shane,

The SearchCursor will work on the selection created from the Select by Location.  Try the following:

def onClick(self):         self.mxd = arcpy.mapping.MapDocument('current')         layers = arcpy.mapping.ListLayers(self.mxd)         self.items = []         for layer in layers:             if layer.name.lower() == "parks":                 hydLayer = layer         print hydLayer         df = arcpy.mapping.ListDataFrames(self.mxd)[0]         extent = str(df.extent)         print extent         dfAsFeature = arcpy.Polygon(arcpy.Array([df.extent.lowerLeft, df.extent.lowerRight, df.extent.upperRight, df.extent.upperLeft]),                             df.spatialReference)         arcpy.SelectLayerByLocation_management(hydLayer, "INTERSECT", dfAsFeature, "", "NEW_SELECTION")         global labels         labels = []         with arcpy.da.SearchCursor(hydLayer, ["NAME"]) as cursor:             for row in cursor:                 labels.append(row[0])         del cursor, row 


You will need to change "NAME" to the label field.  Then, you can then pass the global labels variable to the 'onFocus' function of the combo box:

def onFocus(self, focused):         self.items = labels


Note:  the arcpy.da.SearchCursor can only be used for 10.1 and beyond.  If you are using 10.0 you will need to use:

rows = arcpy.SearchCursor(hydLayer, "", "", "NAME") for row in rows:     labels.append(row.NAME) del row, rows
0 Kudos
ShaneLim
Deactivated User
Thanks worked great.  A little slow but gets the job done.

Shane
0 Kudos