dataframe: zoom to selected feature

2880
2
01-17-2012 07:12 AM
RoyHewitt
New Contributor III
Solved the problem - Selection was made on the feature layer, which was deleted.  Selection should have been made on the layer that persists throughout the script.

I've read through all of the forums relating to the zoomToSelectedFeature() method and have not had any success changing the dataframe extent in my python script.

I'm creating a script tool for less ArcMap inclined employees to enter in a Maryland Tax Parcel ID Code and Landowner name as parameters.  The tool clips the national wetlands inventory layer and zooms to the Tax parcel, finally a PDF is exported.

Everything runs as expected, but the PDF created at the end of the script doesn't represent the data frame of the selected feature.

# Import modules, setup overwrite in environments
import arcpy, os
arcpy.env.overwriteOutput = True

# User input variables
mapTitle = arcpy.GetParameterAsText(0)
userParcel = arcpy.GetParameterAsText(1)
ownerName = arcpy.GetParameterAsText(2)

# Map variables
mxd = arcpy.mapping.MapDocument("P:/Employee_GIS_Data/Hewitt_GIS_Data/Parcel Automapper/ParcelAutomapper_Debug.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]

# File Paths
parcels = ---Personal FILE PATH---"/Current_Parcel_Data"
captures = ---Personal FILE PATH---"/Capture_Locations"
wetlands = ---Personal FILE PATH---"/Delmarva_Wetlands_MDSP"

# Output Paths [removed to shorten script for forum]

# Local Variables
parcelIdField = "ACCTID"
wetlandType = "WETLAND_TY"

# Create where clause based on user input
whereClause = '"' + parcelIdField + '" = ' + "'" + userParcel + "'"

# Feature Class Lists
fcList = ['Current_Parcel_Data', 'Capture_Locations', 'Delmarva_Wetlands_MDSP']

try:
    # Select parcels based on user input, zoom to selected parcel(s)
    parcelLayer = arcpy.mapping.ListLayers(mxd, "Current_Parcel_Data", df)[1]
    arcpy.SelectLayerByAttribute_management(parcelLayer,"NEW_SELECTION", whereClause)
    df.zoomToSelectedFeature()
    arcpy.RefreshActiveView()
except:
    print arcpy.GetMessages()
   
try:# Clip wetlands layer to extent of user dictated parcel, calculate staticstics
    arcpy.Clip_analysis(wetlands, "parcelLayer", shapePath + userParcel + "_Wtld")                         
except:
    print arcpy.GetMessages()

try: # Edit text elements (arcpy.mapping module)
    mxd.title = mapTitle + ' Parcel ID: ' + userParcel # update map title
       
    # Export image as .png for sharing
    print "Exporting map to image file."
    arcpy.mapping.ExportToPNG(mxd, imagePath + ownerName + "_" + userParcel, "PAGE_LAYOUT")

except:
    print arcpy.GetMessages()
finally:
    #mxd.save()
    arcpy.RefreshActiveView()
    print "Finished Automapper."
del mxd
Tags (2)
0 Kudos
2 Replies
JT2
by
New Contributor II
First you have to select the features that you're interested in, and then you zoom to them.

In my example I've already run a definition query on my layer, which means I know it has only one feature. So it's simply a case of selecting all (in this case 1 feature) and zooming to it, like this:

  arcpy.SelectLayerByAttribute_management(sectorlayer,"NEW_SELECTION")
  df.zoomToSelectedFeatures()
  arcpy.SelectLayerByAttribute_management(sectorlayer,"CLEAR_SELECTION")
  df.scale = df.scale * 1.1


Why don't you try something like that?
0 Kudos
JeffBarrette
Esri Regular Contributor
You can also zoom the selected features in a specific layer.

  arcpy.SelectLayerByAttribute_management(sectorlayer,"NEW_SELECTION")
  df.extent = sectorlayer.getSelectedExtent()
  arcpy.SelectLayerByAttribute_management(sectorlayer,"CLEAR_SELECTION")


Jeff
0 Kudos