How can I write a python add-in which zooms to each selected polygon after each button click?

2479
2
07-02-2015 11:25 AM
RobertBuckley1
Occasional Contributor

I want to have a button in a toolbar which when clicked, zooms to the extent of the next selected polygon. I am not sure whether this tool has been developed yet (although i´m sure it has) and do not know whether it should be a python button, or tool class.

Following code creates a list of selected elements. I want to go to the next row after each mouse click.

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()

The code creates the list, but how can I use a button zoom to the next row extent?

Tags (1)
0 Kudos
2 Replies
WesMiller
Regular Contributor III

You could create a layer file from your selected features then use the data driven pages toolbar to move forward and backward through your feature layer.

0 Kudos
DavidLamb
New Contributor III

If you are using a python add-in wizard, you can do something like this. On the first click, you store the envelopes in a class level property. If the list isn't empty then keep going through and setting extents. This worked on my machine running 10.2

import arcpy

import pythonaddins

class ButtonClass1(object):

    """Implementation for zoomto_addin.button (Button)"""

    def __init__(self):

        self.enabled = True

        self.checked = False

        self.mxd = None

        self.df = None

        self.envelopes = []

        self.lastIndex = 0

    def onClick(self):

        print "clicked"

        if len(self.envelopes)<1:

            self.mxd = arcpy.mapping.MapDocument('CURRENT')

            self.df = arcpy.mapping.ListDataFrames(self.mxd, "Layers") [0]

            print "getting stuff"

            # find the selection set

            SelLayer = arcpy.mapping.ListLayers(self.mxd,data_frame=self.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

                        self.envelopes.append(feat[0].extent) # grab the envelope

                print len(self.envelopes)

                self.lastIndex = 0

                self.setExtent

        else:

            self.setExtent()

    def setExtent(self):

        print len(self.envelopes)

        print self.lastIndex

        if self.lastIndex < (len(self.envelopes)-1):

            self.df.extent = self.envelopes[self.lastIndex]

            arcpy.RefreshActiveView()

            self.lastIndex += 1

        else:

            self.lastIndex = 0

            self.df.extent = self.envelopes[self.lastIndex]

            arcpy.RefreshActiveView()

            self.lastIndex += 1

0 Kudos