arcpy.ca.SelectLayerByDateAndTime(in_layer_or_view, selection_type, time_type, {date_field}, {start_date_field}, {end_date_field}, {selection_options}, {date_selection_type}, {single_date}, {start_date}, {end_date}, {use_system_time}, {time_slice}, {start_time}, {end_time}, {days_of_week}, {months}, {years})
|
名称
|
说明
|
数据类型
|
|
in_layer_or_view
|
将应用选择的包含日期字段的数据。
|
Table View; Feature Layer
|
|
selection_type
|
指定如何应用所选内容以及如果已存在所选内容时将发生的情况。
NEW_SELECTION—生成的选择内容将替换当前选择内容。 这是默认设置。
ADD_TO_SELECTION—当存在一个选择内容时,会将生成的选择内容添加到当前选择内容中。 如果不存在选择内容,该选项的作用与新选择内容选项的作用相同。
REMOVE_FROM_SELECTION—将生成的选择内容从当前选择内容中移除。 如果不存在选择内容,该选项不起作用。
SUBSET_SELECTION—将生成的选择内容与当前选择内容进行组合。 仅两者共有的记录保持选中状态。
|
String
|
|
time_type
|
指定日期和时间字段将用于选择记录的方式。
|
String
|
|
date_field
(可选)
|
输入图层中的日期字段,将根据该字段进行选择。 仅当将 time_type 参数设置为 SINGLE_TIME_FIELD 时,此参数才处于活动状态。
|
Field
|
|
start_date_field
(可选)
|
时间范围的开始日期字段,将基于该字段进行选择。 仅当将 time_type 参数设置为 TIME_RANGE_FIELDS 时,此参数才处于活动状态。
|
Field
|
|
end_date_field
(可选)
|
时间范围的结束日期字段,将基于该字段进行选择。 仅当将 time_type 参数设置为 TIME_RANGE_FIELDS 时,此参数才处于活动状态。
|
Field
|
|
selection_options
[selection_options,...]
(可选)
|
指定日期和时间选择的方式。
|
String
|
|
date_selection_type
(可选)
|
指定是否根据日期范围、单个日期、最近时间段或比较时间段选择记录。
仅当将 selection_options 参数设置为 DATE 时,此参数才处于活动状态。
DATE_RANGE—将根据起始日期和结束日期范围选择记录。
SINGLE_DATE—将根据指定的单个日期选择记录。
RECENCY—将根据与当前日期(系统日期和时间)相关的时间段选择记录,例如,在过去的 14 天内。
COMPARATIVE—将根据与当前日期(系统日期和时间)相关的最近时间段之前的时间段选择记录。 例如,如果当前日期是 1 月 29 日,且时间段为 14 天,则将选择发生在 1 月 1 日至 1 月 14 日之间的记录。 此选项可以与后续的 RECENCY 选择配合使用,以比较两个相邻时间段(例如,1 月 1 日至 14 日和 1 月 15 日至 28 日的两个 14 天时间段)之间的记录数。
|
String
|
|
single_date
(可选)
|
要选择的单个日期和时间。
仅当将 date_selection_type 参数设置为 SINGLE_DATE 时,此参数才处于活动状态。
|
Date
|
|
start_date
(可选)
|
日期范围的开始日期。
仅当将 date_selection_type 参数设置为 DATE_RANGE 时,此参数才处于活动状态。
|
Date
|
|
end_date
(可选)
|
日期范围的结束日期。
仅当将 date_selection_type 参数设置为 DATE_RANGE 时,此参数才处于活动状态。
|
Date
|
|
use_system_time
(可选)
|
指定如果当前日期(本地系统时间)中存在于最近时间段内的记录是否将包括在选择中。
例如,指定的时间段为 14 天,当工具运行时,本地系统时间为 1 月 15 日下午 5:00,且最近时间段包括从 14 天前的下午 5:00 到工具运行当天下午 5:00 之间的所有记录,且已选择 SYSTEM_TIME。 在此示例中,14 天时间段的选择将为 2017 年 1 月 1 日下午 5:00:00 到 2017 年 1 月 15 日下午 5:00:00。在使用此示例且已选择 NO_SYSTEM_TIME 的情况下,最近时间段将使用当前日期的开始时间作为结束时间(基于本地系统时间)。 在此情况下,14 天时间段的选择将为 2017 年 1 月 1 日上午 12:00:00 到 1 月 15 日上午 12:00:00。
仅当将 date_selection_type 参数设置为 COMPARATIVE 或 RECENCY 时,此参数才处于活动状态。
|
Boolean
|
|
time_slice
(可选)
|
用于定义最近时间段的时间单位数量(分钟、小时、天、周、月或年),例如,最近 14 天内的事件。
仅当将 date_selection_type 参数设置为 COMPARATIVE 或 RECENCY 时,此参数才处于活动状态。
|
Time Unit
|
|
start_time
(可选)
|
时间范围的开始时间。
仅当将 selection_options 参数设置为 TIME 时,此参数才处于活动状态。
|
Date
|
|
end_time
(可选)
|
时间范围的结束时间。
仅当将 selection_options 参数设置为 TIME 时,此参数才处于活动状态。
|
Date
|
|
days_of_week
[days_of_week,...]
(可选)
|
指定用于选择记录的一周中第一天。
仅当将 selection_options 参数设置为 DAY_OF_WEEK 时,此参数才处于活动状态。
MONDAY—将选择发生在星期一的记录。
TUESDAY—将选择发生在星期二的记录。
WEDNESDAY—将选择发生在星期三的记录。
THURSDAY—将选择发生在星期四的记录。
FRIDAY—将选择发生在星期五的记录。
SATURDAY—将选择发生在星期六的记录。
SUNDAY—将选择发生在星期日的记录。
|
String
|
|
months
[months,...]
(可选)
|
指定用于选择记录的月份。
仅当将 selection_options 参数设置为 MONTH 时,此参数才处于活动状态。
JANUARY—将选择发生在一月的记录。
FEBRUARY—将选择发生在二月的记录。
MARCH—将选择发生在三月的记录。
APRIL—将选择发生在四月的记录。
MAY—将选择发生在五月的记录。
JUNE—将选择发生在六月的记录。
JULY—将选择发生在七月的记录。
AUGUST—将选择发生在八月的记录。
SEPTEMBER—将选择发生在九月的记录。
OCTOBER—将选择发生在十月的记录。
NOVEMBER—将选择发生在十一月的记录。
DECEMBER—将选择发生在十二月的记录。
|
String
|
|
years
[years,...]
(可选)
|
将用于选择记录的年份。
仅当将 selection_options 参数设置为 YEAR 时,此参数才处于活动状态。
|
Long
|
派生输出
|
名称
|
说明
|
数据类型
|
|
out_layer_or_view
|
已应用选择的已更新输入。
|
Table View; Feature Layer
|
|
count
|
所选记录的数量。
|
Long
|
代码示例
SelectLayerByDateAndTime 示例 1(Python 窗口)
以下脚本演示了如何在即时模式下使用 SelectLayerByDateAndTime 函数。
import arcpy
arcpy.env.workspace = r"C:/data/city_pd.gdb"
arcpy.ca.SelectLayerByDateAndTime("Crimes",
"NEW_SELECTION",
"SINGLE_TIME_FIELD",
"offendate",
None,
None,
"DATE",
"DATE_RANGE",
None,
"8/1/2018",
"8/31/2018")
SelectLayerByDateAndTime 示例 2(独立脚本)
下面的独立脚本演示了如何在脚本中使用 SelectLayerByDateAndTime 函数:
# Name: SelectLayerByDateAndTime.py
# Description: Select crimes that occurred in the past 28 days, from 9 PM to 1 PM, on the weekend, from the month of October to December,
# in the year 2018, and show the selection count. Finally, make a selection based on all the queries above.
# Import script modules
import arcpy
# Set the workspace
arcpy.env.workspace = r"C:\data\city_pd.gdb"
# Convert Feature class to table view
arcpy.management.MakeTableView("Crimes", "Crimes_View")
lyr, count = arcpy.ca.SelectLayerByDateAndTime(in_layer_or_view="Crimes_View",
selection_type="NEW_SELECTION",
time_type="SINGLE_TIME_FIELD",
date_field="offendate",
selection_options="DATE",
date_selection_type="RECENCY",
time_slice="28 Days")
arcpy.AddMessage("Crimes that occurred in past 28 days: {}".format(str(count)))
arcpy.management.SelectLayerByAttribute("Crimes_View","CLEAR_SELECTION")
lyr, count = arcpy.ca.SelectLayerByDateAndTime(in_layer_or_view="Crimes_View",
selection_type="NEW_SELECTION",
time_type="SINGLE_TIME_FIELD",
date_field="offendate",
selection_options="TIME",
start_time="9:00 PM",
end_time="1:00 AM")
arcpy.AddMessage("All crimes that occurred between 9PM and 1AM: {}".format(count))
arcpy.management.SelectLayerByAttribute("Crimes_View","CLEAR_SELECTION")
lyr, count = arcpy.ca.SelectLayerByDateAndTime(in_layer_or_view="Crimes_View",
selection_type="NEW_SELECTION",
time_type="SINGLE_TIME_FIELD",
date_field="offendate",
selection_options="DAY_OF_WEEK",
days_of_week=["SATURDAY", "SUNDAY"])
arcpy.AddMessage("All crimes that occurred on the weekend: {}".format(count))
arcpy.management.SelectLayerByAttribute("Crimes_View","CLEAR_SELECTION")
lyr, count = arcpy.ca.SelectLayerByDateAndTime(in_layer_or_view="Crimes_View",
selection_type="NEW_SELECTION",
time_type="SINGLE_TIME_FIELD",
date_field="offendate",
selection_options="MONTH",
months=["OCTOBER", "NOVEMBER", "DECEMBER"])
arcpy.AddMessage("All crimes that occurred October through December: {}".format(count))
arcpy.management.SelectLayerByAttribute("Crimes_View","CLEAR_SELECTION")
lyr, count = arcpy.ca.SelectLayerByDateAndTime(in_layer_or_view="Crimes_View",
selection_type="NEW_SELECTION",
time_type="SINGLE_TIME_FIELD",
date_field="offendate",
selection_options="YEAR",
years=[2018])
arcpy.AddMessage("All crimes that occurred in the year 2018: {}".format(count))
arcpy.management.SelectLayerByAttribute("Crimes_View","CLEAR_SELECTION")
# Combine selection options for more detailed time queries
lyr, count = arcpy.ca.SelectLayerByDateAndTime(in_layer_or_view="Crimes_View",
selection_type="NEW_SELECTION",
time_type="SINGLE_TIME_FIELD",
date_field="offendate",
selection_options=["TIME", "DAY_OF_WEEK", "MONTH","YEAR"],
start_time="9:00 PM",
end_time="1:00 AM",
days_of_week=["SATURDAY", "SUNDAY"],
months=["OCTOBER", "NOVEMBER", "DECEMBER"],
years=[2018])
message = """All crimes that occurred in the year 2018 during the months
of October through November on the weekends from 9PM to 1AM: {}
""".format(count)
arcpy.AddMessage(message)
arcpy.management.SelectLayerByAttribute("Crimes_View", "CLEAR_SELECTION")