AnsweredAssumed Answered

dataframe: zoom to selected feature

Question asked by roy_hewitt on Jan 17, 2012
Latest reply on Jan 19, 2012 by jbarrette-esristaff
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']

    # 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)
    print arcpy.GetMessages()
try:# Clip wetlands layer to extent of user dictated parcel, calculate staticstics
    arcpy.Clip_analysis(wetlands, "parcelLayer", shapePath + userParcel + "_Wtld")                         
    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")

    print arcpy.GetMessages()
    print "Finished Automapper."
del mxd