Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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

share|improve this question
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 '12 at 15:46
    
No, the script runs fine, it's just that the output is not what I'm expecting. –  Jason Oct 23 '12 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 '12 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. –  Jason Oct 23 '12 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 '12 at 17:58

1 Answer 1

up vote 2 down vote accepted

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.

share|improve this answer
    
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) ) ) –  Jason Oct 23 '12 at 18:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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