Select to view content in your preferred language

enableTime() in ArcPy does not work without manually toggling in ArcGIS Pro first

575
5
Jump to solution
12-18-2023 06:22 PM
Labels (1)
Yik-ChingTsui
New Contributor III

enableTime() in ArcPy throws a RuntimeError. But if you manually go to the properties and enable the time filter in ArcGIS Pro, then disable it, enableTime() works.

  1. Open a new ArcGIS Pro project with a new map
  2. Add the layer with time data to the map
  3. Open a Python window and run the following:
aprx = arcpy.mp.ArcGISProject("CURRENT")
m = aprx.listMaps()[0]
m.listLayers()[0].enableTime()

It gives the following error:

Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\_mp.py", line 992, in enableTime
return convertArcObjectToPythonObject(self._arc_object.enableTime(*gp_fixargs((startTimeField, endTimeField, autoCalculateTimeRange, timeDimension), True)))
RuntimeError

In the Contents pane, right click the layer that was added -> Properties -> Time -> Filter layer contents based on attribute values -> OK.

The map refreshes and a time slider is visible. Go to properties and disable the time filter again.

Back in the Python window, run 

m.listLayers()[0].enableTime()

 It successfully enables the time filter with no errors.

 

Relevant documentation: https://pro.arcgis.com/en/pro-app/latest/arcpy/mapping/layertime-class.htm

 

I think this can't be a problem with the data I am using, because clearly it can filter by time when I enable it manually. Is there a step that I missed in ArcPy? Thanks

0 Kudos
1 Solution

Accepted Solutions
Yik-ChingTsui
New Contributor III

I stumbled on a solution when working on a different thing. I had to pass in the name of the datetime field to enableTime, for example:

layer.enableTime('datetime')

 

View solution in original post

0 Kudos
5 Replies
DanPatterson
MVP Esteemed Contributor

LayerTime—ArcGIS Pro | Documentation

example 1

I would think that

if lyr.supports('TIME'):
    if lyr.isTimeEnabled:

would be good checks since you have to set time properties somewhere first


... sort of retired...
0 Kudos
Yik-ChingTsui
New Contributor III

Running this

 

aprx = arcpy.mp.ArcGISProject("CURRENT")
m = aprx.listMaps()[0]
m.listLayers()[0].supports('TIME')

 

returns True, but

 

m.listLayers()[0].isTimeEnabled

 

returns False. 

 

When I manually toggle it, isTimeEnabled is still False, but enableTime() works anyway. I think isTimeEnabled simply reflects the current state, as it becomes True after enableTime(). Assuming we need to make isTimeEnabled True before using enableTime, how would I do that? Where would I "set time properties somewhere" with code?

0 Kudos
DanPatterson
MVP Esteemed Contributor

The layerTime Properties, for the most part, are read/write so presumably if you know what fields etc you intend to use, you could provide and set that information.  I can't find any examples around in Community so you might have to experiment.  Good luck


... sort of retired...
0 Kudos
Yik-ChingTsui
New Contributor III

I stumbled on a solution when working on a different thing. I had to pass in the name of the datetime field to enableTime, for example:

layer.enableTime('datetime')

 

0 Kudos
NietoLaJ
New Contributor

Thanks so much for this, saved me. What I will add is that this didn't work for me at first, but it was because my date field was not actually named what I thought it was. In the attribute table it was called "Date", but when I used python code to display the names of the field , I saw it was actually named "MSA_Date". Just thought I would add to check your field is exactly correct or this fix won't work.

0 Kudos