Select to view content in your preferred language

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

16129
30
07-06-2010 10:24 AM
RyanFurlong
New Contributor III
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
ChrisMathers
Regular Contributor II
If it works outside of arcmap I assume you are using ArcGIS 10. Try selecting a feature by hand and then pasting each line from the script into the python window in ArcMap one at a time. There may be a typo in the code that will rear its head when you are using the code interactivly. Are there multiple data frames in the map?
0 Kudos
SueBreeden
New Contributor
Good morning, thanks for the fast response. I am using Arcgis 10, the code works great in the python window. I can select a parcel with the select tool, or from the attribute table and the code in the python window works. I can also build an expression using the Select by Attributes in Arcmap and the code works in the python window. I have saved the code as a python script file. I can double click the script outside of the python window and it works great as well.The only time it doesn't zoom to the selected feature is when I add the script file to a geoprocess in model builder. It doesn't stop the process or give an error it just doesn't zoom to the selected feature.  I can remove the script file from my model and run the process,and then double click the script file outside of the model and it will work, it just doesn't work within model builder.I have only one data frame and one shapefile added to the map doc.

Code for zoom to selected script

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

I have attached an image of my simple model. Thanks for your help.
0 Kudos
ChrisMathers
Regular Contributor II
I cant imagine why this would be causing a problem, but could you try modifiying the model so that you dont use a feature layer? If the shapefile is already loaded into ArcMap you shouldnt need to make it a feature layer in the model.
0 Kudos
SueBreeden
New Contributor
Thanks so much for the help. The Make Feature Layer was the problem in the model. I removed it and all works well; my model with script runs great now. Appreciate the help.
0 Kudos
RyanFurlong
New Contributor III
Would anyone know how to code something similar as an add-in?  I am trying to do it myself but I am not having much luck.  What I would want the add in to do is select an attribute in 1 specified layer based on user input say like id number for the attribute and then just zoom to it.  It would have to have an input box to enter in the feature ID then a button to initiate the select and zoom to feature.

Pseudo code: 

input number = feature ID
select feature based on entered number
zoom to feature
MikeHouts
New Contributor
I have built a simple model for a user to select a polygon, now I want have the view extent zoom to that selected feature. 

I saw the above script sample and added it to the end of the model, but it would not work.  After searching around the web, found another similar example that zoomed to a scaling of the selected extent, but this does not work either.

Here is the code and the error message.  Any help would be greatly appreciated.  Im just learning Python and am trying to figure this out on the fly.


import arcpy
mxd = ("G:\search_zoom\search_zoom.mxd")
df = (mxd, "PLSS_u1483")[0]
Layer = (mxd, "section", df)[0]
df.zoomToSelectedFeatures(layer)
df.scale = df.scale * 1.1
arcpy.RefreshActiveView()


File "G:\search_zoom\zoom2selected2.py", line 5, in <module>
    df.zoomToSelectedFeatures(layer)
AttributeError: 'str' object has no attribute 'zoomToSelectedFeatures'
0 Kudos
SueBreeden
New Contributor
Could it be the layer in line 5 should be Layer. It's very case sensitive.

df.zoomToSelectedFeatures(layer)

change to:   df.zoomToSelectedFeatures(Layer)
0 Kudos
MikeHouts
New Contributor
Thanks for the response.  I caught that after posting, thats not it.  I tried changing that line a lot of ways and the error always changes to whatever I change the "variable" to as I tried substituing in different expressions for what df. extent =


AttributeError: 'str' object has no attribute 'zoomToSelectedFeatures'
AttributeError: 'str' object has no attribute 'Layer'
AttributeError: 'str' object has no attribute 'section'
AttributeError: 'str' object has no attribute 'selected'
AttributeError: 'str' object has no attribute 'getSelectedExtent'
0 Kudos
ChrisMathers
Regular Contributor II
The problem is the line where you define your dataframe. It should be df=arcpy.mapping.ListDataFrames(MXD, "Layers")[0] instead of df = (mxd, "PLSS_u1483")[0].
0 Kudos
MikeHouts
New Contributor
I had that originally...tried changing it back just now, so script is currently

import arcpy
mxd = ("G:\search_zoom\search_zoom.mxd")
df=arcpy.mapping.ListDataFrames(MXD, "Layers")[0]
Layer = (mxd, "section", df)[0]
df.zoomToSelectedFeatures(layer)
df.scale = df.scale * 1.1
arcpy.RefreshActiveView()

with error of:

File "G:\kpw\arc_server\auto_assessment\search_zoom\zoom2selected2.py", line 3, in <module>
    df=arcpy.mapping.ListDataFrames(MXD, "Layers")[0]
NameError: name 'MXD' is not defined

I originally had the arcpy.mapping.... starting line 2 as well, but that gave me the same error, so I removed it and just went straight to a direct reference and then it read fine until the next arcpy.mapping....

What am I missing?
0 Kudos