Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

enable time layer arcpy

import arcpy, os

# Setup output location and overwrite status
arcpy.env.overwriteOutput = True
path = r"C:ProjectsTimeShips"
output_GDB = os.path.join(path, "Ships.gdb")

# Get time information from a layer in a layer file    
lyrFile = arcpy.mp.LayerFile(os.path.join(path, "ShipPositions.lyrx"))
lyr = lyrFile.listLayers()[0]
lyrTime = lyr.time

# Set the time for which you want to select features in the time-enabled layer
fromTimeSelection = datetime.datetime(1776, 1, 1)
toTimeSelection = datetime.datetime(1777, 1, 1)

# Get the start and end time of the time enabled layer and its time field
startTime = lyrTime.startTime
endTime = lyrTime.endTime
timeField = lyrTime.startTimeField

# Check to see if the time for which you want to select features lies within the start and end time of the time enabled layer
if (fromTimeSelection < startTime or toTimeSelection > endTime):
    print("The time specified for selecting features is not within the time extent of the layer")
else:
    # Formulate the time query
    timeQuery = f"{timeField} >= timestamp '{fromTimeSelection}' And 
                  {timeField} < timestamp '{toTimeSelection}'"
    # Process: Feature Class to Feature Class
    arcpy.FeatureClassToFeatureClass_conversion(lyr, output_GDB, "Ships_1776", timeQuery, "", "")

# Add the new layer to a map and enable time
p = arcpy.mp.ArcGISProject(os.path.join(path, "ships.aprx"))
lyt = p.listLayouts('Ships')[0]
mf = lyt.listElements('MAPFRAME_ELEMENT', 'NoTimeMF')[0]
m = mf.map
m.addDataFromPath(os.path.join(output_GDB, "Ships_1776"))
l = m.listLayers("Ships_1776")[0]
l.enableTime()
mt = mf.time
mt.isTimeEnabled = True

# Save a copy of the project
p.saveACopy(os.path.join(path, "ships2.aprx"))
Comment

PREVIOUS NEXT
Code Example
Python :: how to kill a script if error is hit python 
Python :: hover show all Y values in Bokeh 
Python :: list length in python 
Python :: python how to play mp3 file 
Python :: write in entry() in tkinter 
Python :: np reshape 
Python :: take first 10 row while reading csv python 
Python :: string acharacters count in python without using len 
Python :: python list of whole numbers 
Python :: update xls file using python 
Python :: selenium python find element by class name with space 
Python :: how to change values of dictionary in python 
Python :: how to return a value from a function in python 
Python :: python 4 
Python :: Python message popup 
Python :: labelencoder update 
Python :: transformers bert 
Python :: Reverse an string Using Stack in Python 
Python :: what is kernel_initializer 
Python :: python lambda function if else 
Python :: how to set gpu python 
Python :: dicttoxml python? 
Python :: change data type python 
Python :: time in python code 
Python :: get filename from path python 
Python :: python index method 
Python :: def factorial python 
Python :: how to read linux environment variable in python 
Python :: python assert is datetime 
Python :: Change Python interpreter in pycharm 
ADD CONTENT
Topic
Content
Source link
Name
9+4 =