0

ArcPy Combobox How to leave a if-loop, when selection is found

I developed a Combox which:

  1. combines values from different featurelayers
  2. the User can select an ID or activitycode (acode)
  3. the active view zooms to the object (biotop or Protected Area)

The Combobox works very slow. Populating the Combobox with the values goes quite quick. But the zoom to the selected features is very slow. The problem is, that the if-loop, iterates through all featureclasses. I don't know how to leave the if-loop, when the selected feature is found. Currently it loops throgh the end of all tables of the listed featureclasses no matter if the selection already is found.

import sys, os, arcpy, zipfile, traceback, pythonaddins
from arcpy import mapping, env

class SiteComboBoxClass43(object):
    """Implementation for Site_Python_Addins_addin.combobox (ComboBox)"""
    def __init__(self):
    self.items = []
    self.editable = True
    self.enabled = True
    self.dropdownWidth = 'WWWWWWWWW'
    self.width = 'WWWWWWWWW'
    def onFocus(self, focused):
         if focused:
                self.mxd = arcpy.mapping.MapDocument('current')
                global items, site, akode, fc_biotop_pol, fc_biotop_poi, fc_biotop_li, fc_pro1_pol, fc_pro1_poi, fc_pro1_li
                items = []
                fields_site = "SITE_ID"
                akode = "AKODE"
                fc_biotop_pol = "Biotop (Polygon)"
                fc_biotop_poi = "Biotop (Point)"
                fc_biotop_li = "Biotop (Line)"
                fc_pro1_pol = "Protection Area 1 (Polygon)"
                fc_pro1_poi = "Protection Area 1 (Point)"
                fc_pro1_li = "Protection Area 1 (Line)"
                # build lists and populate the Combobox with sorted values
                fcsites1 = [fc_biotop_pol, fc_biotop_poi, fc_biotop_li, fc_pro1_pol, fc_pro1_poi, fc_pro1_li]
                fcakode1 = [fc_pro1_pol, fc_pro1_poi, fc_pro1_li]
                for x in fcsites1:
                    for row in arcpy.da.SearchCursor(x,fields_site): items.append(row[0])
                for x in fcakode1:
                    for row in arcpy.da.SearchCursor(x,akode): items.append(row[0])
                self.items = sorted(items)                            
    pass
    def onSelChange(self, selection):
            combobox.enabled = True
            self.value = selection
            self.mxd = arcpy.mapping.MapDocument('current')
            self.df = arcpy.mapping.ListDataFrames(self.mxd)[0]
            # looking for the selected ID or code in the attributetable
            if len(selection) > 8:
                fcsites2 = [fc_biotop_pol, fc_biotop_poi, fc_biotop_li, fc_pro1_pol, fc_pro1_poi, fc_pro1_li]
                for x in fcsites2:
                    arcpy.SelectLayerByAttribute_management(x, "NEW_SELECTION", fields_site + "='" + selection + "'")
            else:
                fcakode2 = [fc_pro1_pol, fc_pro1_poi, fc_pro1_li]
                for x in fcakode2:
                    arcpy.SelectLayerByAttribute_management(x, "NEW_SELECTION", akode + "='" + selection + "'")
            # zoom to Selection
            self.df.zoomToSelectedFeatures()
            self.df.scale = 15000
     pass
4
  • 2
    Use FIDset property of Describe object, and break the loop when length of it >0
    – FelixIP
    Commented Feb 29, 2016 at 20:19
  • @FelixIP: In my case, how does the Describe object works with the list-object and in which position do you think I should insert it? Commented Mar 1, 2016 at 14:41
  • d=arcpy.Describe(x);n=len(d.FIDset); if n>0:break
    – FelixIP
    Commented Mar 1, 2016 at 19:28
  • @FelixIP: I insert it like this, but it make no difference. Do you know what went wrong? for x in fcsites2: d = arcpy.Describe(x) n = len(d.FIDset) if n>0:break arcpy.SelectLayerByAttribute_management(x, "NEW_SELECTION", fields_site + "='" + selection + "'") Commented Mar 30, 2016 at 9:56

1 Answer 1

1

Based on the hint of Felix I use now the following code in the onSelChange Part and it works faster:

    def onSelChange(self, selection):
    self.value = selection
    sel_len = len(selection)
    if len(selection) > 8:
        fcsites2 = [fc_biotop_pol, fc_biotop_poi, fc_biotop_li, fc_pro1_pol, fc_pro1_poi, fc_pro1_li]
        for x in fcsites2:
            arcpy.SelectLayerByAttribute_management(x, "NEW_SELECTION", fields_site + "='" + selection + "'")
            d = arcpy.Describe(x) 
            n = len(d.FIDset)
            if n > 0:
                break
    else:
        fcakode2 = [fc_pro1_pol, fc_pro1_poi, fc_pro1_li]
        for y in fcakode2:
            arcpy.SelectLayerByAttribute_management(y, "NEW_SELECTION", akode + "='" + selection + "'")
            d = arcpy.Describe(y)
            n = len(d.FIDset)
            if n > 0:
                break
    self.df.zoomToSelectedFeatures()
    self.df.scale = 5000

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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