Help - exporting data frame (not page layout) to PNG via arcpy

1354
3
Jump to solution
08-14-2013 05:19 PM
AnthonyCheesman1
Occasional Contributor II
Good morning all

I'm trying to pull together some code to export the data view (not the page layout view) to PNG. The issue I'm having is that when I use the df variable in the ExportToPNG function, I am getting an Assertion Error. If I substitute df for "PAGE_LAYOUT" the script runs, but the output is not what I'm looking for.

Any ideas how to solve this?

This is what I've got so far:

import arcpy import os from arcpy import env from time import localtime, strftime  # Set workspace  arcpy.env.workspace = r'd:\gis\aerialtracking\output' ws = arcpy.env.workspace if str(os.path.exists(ws)) == "False":     os.makedirs(ws)  # Set time variable and print output variable  printtime = strftime("%Y%m%d_%H%M", localtime()) print printtime pngname = "mapexport_" + printtime + ".png" print pngname  # Set mxd  mxd = arcpy.mapping.MapDocument("CURRENT") df = arcpy.mapping.ListDataFrames(mxd)  #Export PNG  arcpy.mapping.ExportToPNG(mxd, pngname, df, df_export_width = 1600, df_export_height = 1200, world_file = True)  #delete variables  del df, ws, printtime, pngname, mxd


Edit: I didn't actually get this above code to run, but I got similar code running thru the Python window in ArcMap.

>>> arcpy.mapping.ExportToPNG(mxd, r'D:\gis\aerialtracking\output\test.png', "PAGE_LAYOUT") >>> arcpy.mapping.ExportToPNG(mxd, r'D:\gis\aerialtracking\output\test.png', df) Runtime error <type 'exceptions.AssertionError'>:
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
RhettZufelt
MVP Notable Contributor
import arcpy
import os
from arcpy import env
from time import localtime, strftime

# Set workspace

arcpy.env.workspace = r'd:\gis\aerialtracking\output'
ws = arcpy.env.workspace
if str(os.path.exists(ws)) == "False":
    os.makedirs(ws)

# Set time variable and print output variable

printtime = strftime("%Y%m%d_%H%M", localtime())
print printtime
pngname = "mapexport_" + printtime + ".png"
print pngname

# Set mxd

mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
#Export PNG

arcpy.mapping.ExportToPNG(mxd, pngname, df, df_export_width = 1600, df_export_height = 1200, world_file = True)

#delete variables

del df, ws, printtime, pngname, mxd


Suspect the issue is that you are sending a list of dataframes to the export command rather than just one df as ListDataFrames always produces a list, even if only one item in it. the above code will use the first dataframe in the list (index [0]).

See if that gets it working,

R_

View solution in original post

0 Kudos
3 Replies
RhettZufelt
MVP Notable Contributor
import arcpy
import os
from arcpy import env
from time import localtime, strftime

# Set workspace

arcpy.env.workspace = r'd:\gis\aerialtracking\output'
ws = arcpy.env.workspace
if str(os.path.exists(ws)) == "False":
    os.makedirs(ws)

# Set time variable and print output variable

printtime = strftime("%Y%m%d_%H%M", localtime())
print printtime
pngname = "mapexport_" + printtime + ".png"
print pngname

# Set mxd

mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
#Export PNG

arcpy.mapping.ExportToPNG(mxd, pngname, df, df_export_width = 1600, df_export_height = 1200, world_file = True)

#delete variables

del df, ws, printtime, pngname, mxd


Suspect the issue is that you are sending a list of dataframes to the export command rather than just one df as ListDataFrames always produces a list, even if only one item in it. the above code will use the first dataframe in the list (index [0]).

See if that gets it working,

R_
0 Kudos
AnthonyCheesman1
Occasional Contributor II
Spot on! Thank you - that has worked well.

Kicking myself as I saw that in the example code but stupidly thought it was a comment, therefore didn't include it.
0 Kudos
RhettZufelt
MVP Notable Contributor
Glad it worked for you.

Also, I see you gave me a point with the up arrow, would you might selecting the checkbox as answered also?

Will give me credit for it and also mark it answered for people searching similar issues in the future.

Thanks,

R_
0 Kudos