3

I would like to use a python add-in which uses a button click to iterate through each selected polygon one by one.

The following code manages to zoom only to the first polygon. How can I adapt it to zoom to each selected polygon?

mxd = arcpy.mapping.MapDocument('CURRENT')
df = arcpy.mapping.ListDataFrames(mxd, "Layers") [0]
Envelopes = [] # store extents here

# find the selection set
SelLayer = arcpy.mapping.ListLayers(mxd,data_frame=df)[0] # first layer
fidSet   = arcpy.Describe(SelLayer).FIDSet

if len(fidSet) == 0:
    arcpy.AddMessage("Nothing selected")
else:
    # now cursor through it an get the geometries
    # storing their extents into a list
    with arcpy.da.SearchCursor(SelLayer,"SHAPE@") as SCur:
        for feat in SCur:
            # I'm going to assume polygon/polyline
            Envelopes.append(feat[0].extent) # grab the envelope 

    df.extent = Envelopes[0] # first extent
    arcpy.RefreshActiveView()
  • This code looks OK to me collecting the extents, though you are only zooming to the first one outside the loop. Can you check the length of the envelopes list to see if it contains all the extents ? – kes Jun 30 '15 at 15:31
  • len(Envelopes) iterated through 191 Polygons. PS have edited the question...I want to zoom to the next selected polygon with each subsequent button click. – Robert Buckley Jun 30 '15 at 15:34
  • That code looks somewhat familiar... in your addin tool create the empty list on the base level and then in the button click sub call global Envelopes (to tell python you want the global version, not make a new one) also you need to add an index variable and global that, then Index_Variable += 1, df.extent = Envelopes[Index_Variable], refresh.. Make sure you set your Index_Variable = 0 each time you get a new set. – Michael Stimson Jul 1 '15 at 2:28
  • I see what your saying, but it´s a bit difficult to put into practice. There are two variables here, first the feature index and then the envelope index. – Robert Buckley Jul 1 '15 at 6:32
3

You are collecting the extents for each polygon correctly, but need to zoom to them within the loop, or a later loop depending on what you want to do.

For example to zoom in the same loop as the search cursor e.g.

with arcpy.da.SearchCursor(SelLayer,"SHAPE@") as SCur:
    for feat in SCur:
        # I'm going to assume polygon/polyline
        Envelopes.append(feat[0].extent) # grab the envelope 
        df.extent = feat[0].extent # first extent # <- Move this into the loop
        arcpy.RefreshActiveView()

        time.sleep(5) # Wait 5 Seconds. 
        # ^ Replace this with what you want to do at each poly.
  • That's not quite what it's about... he wants to collect all the extents (see the code) and then when a button is clicked go to the next extent in the list. This code will just zoom to each one with 5 second intervals which is no different to the code he already has. – Michael Stimson Jul 1 '15 at 2:30
  • That´s right. After every click I would like to zoom to the next selected polygon. – Robert Buckley Jul 1 '15 at 5:31
  • @Robert, Sorry I answered before you'd asked for the zooming after each onclick (SE discourages answers in comments). In that case my understanding is to capture the mouse click you would need to implement your AddIn as a Tool Class which has an onMouseDown event/hook where you would iterate through the geometries and zoom to them. – kes Jul 1 '15 at 11:26
  • 1
    Hopefully someone can come with a full answer, but what @Michael says is correct. You could record the envelope loop and the position in the array as attributes/properties on your tool class so they are available on each button click, while the tool is active. – kes Jul 1 '15 at 11:34

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.