LayerTime
汇总
LayerTime 对象提供有关如何在已启用时间功能的图层或表中存储和配置时间的信息。
讨论
包含时间值的字段(例如开始和结束时间、时间步长间隔等)不仅可以用于获取启用时间功能的图层或表的时间属性知识,还可以用于进一步的数据管理和分析任务。 您还可以使用时间信息来确保选择表中的要素或行的指定时间在开始和结束时间范围内。
如果对象包含时间信息,可以使用 Layer 或 Table 类上的 enableTime 方法来启用该对象的时间。 在尝试访问 LayerTime 属性之前,需要测试 isTimeEnabled 属性是否返回 True 。
属性
| 名称 | 说明 | 数据类型 |
|---|---|---|
|
daylightSavings (读取和写入) |
指示启用时间的图层或表中时间字段内的时间值,在收集时是否遵循了输入时区的夏令时规则。 |
Boolean |
|
endTime (读取和写入) |
启用时间的图层或表的结束日期和时间。 |
DateTime |
|
endTimeField (读取和写入) |
包含结束时间值的字段名称。 并非所有图层或表都使用结束时间字段。 如果每个要素或行都有一个单一的时间字段,则仅将使用 |
String |
|
startTime (读取和写入) |
启用时间的图层或表的开始日期和时间。 |
DateTime |
|
startTimeField (读取和写入) |
包含开始时间值的字段名称。 如果每个要素或行都有一个单一的时间字段,则仅将使用 |
String |
|
timeDimension (读取和写入) |
使用 netCDF 数据时包含时间值的维度的名称。 |
String |
|
timeFormat (读取和写入) |
|
String |
|
timeOffset (读取和写入) |
应用于数据中的时间值的时间偏移。 |
Double |
|
timeOffsetUnits (读取和写入) |
描述了应用于数据中的时间值的
|
String |
|
timeStepInterval (读取和写入) |
时间步长间隔定义时态数据的间隔长度。 时间步长间隔可以认为是时间值在数据中记录的频率。 |
Double |
|
timeStepIntervalUnits (读取和写入) |
描述了应用于数据中的时间值的
|
String |
|
timeZone (读取和写入) |
启用时间的图层或表的时区。 提示:有关有效时区字符串的列表,请参阅 |
String |
|
timeZoneIANA (读取和写入) |
启用时间的图层或表的 IANA 时区。 提示:有关有效 IANA 时区字符串的列表,请参阅 |
String |
代码示例
以下脚本可以测试图层文件中的图层是否支持时间以及是否已设置时间属性。 然后,此脚本使用时间信息(开始时间和结束时间)计算已启用时间的图层的时间范围。
import arcpy
lyrFile = arcpy.mp.LayerFile(r'C:\Projects\Time\ShipPositions.lyrx')
for lyr in lyrFile.listLayers():
if lyr.supports('TIME'):
if lyr.isTimeEnabled:
lyrTime = lyr.time
startTime = lyrTime.startTime
endTime = lyrTime.endTime
timeDelta = endTime - startTime
print(f"Layer: {lyr.name}")
print(f" Start Time: {str(startTime.strftime('%m-%d-%Y'))}")
print(f" End Time: {str(endTime.strftime('%m-%d-%Y'))}")
print(f" Time Extent: {str(timeDelta.days)} days")
else:
print("No time properties have been set on the layer")
else:
print("Time is not supported on this layer")
以下脚本根据在特定时间有效的输入要素创建要素类,同时确保选择时间在已启用时间的图层的时间范围内(开始时间和结束时间)。
import arcpy, os
# Setup output location and overwrite status
arcpy.env.overwriteOutput = True
path = r"C:\Projects\Time\Ships"
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.conversion.FeatureClassToFeatureClass(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"))