0

I have a piece of code I'm using to replace layer sources and then zooms to the new features. The part where I replace the workplace source works, but I can't seem to get it to zoom to the extent of the new features. I'm running ArcMap 10.2.

import arcpy
mxd = arcpy.mapping.MapDocument('CURRENT')
mxd.findAndReplaceWorkspacePaths(r"\\Client\L$\17TSLB002_Morice\A95440_1_FULT_21\Submission_GDB\A95440_1_TBA_Digital_Sub_Dec11_2017.gdb", 
                                r"\\Client\L$\17TSLB002_Morice\A95441_1_NO_6\Submission_GDB\A95441_1_TBA_Digital_Sub_Dec1_2017.gdb")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")
lyr = arcpy.mapping.ListLayers(mxd, "BLOCK", df)
df.extent = lyr.getSelectedExtent()
df.scale = 5000
arcpy.RefreshActiveView()
mxd.saveACopy(r"\\Client\L$\17TSLB002_Morice\A95441_1_NO_6\Submission_PDFs\A95441_1_CHR_11by17_Portrait.mxd")
del mxd

I get this error though when I run the script.

Runtime error 
Traceback (most recent call last):
  File "<string>", line 6, in <module>
  File "e:\sw_nt\arcgis\desktop10.2\arcpy\arcpy\utils.py", line 181, in fn_
    return fn(*args, **kw)
  File "e:\sw_nt\arcgis\desktop10.2\arcpy\arcpy\mapping.py", line 1500, in ListLayers
    result = mixins.MapDocumentMixin(map_document_or_layer).listLayers(wildcard, data_frame)
  File "e:\sw_nt\arcgis\desktop10.2\arcpy\arcpy\arcobjects\mixins.py", line 820, in listLayers
    raise TypeError(str(type(dataframe)))
TypeError: <type 'list'>
  • 2
    It's always helpful to include the error--or at least mention that there was an error--in the initial post. Otherwise, we're chasing a moving target. – Tom Dec 13 '17 at 18:01
2

You do not have the layer rather a list of all layers. You need to loop through the list of layers and find the layer you want to zoom to, try:

df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
for lyr in arcpy.mapping.ListLayers(mxd, "BLOCK", df): # the "BLOCK" parameter is just wildcard filter for the list but does not return a single layer
    if lyr.name == "layerNameYouWantToZoomTo":
        df.extent = lyr.getSelectedExtent()
        # continue on with logic
3

Your df variable is a list of data frames. You'll need to grab one--almost certainly the first one. So change the line

df = arcpy.mapping.ListDataFrames(mxd, "Layers")

to

df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]

And, as artwork21 mentioned, you'll need to do the same thing when you ListLayers. However, the current error is from the df line.

You may also need to refresh the map in order to reflect the updates. So, after df.scale = 5000, add the line arcpy.RefreshActiveView().

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.