Select to view content in your preferred language

Pan/Zoom to selected features in Python Add-in

4667
2
03-11-2014 09:39 AM
DanEvans
Frequent Contributor
Hi,

I am trying to write a simple Python add-in that provides a combo box allowing the user to enter a project number. They can then click buttons to 'draw' (i.e. select all the pipe features in that project), pan to (select the pipes in the project, then pan to them, keeping the current scale) or zoom to (select the pipes in the project, then zoom to their extent). If the user enters a project number and presses enter, the default behaviour is to just 'draw'/select the pipes in the project.

The 'draw'/select functionality works fine, either by pressing enter or clicking the button. The pan and zoom to buttons don't pan or zoom though! They do the selection okay, but the view stays in the same extent and scale.

My code is below. Any help would be greatly appreciated!

Dan

import arcpy
import pythonaddins

class DrawProject(object):
    """Implementation for MRPAssistant_addin.panbutton (Button)"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
        mxd = arcpy.mapping.MapDocument('CURRENT')
        df = arcpy.mapping.ListDataFrames(mxd)[0]    
        layer = arcpy.mapping.ListLayers(mxd, "DistributionMain", df)[0]
        arcpy.SelectLayerByAttribute_management(layer, "NEW_SELECTION", "[MRP_EZRESULTS.PROJECTID] = " + projectsearchcombobox.value)
        arcpy.RefreshActiveView()
    
class PanToProject(object):
    """Implementation for MRPAssistant_addin.panbutton (Button)"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
        mxd = arcpy.mapping.MapDocument('CURRENT')
        df = arcpy.mapping.ListDataFrames(mxd)[0]
        layer = arcpy.mapping.ListLayers(mxd, "DistributionMain", df)[0]
        arcpy.SelectLayerByAttribute_management(layer, "NEW_SELECTION", "[MRP_EZRESULTS.PROJECTID] = " + projectsearchcombobox.value)
        df.panToExtent(layer.getSelectedExtent())
        arcpy.RefreshActiveView()

class ProjectSearchComboBox(object):
    """Implementation for MRPAssistant_addin.projectsearchcombobox (ComboBox)"""
    def __init__(self):
        self.items = [""]
        self.editable = True
        self.enabled = True
        self.dropdownWidth = 'WWWWWW'
        self.width = 'WWWWWW'
    def onSelChange(self, selection):
        pass
    def onEditChange(self, text):
        global project
        project = text
    def onFocus(self, focused):
        pass
    def onEnter(self):
        mxd = arcpy.mapping.MapDocument('CURRENT')
        df = arcpy.mapping.ListDataFrames(mxd)[0]
        layer = arcpy.mapping.ListLayers(mxd, "DistributionMain", df)[0]
        arcpy.SelectLayerByAttribute_management(layer, "NEW_SELECTION", "[MRP_EZRESULTS.PROJECTID] = " + project)
        arcpy.RefreshActiveView()
    def refresh(self):
        pass

class ZoomToProject(object):
    """Implementation for MRPAssistant_addin.zoombutton (Button)"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
        mxd = arcpy.mapping.MapDocument('CURRENT')
        df = arcpy.mapping.ListDataFrames(mxd)[0]
        layer = arcpy.mapping.ListLayers(mxd, "DistributionMain", df)[0]
        arcpy.SelectLayerByAttribute_management(layer, "NEW_SELECTION", "[MRP_EZRESULTS.PROJECTID] = " + projectsearchcombobox.value)
        df.extent = layer.getSelectedExtent()
        arcpy.RefreshActiveView()
Tags (2)
0 Kudos
2 Replies
MathewCoyle
Honored Contributor
That should work. Are you sure you only have one data frame? If you are getting to the point of the selection working properly I don't see what would be causing these issues. Try removing, recreating, and reinstalling the add-in. Maybe it is still hanging on to an older version.

Are you getting any error messages?

You could also try this to see if you get any different behaviour.
df.zoomToSelectedFeatures()


As a side note, this would be my preferred manner of doing the pan.
df.extent = df.panToExtent(layer.getSelectedExtent())
0 Kudos
DanEvans
Frequent Contributor
Thanks for the reply Mathew.

I made some changes to (seemingly) unrelated parts of the script today and somewhere along the line the pan and zoom buttons started working again!

The only (minor) issue now is that zooming to selected features, especially from a small scale, doesn't zoom all the way in on the first click, and if you then click zoom again it'll zoom properly. Sometimes you have to click three times to zoom right in... It's weird, but not a massive problem at the moment.

Thanks

Dan
0 Kudos