In this recipe, you will learn how to time-enable a layer and data frame. You will then write a script that cycles through the time range for the layer and exports a PDF map showing crimes through time in seven-day intervals.
The DataFrameTime
object provides access to time management operations for time-enabled layers in a data frame. This object is returned when you reference the DataFrame.time
property, and includes properties for retrieving the current time, end time, start time, time step interval, and others that are established by using the Time Slider Options dialog box and then saved with the map document. One or more layers in a data frame must be time-enabled for this functionality to be operational.
Follow these steps to learn how to work with time-enabled layers:
c:ArcpyBookCh2Crime_Ch2.mxd
with ArcMap.Crime
is the active data frame.Enable time for the layer by clicking on the Enable time on this layer checkbox.
Define the Layer Time Extent: by clicking the Calculate button, circled in the following screenshot:
arcpy.mapping
module:import arcpy.mapping as mapping
Crime_Ch2.mxd
), and assign the reference to a variable:mxd = mapping.MapDocument("CURRENT")
Crime
data frame:df = mapping.ListDataFrames(mxd, "Crime")[0]
DataFrameTime
object:dft = df.time
DataFrameTime.currentTime
property to the DataFrameTime.startTime
property:dft.currentTime = dft.startTime
while
loop that will loop through the time while the currentTime
is less than or equal to the endTime
:while dft.currentTime <= dft.endTime:
currentTime
property. The entire while
loop should appear as follows:while dft.currentTime <= dft.endTime: fileName = str(dft.currentTime).split(" ")[0] + ".pdf" mapping.ExportToPDF(mxd,os.path.join(r"C:ArcpyBookCh2", fileName), df) print("Exported " + fileName) dft.currentTime = dft.currentTime + dft.timeStepInterval
c:ArcpyBookcodeCh2TimeEnabledLayers.py
to verify the accuracy of your code:The
DataFrameTime
object provides access to time management operations in a data frame. Several properties of DataFrameTime
, including currentTime
, startTime
, endTime
, and timeStepInterval
, are used in this recipe. Initially, we set the currentTime
property equal to the startTime
property. The initial startTime
was calculated when you set the Time Step Interval: properties in ArcMap. The while
loop was set up to loop as long as the currentTime
property is greater than the endTime
property. Inside the loop, we created a fileName
variable that is set to the currentTime
property, plus an extension of .pdf
. We then called the ExportToPDF()
function, passing in a path and the filename. This should ideally export the page layout view to the PDF file. Finally, we updated the currentTime
property by the timeStepInterval
property that was set to 7.0 days in in the Time Step Interval: properties dialog.