Select to view content in your preferred language

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

16971
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
SueBreeden
Deactivated User
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'


Mike,

I got this to work for me, fill in your dataframe name and shapefile name where indicated. Make sure map is open and a feature is selected. This zooms to selected feature.

import arcpy
mxd = arcpy.mapping.MapDocument('CURRENT')
df = arcpy.mapping.ListDataFrames(mxd,"put name of top dataframe here")[0]
Layer = (mxd, "put name of first shapefile in top dataframe here", df)[0]
df.zoomToSelectedFeatures()
arcpy.RefreshActiveView
0 Kudos
RichardFairhurst
MVP Honored 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?


Python is case sensitive and the error is indicating that you have changed the case of the mxd variable in the 3rd line, which rather than using the assignment of the variable in the second line is being interpreted as a new unassigned variable.  You have the same problem with the variable called Layer/layer.  Be consistent with your variable spelling cases.  Change the code to:

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()
0 Kudos
MikeHouts
Emerging Contributor
Mike,

I got this to work for me, fill in your dataframe name and shapefile name where indicated. Make sure map is open and a feature is selected. This zooms to selected feature.

import arcpy
mxd = arcpy.mapping.MapDocument('CURRENT')
df = arcpy.mapping.ListDataFrames(mxd,"put name of top dataframe here")[0]
Layer = (mxd, "put name of first shapefile in top dataframe here", df)[0]
df.zoomToSelectedFeatures()
arcpy.RefreshActiveView





THANKS SO MUCH.  That fixed it and your annotated script helped to.
0 Kudos
SueBreeden
Deactivated User
THANKS SO MUCH.  That fixed it and your annotated script helped to.


Glad to help. For future reference, the following code will zoom to a selected feature in any shapefile that is in the top dataframe.

import arcpy
mxd = arcpy.mapping.MapDocument('CURRENT')
df = arcpy.mapping.ListDataFrames(mxd, "put name of top dataframe here")[0]
df.zoomToSelectedFeatures()
df.scale = df.scale * 1.1
arcpy.RefreshActiveView()
0 Kudos
PaulLang
Deactivated User
This code works; zooms to but gives this error message that you have to close.

import arcpy
mxd = arcpy.mapping.MapDocument('CURRENT')
df = arcpy.mapping.ListDataFrames(mxd,"Layers")[0]
Layer = (mxd, "PUBLICGIS.GISADMIN.PARCELCAMA", df)[0]
df.zoomToSelectedFeatures()
arcpy.RefreshActiveView ()
0 Kudos
PaulLang
Deactivated User
This works!

Expression:
zoomToSelectedFeatures()

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

Many failed attempts until i saw this answer. You saved me. Thanks. 

0 Kudos
DougKnight
Emerging Contributor
I am trying to execute a "Zoom to Selected" command at the end of a tool I am building in ModelBuilder.  I have followed the advice on this thread and am almost all the way there.  The model will work as I intend and it zooms to the selected features; however, when I save the model to a tool and try to run it from the toolbox, the zoom does not work and just zooms to the full extent of the map (as if there were no features selected).  If I manually select "Zoom to Selected Features" from the "Selection" menu after the tool has run, it zooms as it should.  I am including a screenshot of the model as well as the script I am using...If anyone has any tips they would be greatly appreciated.

Cheers!


I have the EXACT same problem.  I have read on this forum that you can not use 'current' with a geoprocessing servcies.  I had my whol tool working using current inside of arcmap, but it was giving errors when I published it as a gpservice (couldn'topen mxd or something)  So I tried to give it the exact location of the mxd and the entire tool ran without errors, but it did not execute the zoom at the end of the process.  Have you figured out a solution, or does anybody know what I am doing wrong?
0 Kudos
HectorChapa
Frequent Contributor
Greetings,

I have a question. I am trying to write a stand alone script trying to select a attribute and zoom in to.
What would the code look like please can you show an example?
Also check out my snippet of code and let me know what I am doing wrong.


import arcpy

value = 112354

arcpy.env.workspace = arcpy.mapping.MapDocument("CURRENT")

arcpy.SelectLayerByAtrribute_management("H_PARCELS_120814", "NEW_SELECTION", "PROP_ID" = value)

df = arcpy.mapping.ListDataFrames(arcpy.env.workspace, "Layers")[0]
df.zoomToSelectedFeatures()
0 Kudos
SueBreeden
Deactivated User
This works for me with field names and values in the SelectLayerByAttribute_management tool. You will of course need to change the name of the fields and layer name to your own.

import arcpy
mxd = arcpy.mapping.MapDocument('CURRENT')
arcpy.SelectLayerByAttribute_management("PARC+ADDR_all","NEW_SELECTION","C_MAP = '010N' AND Parcel_Num = 6")
parcels = arcpy.mapping.ListLayers(mxd,"PARC+ADDR_all")[-1]
df = arcpy.mapping.ListDataFrames(mxd,"Layers")[0]
df.extent=parcels.getSelectedExtent()
df.zoomToSelectedFeatures()

this also worked for me using your variable instead of field names.

import arcpy
mxd = arcpy.mapping.MapDocument('CURRENT')
value = ' "Parcel_Num" = ' + str(6)
arcpy.SelectLayerByAttribute_management("PARC+ADDR_all","NEW_SELECTION",value)
parcels = arcpy.mapping.ListLayers(mxd,"PARC+ADDR_all")[-1]
df = arcpy.mapping.ListDataFrames(mxd,"Layers")[0]
df.extent=parcels.getSelectedExtent()
df.zoomToSelectedFeatures()

Hope this helps or at least points you in the right direction.
0 Kudos