1

I am trying to pan to a selected feature. When I run my code it does not show any errors, but when I open the mxd it does not pan to the extend of the selected feature. Here is my current code:

import arcpy, os
ws = arcpy.env.workspace = r"C:\Users\vlaw\Desktop\test\1313\figures"

for mxd in arcpy.ListFiles("*.mxd"):
    current_mxd = arcpy.mapping.MapDocument(os.path.join(ws, mxd))
    for lyr in arcpy.mapping.ListLayers(current_mxd):
        if "site" in lyr.name:

            lyr.replaceDataSource(r'C:\Users\vlaw\Desktop\test\1313\project_data\1313.gdb','FILEGDB_WORKSPACE','Site_Feature_Polygon')


    for df in arcpy.mapping.ListDataFrames(current_mxd):

        for lyr in arcpy.mapping.ListLayers(current_mxd,"",df):
            if lyr.name == "site_boundary":



               query = "'[Type]' = 'Site Boundary'"
               extent = arcpy.SelectLayerByAttribute_management(lyr,"NEW_SELECTION", query)

               df.panToExtent(lyr.getSelectedExtent(extent))
               print lyr.name
    current_mxd.save()
  • 1
    Make sure the layer name matches. Add a debug message confirming the zoom was executed. There is a dataframe.zoomToSelectedFeature() method that should simplify your code. – Ben S Nadler Jan 18 '18 at 6:01
1

There is an issue with your query: embedded quotes and square brackets for the field name are not necessary. Remove those, and wrap everything with triple double quotes. This will make sure pairs of quotes are properly coupled:

query = """Type = 'Site Boundary'"""

Since your query doesn't return any result, getting the extent of selected features and zooming to it doesn't work.

  • Surrounding by single double quotes will work too (in this instance): query = "Type = 'Site Boundary'" – PolyGeo Jan 19 '18 at 8:46
  • @PolyGeo - Sure in this case, but not if you need double quotes inside the query. Those triple double quotes are bullet-proof, that's why I really like them and have made an habit of using them. – GISGe Jan 19 '18 at 10:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.