Select to view content in your preferred language

Adding Zoom to Selected feature to execute at the end of model.

16966
30
07-06-2010 10:24 AM
RyanFurlong
Regular Contributor
Greetings everyone, I work for a retailer and we have a shapefile of all our stores and they are individually ID'ed with a store number.  I have a very simple model that allows the user to input the store number and then the model selects it.  Ideally I would like the model to automatically zoom to the selected feature, right now I have to click the "zoom to feature" tool as a work around but I would like to not have to do that.  Do I have to write a custom scrip or is there an option I can just check in model builder?
0 Kudos
30 Replies
ChrisSnyder
Honored Contributor
Can't do it in v9.3, but now in v10.0 there is a zoomToSelectedFeatures() method as part of the arcpy.mapping module. This would probably have to be in script format (not modelbuilder) to work though....
0 Kudos
RyanFurlong
Regular Contributor
That is a shame to hear that, I was hoping it would be a simple thing to add.  Anyone else have ideas on different ways to accomplish this?
0 Kudos
ChrisMathers
Deactivated User
You can write a simple script and add it as part of the model, that way you stay in the MB environment.
0 Kudos
GeorgeDunbar
Occasional Contributor
It might be simple for you, but I have never written a script. I'm not even sure i what editor to write it, let alone what syntax to use or where to store it. Can anyone point me to an idiots guide to script writting?
0 Kudos
ChrisMathers
Deactivated User
Well the whole script in 10 would be:

import arcpy
MXD=arcpy.mapping.mapdocument('current') #get access to the current mxd
dataframe = arcpy.mapping.ListDataFrames(MXD, "Layers")[0] #grab the dataframe object you want (probably named Layers)
dataframe.zoomToSelectedFeatures() #zoom to the feature in memory
arcpy.RefreshActiveView() #refresh the map so that the extent change shows up


This is written in python 2.6 in the IDLE IDE and saved really wherever you want then added to a toolbox in your toolbox dialog. The help documents are a great place to start for learning this stuff. You could do this in ArcObjects but thats a giant hassle and a whole other ball game code wise. Python.org has lots of resources to get started with python in general and if you want something professionally taught ESRI and many others offer 'Python for ArcGIS' classes. I learned what I know about python from Learning Python by O'Rielly, the ArcGIS help pages and lurking in #python on IRC.
0 Kudos
KyleValkenburg
Deactivated User

What needs to change in this code for it to be able to be published as a geoprocessing service?  This code works for me in Desktop, but Server does not like the "current" mxd......

0 Kudos
WesMiller
Deactivated User

Try changing the 'Current' keyword to a path to an mxd

0 Kudos
RyanFurlong
Regular Contributor
This is great!  Thank you clm42!
0 Kudos
SueBreeden
Deactivated User
I have the python script that will zoom to selected features. It works great outside of model builder, however; when I add the script to a model and connect it to the output of Select Layer By Attributes it doesn't work. The output is set as a precondition before the script runs. I don't receive any errors when I run the model, it just doesn't zoom to the selected features. Any insight to this problem would be helpful. Thanks
0 Kudos