Select to view content in your preferred language

Create a Time-Enabled Layer in Arcpy

4035
4
12-24-2013 10:22 AM
JonathanMulder
Occasional Contributor
I guess I should be sipping eggnog today, but I'm getting hooked on the Python scripting...

I want to time-enable a layer (have time-enabled it in ArcMap so it is a good featureclass) in Arcpy.  Does someone have a snippet of code they could share?  Below is my start but it errors out on     "lyrTime = lyr.Time" saying  AttributeError: 'str' object has no attribute 'Time'

Jon Mulder

LayerFileLocation = "H:\Documents\GIS\HydstraData"
LayerName = "CasedContours.lyr"
lyr = os.path.join(LayerFileLocation,LayerName)
arcpy.mapping.Layer(lyr).supports("TIME")
lyrTime = lyr.Time
Tags (2)
0 Kudos
4 Replies
T__WayneWhitley
Honored Contributor
I guess I can answer this in between sipping on eggnog and wondering where the UPS guy...er, I mean Santa...is with my packages.

Your var lyr is set to a pathname string so that is your error...then the following line I guess you meant as an 'if' statement?  I guess you could use a mapping.Layer(lyr) to create your layer object...but if you are working with a layer already loaded in the map, why not use ListLayers (also part of the mapping module)?


Wayne
0 Kudos
JonathanMulder
Occasional Contributor
Now that I've got my "Labels not appearing in map document" resolved (Thanks to Wayne Whitley), I'm back to working on time-enabling two layers in my map document.  In looking through threads etc, it seems like the actual dataframe MUST be time-enabled before "time" properties can be assigned to a layer(s) residing in that dataframe.

I'm not quite sure how to enable time for the dataframe.  Is there something I need to do to the dataframe properties before manipulating the layer property?

I've tried the following line:

##Enable time for the layer.
arcpy.mapping.Layer(Wells_lyr).supports("TIME")

But I get back:

Traceback (most recent call last):
  File "H:\Documents\GIS\HydstraData\UpdateMxd20131228.py", line 36, in <module>
    arcpy.mapping.Layer(Wells_lyr).supports("TIME")
  File "c:\program files (x86)\arcgis\desktop10.2\arcpy\arcpy\arcobjects\mixins.py", line 388, in __init__
    raise TypeError(repr(type(lyrfile)))
TypeError: <class 'arcpy._mapping.Layer'>

Failed to execute (UpdateMap).
Failed at Sat Dec 28 12:50:31 2013 (Elapsed Time: 0.20 seconds)


Complete script shown below:

Jon Mulder

## Step 1
import arcpy
import arcpy.mapping
import os
OutFolderPath = "H:\Documents\GIS\HydstraData\HydstraMeasurementsDeep"
GeoDatabaseName = "GSE_Daily_Contours_20130625_20130625.gdb"
LayerFileLocation = "H:\Documents\GIS\HydstraData"

##Reference the Current map document.
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]

##arcpy.env.workspace = GeoDatabaseName
arcpy.env.workspace = "H:\Documents\GIS\HydstraData"

##Add AllPoints Featureclass to map.
WellsPtsLyrFile = os.path.join(LayerFileLocation,"Template_WellPoints.lyr")
##print Wells_lyr
arcpy.AddMessage(Wells_lyr)
Wells_lyr = arcpy.mapping.Layer(WellPtsLyrFile)
##Replace the Layer DataSource.
Wells_lyr.replaceDataSource(os.path.join(OutFolderPath,GeoDatabaseName),"NONE","WellPoints")

##Show labels for WellPoints.
if Wells_lyr.supports("LABELCLASSES"):
    Wells_lyr.labelClasses[0].expression =  "[StateWellNumber]"
    Wells_lyr.showLabels = True

##Enable time for the layer.
arcpy.mapping.Layer(Wells_lyr).supports("TIME")

arcpy.mapping.AddLayer(df, Wells_lyr, "AUTO_ARRANGE")
0 Kudos
T__WayneWhitley
Honored Contributor
Jon, I'm no expert with this, 'time-aware' layers, but yes it appears you have to both make your data frame 'time aware' in order to have associated tools available in the mxd and of course time-enabled layers loaded.

See the following, a quote of a portion from the webhelp pertaining to the DataFrameTime class:

"...a scenario in which a new time-enabled layer file is added to a new data frame that is not time aware; therefore, the time slider properties are not set within the map document.  ..."

I'd study the page at the link below - the quote above was taken from the explanation of Example 3.

DataFrameTime (arcpy.mapping)
Desktop » Geoprocessing » ArcPy » Mapping module
http://resources.arcgis.com/en/help/main/10.2/index.html#//00s300000023000000

Looks like all the examples are using the same visualization tools you'd use in the mxd (for a time-aware data frame) to export 'snapshots' in time to a set of image files (png).

Hope that helps,
Wayne

EDIT:
Also, Jon, you've repeatedly used the 'supports' statement in a way that isn't useful -- in an 'if' statement it would be useful to check before proceeding if, say, 'time' is supported, or (as in your previous working script) the line which checks to see if labels are supported (and if not, the indented block of code is ignored):

if Wells_lyr.supports("LABELCLASSES")

Likewise you could check for the layer being time-enabled, as in Example 5 from the webhelp doc on Layer--
Layer (arcpy.mapping)
Desktop » Geoprocessing » ArcPy » Mapping module
http://resources.arcgis.com/en/help/main/10.2/index.html#/Layer/00s300000008000000/

The portion I'm speaking of is below (of course your layer obj is not 'lyr', but 'Wells_lyr'):
if lyr.supports("TIME"):
    lyrTime = lyr.time
    if lyr.time.isTimeEnabled:
        ...
        ...



...some good examples here too:

LayerTime (arcpy.mapping)
Desktop » Geoprocessing » ArcPy » Mapping module
http://resources.arcgis.com/en/help/main/10.2/index.html#//00s300000063000000


Specifically, your error is with this line:
arcpy.mapping.Layer(Wells_lyr).supports("TIME")

...but Wells_lyr is already a layer object from a previous line:
Wells_lyr = arcpy.mapping.Layer(WellPtsLyrFile)

So if you just wanted to test whether time is supported, try adding this part:
if Wells_lyr.supports("TIME"):
    arcpy.AddMessage("TIME is supported.")
    if Wells_lyr.time.isTimeEnabled:
        arcpy.AddMessage("TIME is enabled.")


Enough for now - hope I haven't overloaded you.  This is a very interesting topic.
0 Kudos
JonathanMulder
Occasional Contributor
Actually, thanks to several contributors on this forum, I got my issue of time-enabled layers solved.  My code and an explanation of the process is located on another thread:

http://forums.arcgis.com/threads/99872-ApplySymbologyFromLayer_management-Not-Updating-Layer

Jon Mulder
Engineering Geologist
California Department of Water Resources
Red Bluff, CA
0 Kudos