4

I'm using the arcpy mapping module to create a series of images and am having trouble exporting a map document to PNG in layout view; I can only get the script to export the data frame view. I've seen this post on setting the activeView attribute of the MapDocument object, but it hasn't worked for me. It wouldn't matter to me which view was exported except that I'd like to have a legend to identify each unique image.

Even more maddening is that I've added different text elements to both the data frame view and the layout view so that I can verify which is being exported. Even with the map.activeView = 'PAGE_LAYOUT', the exported image has the data frame view text element (see below). However, when I print the text of the element using print text_elm.text, it properly prints the layout view text.

Can anyone see anything wrong with my code? Thanks!

import os,arcpy
imagesDir = r'--\images'
gridsDir = r'--\grids'
mxd = r'--\map\template.mxd'
img_height_px = 800
img_width_px = 600
img_res_dpi = 300

arcpy.env.workspace = gridsDir

# add difference raster to mxd and export as png
map = arcpy.mapping.MapDocument( mxd )
map.activeView = 'PAGE_LAYOUT'
#arcpy.RefreshActiveView()

df = arcpy.mapping.ListDataFrames(map)[0]
for r in arcpy.ListRasters():
    year = r.split('_')[1]
    mo = r.split('_')[2]

    # automatically add new layers to legend
    legend = arcpy.mapping.ListLayoutElements(map, 'LEGEND_ELEMENT')[0]
    legend.autoAdd = True  # newly added layers will be automatically added to the legend
    legend.elementHeight = 4
    legend.elementWidth = 4

    # add layer
    print 'Exporting layer:', os.path.join( imagesDir,'dif-%s-%s.png' % (year,mo) )
    new_lyr = arcpy.mapping.Layer( os.path.join(gridsDir,r) )
    arcpy.mapping.AddLayer(df, new_lyr, 'BOTTOM')

    # update symbology of new layer using source layer in the map
    temp_lyr = arcpy.mapping.ListLayers(map,'temp_lyr',df)[0]       # find template layer
    temp_lyr.visible = False                                        # hide template layer
    arcpy.mapping.UpdateLayer(df,new_lyr,temp_lyr,True)             # update symbology of new layer using template layer

    # export map layout view
    arcpy.mapping.ExportToPNG(map, os.path.join( imagesDir,'dif-%s-%s.png' % (year,mo) ),df )

del map
print 'operation complete.'

Expected output: enter image description here

Actual output: exported .png

Text element printed:

printed text element

6
  • 1
    You may want to add any errors you have been receiving and/or where in the process it crashes. Or does it run with no errors, but you don't get any results?
    – Baltok
    Oct 23, 2012 at 15:46
  • No, the script runs fine, it's just that the output is not what I'm expecting. Oct 23, 2012 at 15:47
  • I managed to get this map to export/print properly, the only difference that I can see is that I used 'CURRENT' instead of indicating a path to the mxd. I didn't make reference to map.activeView at all...
    – Roy
    Oct 23, 2012 at 15:57
  • 1
    @Roy, I'm not running the script from within ArcMap so I'm not sure that I can use the string 'CURRENT' to create the MapDocument object. Oct 23, 2012 at 16:10
  • 1
    If you are running from outside the session, then you can not use 'CURRENT'. If you are using script from outside always make sure to checkout your licence for Arc. It'll save you some time in future :) Despite difference in scale your program runs as expected. You do export df which is defined as df = arcpy.mapping.ListDataFrames(map)[0], which is exactly DataFrame with out MapSurrounding element and other graphic, which make up Page Layout. Try "PAGE_LAYOUT" instead of df.
    – Tomek
    Oct 23, 2012 at 17:58

1 Answer 1

2

As per comment above, when you are running from outside the session, then you can not use 'CURRENT'. If you are using script from outside always make sure to checkout your licence for Arc. It'll save you some time in future :) Despite difference in scale your program runs as expected. You do export df which is defined as df = arcpy.mapping.ListDataFrames(map)[0], which is exactly DataFrame with out MapSurrounding element and other graphic, which make up Page Layout. Try "PAGE_LAYOUT" instead of df.

1
  • Removed my DataFrameObject, df, from the export function my script and it now exports as layout view: arcpy.mapping.ExportToPNG(map, os.path.join( imagesDir,'dif-%s-%s.png' % (year,mo) ) ) Oct 23, 2012 at 18:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.