2

What I'm currently working on: approximately 20 historical topographic maps of a specific area spanning 70 years.

What I'd like to do: instead of saving each historical topo as a different file, I would like to just enable each layer (in this case rasters .tif) and print or export to pdf.

For example the particular text in question would say 1938 HISTORICAL TOPOGRAPHIC MAP or 1940 HISTORICAL TOPOGRAPHIC MAP which corresponds to the layer name.

I don't think Data Driven pages applies here since I'm using the same extent (please correct me if I'm wrong).

It would appear that maybe a python script is the best way to go and I found this one here by https://gis.stackexchange.com/users/1608/artwork21.

mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0] # default Layers dataframe
for lyr in arcpy.mapping.ListLayers(mxd, "*", df):
     if lyr.visible == True:
          for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):
               if elm.text == 'Text': # whatever your text element is named here
                    elm.text = lyr.name
                    break

However, it's been years since I used python and I'm not sure how to implement this code in ArcPy.

1

I use arcpy.mapping all the time to do this kind of batch map export.

To get the dynamic text working, you will need to create a text element in your map document. Then right click > Properties > Size and Position tab > Element name, and enter a name for your text element (see screenshot).

ElementName

Then, you just run the Python code, passing in that text element name in the line with arcpy.mapping.ListLayoutElements, as shown in this simple example:

import arcpy, os

#Map document stuff
CurrentMXD = arcpy.mapping.MapDocument("CURRENT")
CurrentDF = CurrentMXD.activeDataFrame

#Output map directory
Output_dir = "C:\temp"

#Iterate through layer names
LayersToExport = ["1938 HISTORICAL TOPOGRAPHIC MAP","1940 HISTORICAL TOPOGRAPHIC MAP"]
for LayerName in LayersToExport:

    #Get current layer, make visible
    Layer = arcpy.mapping.ListLayers(CurrentMXD, LayerName, CurrentDF)[0]
    Layer.visible = True

    #Change title text
    TextElement = arcpy.mapping.ListLayoutElements(CurrentMXD, "TEXT_ELEMENT","TitleText")[0]
    TextElement.text = Layer.name

    #Get output JPG filename, remove existing, export
    Output_jpg = os.path.join(Output_dir,LayerName+".jpg")
    if os.path.exists(Output_jpg): os.remove(Output_jpg)
    arcpy.mapping.ExportToJPEG(CurrentMXD, Output_jpg, resolution=300, jpeg_quality=95)

    #Make layer invisible again
    Layer.visible = False
  • ah! thats what was getting me. Is there a way you can also show me how to make it so that I can have each map update with another separate dynamic text that just uses consecutive numbers? (eg 16.2-1, 16.2-2, 16.2-3) ? – KGJ Jan 9 '15 at 20:38
  • @KGJ that should be a new question. – PolyGeo Jul 27 '17 at 20:55
0

If anyone finds this in the future I changed the code provided by dmahr slightly for PDFs instead of JPEGS. Make sure you disable all image layers that you don't want to transfer when you start the export process.

>>> import arcpy, os

#Map document stuff
CurrentMXD = arcpy.mapping.MapDocument("CURRENT")
CurrentDF = CurrentMXD.activeDataFrame

#Output map directory
Output_dir = "C:\Temp"

#Iterate through layer names
LayersToExport = ["1943 HISTORICAL TOPOGRAPHIC MAP", "1951 HISTORICAL TOPOGRAPHIC MAP", "1968 HISTORICAL TOPOGRAPHIC MAP"]
for LayerName in LayersToExport:

#Get current layer, make visible
Layer = arcpy.mapping.ListLayers(CurrentMXD, LayerName, CurrentDF)[0]
Layer.visible = True

#Change title text
TextElement = arcpy.mapping.ListLayoutElements(CurrentMXD, "TEXT_ELEMENT","TitleText")[0]
TextElement.text = Layer.name

Output_pdf = os.path.join(Output_dir,LayerName+".pdf")
if os.path.exists(Output_pdf) : os.remove(Output_pdf)
arcpy.mapping.ExportToPDF(CurrentMXD,Output_pdf)

#Make layer invisible again
Layer.visible = False

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.