Hello:
I want a user to select a date range which will select all features in that range based on the date field. Any advice?
Solved! Go to Solution.
It looks like your friend is working with a custom script toolbox and not a Python toolbox. There's some differences in the coding and how they work. See Comparing custom and Python toolboxes.
I am assuming you want to use arcpy.Select_analysis in your tool. This tool requires 2 parameters with an optional where clause.
Select_analysis (in_features, out_feature_class, {where_clause})
In my sample script, there are three parameters: one for the in_features and two for use in the where clause. You will need to add another parameter which will be the out_feature_class. Replace lines 47-48 with something like:
outFC = arcpy.Parameter(
displayName = "Feature to be saved",
name = "outFC",
datatype = "DEFeatureClass",
parameterType = "Required",
direction = "Output")
return [ feature, startDate, endDate, outFC ]
The execute block also will need changes. Replace lines 65 to the end with something like:
def execute(self, parameters, messages):
"""The source code of the tool."""
feature = parameters[0].valueAsText
startDate = parameters[1].value.strftime("%Y-%m-%d %H:%M:%S")
endDate = parameters[2].value.strftime("%Y-%m-%d %H:%M:%S")
outFC = parameters[3].valueAsText
where = "DateTime BETWEEN DATE '{}' AND DATE '{}'".format(startDate, endDate)
# messages.addMessage('Where: {}'.format(where))
arcpy.Select_analysis(feature, outFC, where)
for i in range(arcpy.GetMessageCount()):
messages.addMessage(arcpy.GetMessage(i))
return
You will need to watch the indentation as you edit your code. Lines 14-15 ( for i in range.... GetMessage ) will print the output of the Select_analysis tool, so you can see the parameters that are passed to it and any status messages it produces.
As Joe Borgione pointed out in his comment will be the formatting of the time/date. You may need to investigate the SQL reference for your geodatabase. I am assuming that it is a file geodatabase (.gdb). Field names can be put inside double quotes, but this appears to be optional. Date/time strings go inside single quotes, and the DATE in front of the string might also be optional. Again, this depends on your geodatabase.
As you finalize your project, you may want to add some code in the updateParameters section for validating your parameters.