I'm using this thread as the base for my python addin toolbar.How to populate Add-In Combo Box with attributes
I was wondering if it was possible to limit the field unique values to whats in the map extent. There are thousands of unique values which doesn't take long to populate the combobox but to help the end user I would like to limit those to what's in the map extent.
Here's my python code so far. You select the specific layer, then it sorts and pushes the unique values to a combobox. Once you select a feature from the drop down, it selects and zooms to that feature.
import arcpy import pythonaddins class leveeLayerListSelector(object): """Implementation for LeveeSearch_addin.comboboxLayerList (ComboBox)""" def __init__(self): self.items = [] self.editable = True self.enabled = True self.dropdownWidth = 'WWWWWWWWWWWWWWWWWWWWWWWWWW' self.width = 'WWWWWW' def onEditChange(self, text): pass def onFocus(self, focused): if focused: self.mxd = arcpy.mapping.MapDocument('current') layers = arcpy.mapping.ListLayers(self.mxd) self.items = [] for layer in layers: self.items.append(layer.name) def onEnter(self): pass def refresh(self): pass def onSelChange(self, selection): global fc fc = arcpy.mapping.ListLayers(self.mxd, selection)[0] class leveeSystemSelector(object): """Implementation for LeveeSearch_addin.comboboxLeveeSystem (ComboBox)""" def __init__(self): self.items = [] self.editable = True self.enabled = True self.dropdownWidth = 'WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW' self.width = 'WWWWWW' def onSelChange(self, selection): self.mxd = arcpy.mapping.MapDocument('current') df = arcpy.mapping.ListDataFrames(self.mxd, "Layers") [0] arcpy.SelectLayerByAttribute_management(layer, "NEW_SELECTION", "System_Name = '" + selection + "'") df.zoomToSelectedFeatures() arcpy.RefreshActiveView() def onEditChange(self, text): pass def onFocus(self, focused): self.mxd = arcpy.mapping.MapDocument('current') global layer layer = arcpy.mapping.ListLayers(self.mxd, fc)[0] self.items = [] values = [row[0] for row in arcpy.da.SearchCursor(layer, ["System_Name"])] for uniqueVal in sorted(set(values)): self.items.append(uniqueVal) def onEnter(self): pass def refresh(self): pass
-Steven
Solved! Go to Solution.
You may consider changing the onFocus to populateCombobox function and recreate onFocus calling populateCombobox and then you could also call it from your clear selection button.
Thanks! I was in the process of moving the location search to a button and now it seems to be functioning right. I will test some more and post my code.
-Steven
Here's my full code. This is for use within an esri addin. The first button allows you to select the features that are within the extent. The combobox has unique values of a specified field from the features within the extent. Select a feature to zoom to it. The 'X' button clears selected results. The PDF export button isn't functional yet. The PDF button will be for exporting the selected feature as a PDF map while dynamically adding attributes to the pdf map. I don't know if this is possible yet. I'll probably be opening another thread for that part. Thanks for the help this far
-Steven
import arcpy import pythonaddins class selectByLocation(object): """Implementation for LeveeSearch_addin.ExportPDF (Button)""" def __init__(self): self.enabled = True self.checked = False def onClick(self): self.mxd = arcpy.mapping.MapDocument('current') df = arcpy.mapping.ListDataFrames(self.mxd, "Region VII") [0] layer = arcpy.mapping.ListLayers(self.mxd, "Levee Centerline (Segment Only)")[0] extent = df.extent extentPoly = arcpy.Polygon(arcpy.Array([arcpy.Point(extent.XMin, extent.YMin), arcpy.Point(extent.XMin, extent.YMax), arcpy.Point(extent.XMax, extent.YMax), arcpy.Point(extent.XMax, extent.YMin)])) arcpy.SelectLayerByLocation_management(layer, 'intersect', extentPoly) class LeveeSystemSelector(object): """Implementation for LeveeSearch_addin.LeveeSystemName (ComboBox)""" def __init__(self): self.items = [] self.editable = True self.enabled = True self.dropdownWidth = 'WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW' self.width = 'WWWWWW' def onEditChange(self, text): pass def onFocus(self, focused): global layer global df self.mxd = arcpy.mapping.MapDocument('current') layer = arcpy.mapping.ListLayers(self.mxd, "Levee Centerline (Segment Only)")[0] df = arcpy.mapping.ListDataFrames(self.mxd, "Region VII") [0] self.items = [] values = [row[0] for row in arcpy.da.SearchCursor(layer, ["FC_SYSTEM.System_Name"])] for uniqueVal in sorted(set(values)): self.items.append(uniqueVal) def onSelChange(self, selection): arcpy.SelectLayerByAttribute_management(layer, "NEW_SELECTION", "FC_SYSTEM.System_Name = '" + selection + "'") df.zoomToSelectedFeatures() arcpy.RefreshActiveView() def onEnter(self): pass def refresh(self): pass class ClearSelection(object): """Implementation for LeveeSearch_addin.ClearSelected (Button)""" def __init__(self): self.enabled = True self.checked = False def onClick(self): arcpy.SelectLayerByAttribute_management(layer, "CLEAR_SELECTION") class PDFExport(object): """Implementation for LeveeSearch_addin.exportPDF (Button)""" def __init__(self): self.enabled = True self.checked = False def onClick(self): pass