Select to view content in your preferred language

Layers On/Off

4553
14
10-25-2011 08:49 AM
MathieuCain
Occasional Contributor
Hello,

I have a map which I am publishing to be viewed in ArcReader.

I have 2 layer groups which I do not want visible at the same time (i.e., both can be turned off, or one or the other can be turned one, but not both on at the same time).

Is there a way to do this?

Thanks for your thoughts.
0 Kudos
14 Replies
JeffBarrette
Esri Regular Contributor
Tracey,

A couple of comments:
- I don't see an mxd.save() in your code so how is it that you expect to see changes?
- If you are running this from the Python window, use "CURRENT" instead of the path to the file on disk.
- it would be much easier to read your code if you posted it withing
 tags.  Its the # button.  You want to preserve indents.


Jeff
0 Kudos
TraceyKing
New Contributor
Tracey,

A couple of comments:
- I don't see an mxd.save() in your code so how is it that you expect to see changes?
- If you are running this from the Python window, use "CURRENT" instead of the path to the file on disk.
- it would be much easier to read your code if you posted it withing
 tags.  Its the # button.  You want to preserve indents.

Jeff


Hi Jeff
Thanks alot for your help.  That was my first post and I didn't know how to include code, so thanks for the heads up.  My code works now, here is a copy.  I had the save a copy in my code, what I showed here was just a snippet, the real one is longer.  I don't know what was wrong but I will continue with this code and add the rest in gradually to determine where my error was.  Thanks again!
# Import system modules


import arcpy, sys, os, traceback, datetime
from arcpy.mapping import *

# set the current workspace (in the case a folder)
arcpy.env.workspace = "E:\MapProject\MapProject.gdb"

# Define Variables   
datapath = "J:\MapProject\MapProject.gdb\\"
mappath =  "J:\\MapProject\\Maps"
newmxd= mappath + os.sep + "outReportMap.mxd"

try:   
   
    # Get the map document. In this case a custom template set up for map production
    mxd = MapDocument(mappath + os.sep + "ReportMapsTemplate9.mxd")

    print "Getting Map Document properties..."
   
    # Report properties of the Map Document
   
    print "Map Document Title: " + str(mxd.title)
    print "Map Document Author: " + str(mxd.author)
    mxd.author = "Tracey A. King"
    print "Map Document Author is now: " + str(mxd.author)
    print mxd.filePath
    print mxd.pageSize
   
    # Get a list of data frames
    # [0] indicates the first data frame
    # The data frame names is "Map Frame" 
    df = ListDataFrames(mxd, "Map Frame")[0]

    print "Getting Data Frame properties..."

    # Report properties of the data frame named "Map Frame"
  
    print 'Data Frame Name: ' + str(df.name)
    print "Map Frame Map Units: " + str(df.mapUnits)
    print "Map Frame Scale: " + str(df.scale)         

    # Get a list of layers in the table of contents of the map document
    TOCLayers = ListLayers(mxd)
       
    print "Processing layout elements..."

    # Loop through the layers 
       
    for TOCLayer in ListLayers(mxd):
        print 'Layer Name: ' + str(TOCLayer.name)
        print 'Longname: ' + str(TOCLayer.longName)

    # 3. Set initial layer visibility               
        if TOCLayer.name=="SSLT_Boundary":
            TOCLayer.visible=True
        if TOCLayer.name=="SSLT_Easements":
            TOCLayer.visible=True
        if TOCLayer.name=="NatGeo_World_Map":
            TOCLayer.visible=True

    # 4. Save to a new map document

    # Check to see if map document already exists, if it does delete it
    if arcpy.Exists(newmxd):
        arcpy.Delete_management(newmxd)

    # Save the changes in a new map document
    mxd.saveACopy(newmxd)
    print "A copy of the map document has been saved"

except:
    # http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//002z0000000q000000
   
    tb = sys.exc_info()[2]
    tbinfo = traceback.format_tb(tb)[0]
    pymsg = "PYTHON ERRORS:\nTraceback Info:\n" + tbinfo + "\nError Info:\n     " +        str(sys.exc_type) + ": " + str(sys.exc_value) + "\n"
    msgs = "ARCPY ERRORS:\n" + arcpy.GetMessages(2) + "\n"

    arcpy.AddError(msgs)
    arcpy.AddError(pymsg)

    print msgs
    print pymsg
   
    arcpy.AddMessage(arcpy.GetMessages(1))
    print arcpy.GetMessages(1)


0 Kudos
SusanWalden
Deactivated User
I think the following script will do what you need.  Make sure to save the MXD with ALL group layers checked off (and all layers within the group layer checked on).  See attached screen shot. A field was added to the index layer called "GroupLayerName".  It has the values "GroupLayer1, GroupLayer2, etc).  After exporting to PDF, the script then turns the group layer back off again before going to the next DDP page.


import arcpy
mxd = arcpy.mapping.MapDocument(r"c:\Temp\test.mxd")
ddp = mxd.dataDrivenPages

for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):
  mxd.dataDrivenPages.currentPageID = pageNum
  fieldValue = mxd.dataDrivenPages.pageRow.GroupLayerName
  for lyr in arcpy.mapping.ListLayers(mxd):
    if lyr.name == fieldValue:
      lyr.visible = True
      arcpy.mapping.ExportToPDF(mxd, r"C:\\Temp\\" + fieldValue + ".pdf")
      lyr.visible = False
del mxd



Hi Jeff,
I'm using the same code you provided above, but my code only exports the first page as a pdf. I do have a different field name in the index layer, it's called GroupLayer without the Name as in yours.

I'm wondering if it has anything to do with my index layer which is 14 polygons on top of each other. I did that so I could export a different aerial image with each page. My group layers are called GroupLayerr1, GroupLayer2, etc and the page names in the index layer are the same.

Is it confused because every polygon inthe index layer is exactly the same?
Can I export a map, not using DDP, cycling through group layers for each export?
Thanks,
Susan
0 Kudos
JeffBarrette
Esri Regular Contributor
Susan,

I don't know why this is not working for you but I have never tried duplicate index features like you are.  This is definately a creative way to use DDP but I don't think this is the correct approach.  You are trying to create a thematic map - extent remains the same but different layers or themes are displayed for each page.

The approach I would take is to not use DDP and use arcpy.mapping only.  Lets say you have 14 different pages because there are 14 different images.  To makes things really easy, I would order the images in the TOC the way I want them to be exported.  Image 1 / Page 1 at the top, image 14 / page 14 at the bottom of the TOC.

Then I would do something like:

import arcpy
mxd = arcpy.mapping.MapDocument(someMXDpath)
for lyr in arcpy.mapping.ListLayers(mxd):
  lyr.visible = False
for lyr in arcpy.mapping.ListLayers(mxd):
  lyr.visble = True
  arcpy.mapping.ExportToPDF(mxd, somePDFpath)
  lyr.visble = False


The code obviously needs more logic if there are multiple data frames, sets of layers that are always visible, etc

Jeff
0 Kudos
SusanWalden
Deactivated User
Susan,

I don't know why this is not working for you but I have never tried duplicate index features like you are.  This is definately a creative way to use DDP but I don't think this is the correct approach.  You are trying to create a thematic map - extent remains the same but different layers or themes are displayed for each page.

The approach I would take is to not use DDP and use arcpy.mapping only.  Lets say you have 14 different pages because there are 14 different images.  To makes things really easy, I would order the images in the TOC the way I want them to be exported.  Image 1 / Page 1 at the top, image 14 / page 14 at the bottom of the TOC.

Then I would do something like:

import arcpy
mxd = arcpy.mapping.MapDocument(someMXDpath)
for lyr in arcpy.mapping.ListLayers(mxd):
  lyr.visible = False
for lyr in arcpy.mapping.ListLayers(mxd):
  lyr.visble = True
  arcpy.mapping.ExportToPDF(mxd, somePDFpath)
  lyr.visble = False


The code obviously needs more logic if there are multiple data frames, sets of layers that are always visible, etc

Jeff


Thanks Jeff, I thought as much. I'll try your approach and see how it works. Python is not simple for me...struggling with it for a while now, maybe I'll have a breakthrough!
0 Kudos