Zoom To Selected Features not working.

3215
1
10-23-2013 07:36 AM
ChristopherOos
New Contributor
I'm trying to create a map that allows a user to input information about a location including lat and long through a geoprocessing tool along with a unique tasking number. I want to then select that point and zoom to it. I'm having 2 problems. The first is when I run my code through the geotool my make feature layer does not appear in the TOC however if I run it in the python window that layer does show up. My second problem is that the Zoom To Selected Feature is not working. It seems to be selecting (if I open the table the correct row is highlighted and the point is symbolized as having been selected) but the active view is not shifting to the point the same way it would if I used the menus in ArcMap. My initial research said that the Select Layer By Attribute only worked on a feature layer so I know why that might be an issue when running through the geoprocessingtool but not sure why it wouldn't work when I run it in the python window which shows the creation of the feature layer.   So to clarify my questions:  is there a reason that I can???t see the Feature Layer I create (even when I refresh the TOC) when I run my script through the geoprocessing tool?  Also, why isn???t the Zoom to Selected Feature not working when my Feature Layer is created and showing in the TOC?  Thanks in advance.
#Make Feature Layer from Feature Class
arcpy.MakeFeatureLayer_management("Facility_Location","Facility_Lyr")
arcpy.RefreshTOC

#Select the inputed MAS Number
Expression = "MASNumber = "+"'"+ MASNum +"'"
arcpy.SelectLayerByAttribute_management("Facility_Lyr", "NEW_SELECTION", Expression)

#Zoom to selected attribute (MAS Number)
df=arcpy.mapping.ListDataFrames(mxd)[0]
df.zoomToSelectedFeatures()
arcpy.RefreshActiveView ()
Tags (2)
0 Kudos
1 Reply
RichardFairhurst
MVP Honored Contributor
Adding layers only automatically occurs while you are working directly in the Desktop environment, which includes the Python Window.  In script tools you explicitly have to add layers to a map.  Look at arcpy.mapping.AddLayer, arcpy.mapping.InsertLayer, and arcpy.mapping.Layer to deal with the map TOC.  Once the TOC is handled the zoom function should work.  If you are just selecting one point you may want to look at panToExtent.  Assuming you have a Layer called lyr with a selection this will pan the dataframe:

df.panToExtent(lyr.getSelectedExtent())

Edit: MakeFeatureLayer_management is only for the geoprocessor and converts a feature class on disk to a layer in memory that the geoprocessor can use.  Models and scripts use this tool frequently to make feature class outputs into layers so that other geoprocesses that only accept layer inputs will work.  It is not used to alter maps, otherwise almost every model would alter your maps.
0 Kudos