Select to view content in your preferred language

problem with python code exporting to png with time enabled layer

692
1
03-08-2011 05:07 AM
ChadKeith
Emerging Contributor
Hi-
I'm using the ESRI sample code (modified below) to export a mxd to a series of .PNG files.  I'm having problems with the dynamic text associated with the layers.  In arcmap I can use the time slider and the text appears correctly - changing by 5 years for each .PNG.  But when I export it using the python code it changes the dynamic time text as  the time series advances - I think it may have something to do with leap years but I'm not sure.  In the .PNG files I'm displaying dynamic text ("time.date" format = "MMM yyyy") and what happens as the time series advances is that at first it shows correctly as Jan 1961 to Jan 1966, Jan 1966 to Jan 1971, Jan 1971 to Jan 1976 but then to Dec 1975 to Dec 1980 - should be Jan 1976 to Jan 1981.
 
I've tried hard coding the py.script using datetime.timedelta(days=1825) but I get similar problems.

I just don't understand why it works in ArcMap but not when exporting.  Any help or thoughts would be greatly appreciated. 

here is the code.
import arcpy, datetime
mxd = arcpy.mapping.MapDocument(r"C:\GIS_data\mxds\ytf.mxd")
df = arcpy.mapping.ListDataFrames(mxd)[0]
df.time.currentTime = datetime.datetime(1961,01,01)
while df.time.currentTime <= df.time.endTime + df.time.timeStepInterval:
    #An example str(newTime) would be: "2008-12-29 02:19:59"
    #The following line splits the string at the first - and takes the first
    #item in the resulting string. 
    fileName = "YF2_" + str(df.time.currentTime).split("-")[0] + ".png"
    arcpy.mapping.ExportToPNG(mxd, r"C:\GIS_data\media\ytf\\" + fileName)
    df.time.currentTime = df.time.currentTime + df.time.timeStepInterval
    print fileName
del mxd
Tags (2)
0 Kudos
1 Reply
ChadKeith
Emerging Contributor
I was able to figure it out.  I'm almost positive it was a leap year problem so
I hard coded a time interval for 5 years using 1826.25 days (365.25 * 5).  Now the
dynamic text updates correctly.

import arcpy, datetime
mxd = arcpy.mapping.MapDocument(r"C:\GIS_data\mxds\ytf.mxd")
df = arcpy.mapping.ListDataFrames(mxd)[0]
df.time.currentTime = datetime.datetime(1961,01,01)
endTime = datetime.datetime(2010,01,01)
interval  = datetime.timedelta(days=1826.25)
#added "+ df.time.timeStepInterval" to the end of the while statement because
#the final year group 2010 was getting cut off
while df.time.currentTime <= df.time.endTime + interval:
    #An example str(newTime) would be: "2008-12-29 02:19:59"
    #The following line splits the string at the space and takes the first 
    #item in the resulting string.  
    fileName = "ytf_fall_" + str(df.time.currentTime).split("-")[0] + ".png"
    arcpy.mapping.ExportToPNG(mxd, r"C:\GIS_data\media\ytf\\" + fileName)
    df.time.currentTime = df.time.currentTime + interval
    print fileName
del mxd
0 Kudos