Select to view content in your preferred language

Turn off Base Map Error

965
1
04-26-2011 06:44 PM
ShaunWeston
Frequent Contributor
I have a script as follows:
    dataFrame = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
    for lyr in arcpy.mapping.ListLayers(mxd):
        lyr.visible = False
    for lyr in arcpy.mapping.ListLayers(mxd, "Parcel Boundary", dataFrame):
        lyr.visible = True
    for lyr in arcpy.mapping.ListLayers(mxd, "Plan Features", dataFrame):
        lyr.visible = True    
    arcpy.RefreshActiveView()
    mxd.save()

When I run this I get the below error:
Traceback (most recent call last):
  File "E:\Current Projects\Kapiti Coast District Council\Scripts\ChangeLayers.py", line 19, in <module>
    lyr.visible = False
  File "D:\ArcGIS\Desktop10.0\arcpy\arcpy\arcobjects\_base.py", line 77, in _set
    return setattr(self._arc_object, attr_name, ao)
AttributeError: LayerObject: Error in accessing Visible property
LayerObject: Error in accessing Visible property

I don't really know what this means and it works fine when I have no base maps in my map document, but whenever I use this command lyr.visible = False on a basemap I get the above error and it works fine when I set it to true.

Does anyone know why I can't turn the visibility of a base map off with python?
Tags (2)
0 Kudos
1 Reply
TrentHardy
Emerging Contributor
Not all layers can be turned off. If you are using the ESRI basemap layers, you can't even turn the actual imagery layer off in ArcMap. The arcpy documentation mentions this in the description of the layer visible property. The documentation prescribes using the supports function to check if a property is supported, but that didn't work entirely for me (might be doing something wrong). The code I use to handle this situation is below:

if (layer.supports('VISIBLE')):
    try:
        self.layer.visible = visible
    except AttributeError:
        pass
0 Kudos