"zoom to selected feature" possible in arcpy.mapping?

2,657 views
Skip to first unread message

Denis Potvin #5

unread,
Dec 7, 2011, 7:00:30 PM12/7/11
to geop...@googlegroups.com
I'd like to write something where a user can open an mxd, run a python script that will allow them to enter a value. That value will be used to select against a dataset. The selected subset should then be the new map extent. if you were doing this manually it would be "zoom to selected feature"

is that possible in arcpy.mapping or some other python module?

The BigB

unread,
Dec 8, 2011, 7:25:10 PM12/8/11
to Python - ARCGIS geoprocessing
Yah I am thinking that you could do this by creating a featureLayer
from the selected subset and using describe to grab the extent from
that new layer. You could then apply that extent to the data frame
extent.

Conz

unread,
Dec 8, 2011, 7:35:35 PM12/8/11
to Python - ARCGIS geoprocessing
Yep, in the arcpy,mapping module the dataframe class has a
zoomToSelectedFeatures() method. So it would look something like:

import arcpy
mxd = arcpy.mapping.MapDocument('CURRENT')
df = arcpy.mapping.ListDataFrames(mxd, "Layers") [0]
df.zoomToSelectedFeatures()

Obviously you'd have to modify the code to include your custom
selection. More discussion can be found in this thread on the ESRI
forums:

http://forums.arcgis.com/threads/7703-Adding-Zoom-to-Selected-feature-to-execute-at-the-end-of-model.

Denis Potvin #5

unread,
Dec 12, 2011, 2:46:56 PM12/12/11
to geop...@googlegroups.com
Thank you both. I need to do some investigating because it seemed to zoom into the wrong layer. Now my script keeps hanging before it even gets to that part so I need to get my divining rod, my magic wand, and figure out what went wrong.

ptho...@gmail.com

unread,
Nov 25, 2013, 3:44:44 PM11/25/13
to geop...@googlegroups.com
Did you ever get an answer to this and if so would you mind posting it?  Thank you :-)

Denis Potvin #5

unread,
Jan 6, 2014, 5:30:54 PM1/6/14
to geop...@googlegroups.com, ptho...@gmail.com
sorry, it's been a long time since I looked at this code. I vaguely remember that part of it was human error, part of it was me forgetting to clear the definition query - in a sense, resetting the feature layer, and then putting in the new definition qry.
 
for what it's worth, here's my module:

def dataFrameFilter(self, df, defQry, insetmap):

# get layer object

arcpy.AddMessage(

"beginning layer filter of data frame" + df.name)

arcpy.AddMessage(defQry)

lyr = arcpy.mapping.ListLayers(obj.mxd,

"Tantalis Files", df)[0]

arcpy.AddMessage(lyr.name)

arcpy.AddMessage(

"defined the lyr for Tantalis Files?")

# ARGH! REMEMBER TO CLEAR DEFINITION QUERY FIRST

# otherwise there'll be nothing to query if it's the 2nd time !

lyr.definitionQuery =

""

# Do selection and zoom in

arcpy.SelectLayerByAttribute_management(lyr,

"CLEAR_SELECTION")

arcpy.SelectLayerByAttribute_management(lyr,

"NEW_SELECTION", defQry)

obj.SelectedSetChecker(lyr)

# check for no polys or zero area

df.zoomToSelectedFeatures()

arcpy.AddMessage(

"zoomed to my selected feature! ")

arcpy.SelectLayerByAttribute_management(lyr,

"CLEAR_SELECTION")

lyr.definitionQuery = defQry

# filter the layer to just the ones you want

# main map, tiny polygon, and no inset, then zoom out

if df.name == "Layers" and df.scale < 10000 and insetmap <> "None":

df.scale =

10000

else: # otherwise do the scale rounding as per routine

df.scale = obj.scalefixer(df.scale)

Reply all
Reply to author
Forward
0 new messages