setTimeInclusion
汇总
讨论
MapTime 类允许您使用许多可用的时间设置来过滤地图中的要素,这些时间设置对应于 时间 选项卡上的控件和 ArcGIS Pro 的布局中已激活的地图框上的 时间滑块 。 更改地图时间属性仅适用于与 布局 上的 MapFrame 关联的地图,当前不适用于地图视图。
currentStartTime 和 currentEndTime 属性用于指定时间跨度。 timeInclusion 属性可控制时间跨度中是否包含要素。 该属性使用 setTimeInclusion 方法进行设置。
提示:
currentStartTime 属性不能大于 currentEndTime 属性。 相反, currentEndTime 属性不能小于 currentStartTime 属性。 如果您要更改为新的未来时间跨度,则最好在设置 currentStartTime 之前设置 currentEndTime 。
timeStep 方法可用于计算新的 currentStartTime 和 currentEndTime 值。 若要创建时间地图序列,请使用 timeStep 方法来迭代时间值。 有关代码示例,请参阅下方的示例 3。
可以在地图框的地图上新添加的图层上启用时间,从而使您可以执行时间分析、创建时态地图册等。 有关代码示例,请参阅下面的示例 2 和 3。
语法
setTimeInclusion()
属性
| 名称 | 说明 | 数据类型 |
|---|---|---|
|
currentTimeEnd (读取和写入) |
地图框的当前时间跨度的当前结束日期和时间。 |
DateTime |
|
currentTimeSpan (只读) |
当前时间跨度,表示地图框的时间跨度的开始时间和结束时间之间的差异。 |
Double |
|
currentTimeSpanUnits (只读) |
表示
|
String |
|
currentTimeStart (读取和写入) |
地图框的时间跨度的当前开始日期和时间。 |
DateTime |
|
isTimeEnabled (读取和写入) |
用于确定是否为地图框启用时间的布尔值。 |
Boolean |
|
timeInclusion (只读) |
可控制当前时间跨度中是否包含
|
String |
|
timeStepInterval (只读) |
指示开始时间和结束时间将在每个时间步长上偏移的时间。 |
Double |
|
timeStepIntervalUnits (只读) |
用于
|
String |
方法
setTimeInclusion(time_inclusion)
setTimeInclusion 方法控制将 currentTimeStart 和 currentTimeEnd 包括在当前时间跨度内的方式。
| 名称 | 说明 | 数据类型 |
|---|---|---|
|
time_inclusion |
A string that specifies a valid time inclusion value.
|
String |
timeStep(reference_time, interval, interval_units)
timeStep 方法基于给定的参考时间和时间间隔返回新时间。
| 名称 | 说明 | 数据类型 |
|---|---|---|
|
reference_time |
A Python 提示:The |
DateTime |
|
interval |
A double that defines a time interval. 提示:Positive or negative intervals can be used. |
Double |
|
interval_units |
A string that specifies a valid interval unit.
|
String |
返回值
| 数据类型 | 说明 |
|---|---|
|
DateTime |
根据 |
代码示例
以下脚本为地图框设置了初始开始时间和结束时间,然后在一年中以一个月为时间间隔进行迭代。 每个月都会导出到 PDF 文件。
import arcpy
import os
path = r"C:\Projects\Time\Ships"
#Reference a project, layout, and a map frame
p = arcpy.mp.ArcGISProject(os.path.join(path, "ships.aprx"))
lyt = p.listLayouts('ships')[0]
mf = lyt.listElements('MAPFRAME_ELEMENT', 'ShipsWithTimeMF')[0]
if not mf.time is None:
#Get the map frame time, set the initial time window and inclusion settings
t = mf.time
t.currentTimeStart = datetime.datetime(1776, 1, 1, 0, 0)
t.currentTimeEnd = datetime.datetime(1776, 2, 1, 0, 0)
t.setTimeInclusion('INCLUDE_ONLY_START')
for x in range(1,13):
#Export to PDF
outPDF = os.path.join(path, "Output", f"1776-{x}.pdf")
lyt.exportToPDF(outPDF)
#Change the time window to the next 1 month interval
t.currentTimeStart = t.timeStep(t.currentTimeStart, 1, "months")
t.currentTimeEnd = t.timeStep(t.currentTimeEnd, 1, "months")
以下脚本将时间感知图层从图层文件添加到地图框的地图。 在此工作流中,地图框不是时间感知型地图框。 然后,脚本将缩放到已添加图层的范围。 通过将 isTimeEnabled 设置为 True 以在地图框上启用时间。 最后,脚本通过设置 currentStartTime 、 currentEndTime 和 setTimeInclusion 来定义初始时间跨度。
import arcpy
import os
path = r"C:\Projects\Time\Ships"
#Reference a project, layout, and a map frame
p = arcpy.mp.ArcGISProject(os.path.join(path, "ships.aprx"))
lyt = p.listLayouts('Ships')[0]
mf = lyt.listElements('MAPFRAME_ELEMENT', 'NoTimeMF')[0]
#Reference the map frame's map and add a time enabled layer from a layerfile
m = mf.map
m.addDataFromPath(os.path.join(path, "ShipPositions.lyrx"))
#Zoom to the layer
l = m.listLayers()[0]
mf.camera.setExtent(mf.getLayerExtent(l))
#Enable time on the map and set its time window
mt = mf.time
mt.isTimeEnabled = True
mt.currentTimeEnd = datetime.datetime(1900, 1, 1, 0, 0)
mt.currentTimeStart = datetime.datetime(1800, 1, 1, 0, 0)
mt.setTimeInclusion('INCLUDE_ONLY_START')
#Export the layout
lyt.exportToPDF(os.path.join(path, output, "ShipPositions_1800s.pdf"))
以下脚本将时间感知图层从图层文件添加到地图框的地图。 然后,将在图层和地图上启用时间。 将使用 timeStep 方法在一系列时间跨度中进行迭代。 每个时间跨度都将导出到 PDF 文件。 要创建完整的地图册体验, PDFDocument 类将用于为临时地图册添加标题页和尾页。
import arcpy, os
basedir = r"C:\Project\Time"
# Create the output pdf file
pdfPath = os.path.join(basedir, "output", "HistoricalBoundaries.pdf")
pdfDoc = arcpy.mp.PDFDocumentCreate(pdfPath)
# Reference project, layout, map frame and map
aprx = arcpy.mp.ArcGISProject(os.path.join(basedir, "Tokyo.aprx"))
layout = aprx.listLayouts('Historical Boundaries')[0]
mfTokyo = layout.listElements('MAPFRAME_ELEMENT', 'Tokyo Map Frame')[0]
mTokyo = mfTokyo.map
# Add a layer that contains time values to the map
lyrTokyo = mTokyo.addLayer(arcpy.mp.LayerFile(os.path.join(basedir, 'Tokyo_Historic_Boundaries.lyrx')), 'TOP')[0]
# Enable time on the newly added layer
lyrTokyo.enableTime(startTimeField='Year', endTimeField='YearEnd')
# Enable Map Time
mtTokyo = mfTokyo.time
mtTokyo.isTimeEnabled = True
# Access Map Time properties for use in the MapTime.timeStep() method
interval = mtTokyo.currentTimeSpan
unit = mtTokyo.currentTimeSpanUnits
# Use the MapTime.timeStep() method to get the first time span
start = lyrTokyo.time.startTime
end = mtTokyo.timeStep(start, interval, unit)
# Loop through time spans
while end < lyrTokyo.time.endTime:
# Update the map frame with the time span
mtTokyo.currentTimeStart = start
mtTokyo.currentTimeEnd = end
# Export the time span to pdf
output_pdf_file = os.path.join(basedir, "output", f"HistoricalBoundaries_{start.year}_to_{end.year}.pdf")
layout.exportToPDF(os.path.join(basedir, "output", output_pdf_file))
# Appened the time step pdf to the output pdf
pdfDoc.appendPages(output_pdf_file)
# Use the MapTime.timeStep() method to proceed to the next time span
start = end
end = mtTokyo.timeStep(end, interval, unit)
# Add title and end pages to create a complete map book
pdfDoc.insertPages(os.path.join(basedir, 'Title Page.pdf'), 1)
pdfDoc.appendPages(os.path.join(basedir, 'End Page.pdf'))
# Save the output pdf
pdfDoc.saveAndClose()