Select to view content in your preferred language

Python data driven turn off layer and modify scale bar

2786
3
Jump to solution
10-31-2012 11:18 AM
AlexGole1
Emerging Contributor
Hi all,
I am looking for a script that would turn off a specific layer for certain data driven pages (for instance: turn-off layer page 25 and 29).

I wrote a quick script that does not work (I would like to get help with)

import arcpy, os
#Specify map doc, data frame and table
mxd = arcpy.mapping.MapDocument(r"K:\Projects_1\Caltrans\00543_09_Willits_from_URS\mapdoc\Crossection_2012\Crossection_20121024.mxd")
df = arcpy.mapping.ListDataFrames(mxd)[0]
mapLyr = arcpy.mapping.ListLayers(mxd, "Mapbook_table", df)[0]
#Map book
for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):
mxd.dataDrivenPages.currentPageID = pageNum
    rows = arcpy.SearchCursor(mapLyr)
    for row in rows:
  source = row.getValue ("Source")
  if source == "Willits":
   lyrName = "On-Site Mitigation Area_Python"
   lyr = arcpy.mapping.ListLayers(mxd, lyrName, df)[0]
   lyr.visible = True
                        arcpy.mapping.ExportToPDF(mxd, r"K:\\Working\\Alex_Gole\\" + Try + ".pdf")
   lyr.visible = False

del lyr
del mxd, df


Since each data driven has its own dependent scale, I would also like to incorporate in this script some code that would modify the scale bar  (for instance: if scale is below 1:150, scale bar is 0 to 50, or if scale is 1:1000, scale bar is 1 to 100). Could anybody help with this,
Thanks,
Alex
0 Kudos
1 Solution

Accepted Solutions
JeffBarrette
Esri Regular Contributor
After working with Alex's data, we were able to make the above code much more efficient.

import arcpy, os #Specify map doc, data frame and visible layer mxd = arcpy.mapping.MapDocument(r"C:\Temp\Alex\Crossection_201210311a.mxd") df = arcpy.mapping.ListDataFrames(mxd, "Main")[0] visLyr = arcpy.mapping.ListLayers(mxd, "On-Site Mitigation Area_Python", df)[0]  #Reference DDP object and iterate through pages ddp = mxd.dataDrivenPages for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):   print pageNum   mxd.dataDrivenPages.currentPageID = pageNum   visLyr.visible = False   if ddp.pageRow.Source == "Willits":     visLyr.visible = True   ddp.exportToPDF(r"C:\Temp\Alex\Output\Page" + str(ddp.pageRow.Sorting_Field) + ".pdf", "CURRENT") del mxd


Jeff

View solution in original post

0 Kudos
3 Replies
JeffBarrette
Esri Regular Contributor
Your code looks pretty good.  My immediate comment is that you are referencing an MXD on disk (i.e., you are not using "CURRENT") but your code doesn't do anything like save out a new MXD or export to PDF so you are not going to see the changes.  I've added code below that includes the exportToPDF statement and also shows proper indentation (in case that is an issue too).

import arcpy, os
#Specify map doc, data frame and table
mxd = arcpy.mapping.MapDocument(r"K:\Projects_1\Caltrans\00543_09_Willits_from_URS\mapdoc\Crossection_2012\Crossection_20121024.mxd")
df = arcpy.mapping.ListDataFrames(mxd)[0]
mapLyr = arcpy.mapping.ListLayers(mxd, "Mapbook_table", df)[0]

for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):
  mxd.dataDrivenPages.currentPageID = pageNum
  rows = arcpy.SearchCursor(mapLyr)
  for row in rows:
    source = row.getValue ("Source")
    if source == "Willits":
      lyrName = "On-Site Mitigation Area_Python"
      lyr = arcpy.mapping.ListLayers(mxd, lyrName, df)[0]
      lyr.visible = True  #This is turning the layer on based on if statement.
      ddp.exportToPDF(r"C:\Temp\Page" + str(pageNum) + ".pdf", "CURRENT")
      lyr.visible = False #This turns it back off for other maps.   
del mxd, df, lyr


The above code will create individual PDF files: Page1.pdf, Page2.pdf, etc

Concerning your scale bars.  This is a bit more trick but very doable.  I would author the MXD with two scale bars (for each of the appropriate scale ranges), each with unique element names and one of them positioned just off the page.  Then via arcpy.mapping I would move them on/off the page depending on the current page using .elementPositionX and/or .elementPositionY.

I hope this helps,
Jeff
0 Kudos
JeffBarrette
Esri Regular Contributor
After working with Alex's data, we were able to make the above code much more efficient.

import arcpy, os #Specify map doc, data frame and visible layer mxd = arcpy.mapping.MapDocument(r"C:\Temp\Alex\Crossection_201210311a.mxd") df = arcpy.mapping.ListDataFrames(mxd, "Main")[0] visLyr = arcpy.mapping.ListLayers(mxd, "On-Site Mitigation Area_Python", df)[0]  #Reference DDP object and iterate through pages ddp = mxd.dataDrivenPages for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):   print pageNum   mxd.dataDrivenPages.currentPageID = pageNum   visLyr.visible = False   if ddp.pageRow.Source == "Willits":     visLyr.visible = True   ddp.exportToPDF(r"C:\Temp\Alex\Output\Page" + str(ddp.pageRow.Sorting_Field) + ".pdf", "CURRENT") del mxd


Jeff
0 Kudos
AlexGole1
Emerging Contributor
Great job Jeff!
Thanks for all your help and hard work.
Alex
0 Kudos