Model Iterator for Date Ranges?

2081
6
04-07-2011 12:11 PM
JeffPickles
New Contributor III
Is there a way to programmatically go through a range of dates, select features that were present on each date, and create a shapefile with each date's features?

I have a single shapefile with a [START_DATE] and an [END_DATE] field, both formatted as Date fields.

I'm looking for a way to go through a series of dates (say, January 1 through today) and create a series of daily snapshots that will show which features were present on those dates. The kicker is that I need this to be done in a geoprocessing environment because I'd then like to turn around and create Directional Distribution polygons for each date (otherwise I could just turn on the Time Slider and be satisfied with the original SHP).

I'm trying to do this in Modelbuilder, but the available Iterators don't seem to include date range functionality.

Ideas?
0 Kudos
6 Replies
DarrenWiens2
MVP Honored Contributor
Do you want one shapefile for each date in the range?
0 Kudos
JeffPickles
New Contributor III
For this step in the process, yes.

For a final product, not necessarily. After I run the Directional Distribution for each date's SHP, I plan on appending the results into a single directional distribution SHP with a date field.

So maybe the best way of explaining it would be that I'm looking to run Directional Distribution for each date within a date range. One source point shapefile with start and end dates, one result polygon shapefile with a date field.
0 Kudos
DarrenWiens2
MVP Honored Contributor
The following python script will take an input feature class ("input"), compare each row with the dates between specified start ("start") and end ("end") dates, and output shapefiles into a folder ("outfolder").

# Import arcpy module
import arcpy, datetime

# Script arguments
input = arcpy.GetParameterAsText(0)     # feature class with date data
outfolder = arcpy.GetParameterAsText(1) # folder for your shapefiles
start = arcpy.GetParameterAsText(2)     # first date in date range
end = arcpy.GetParameterAsText(3)       # last date in date range

# Fun stuff
start = datetime.datetime.strptime(start,"%m/%d/%Y")
end = datetime.datetime.strptime(end,"%m/%d/%Y")

curdate = start

while curdate < end:
    curdate = curdate + datetime.timedelta(days=1)
    where_clause = "\"Start_Date\" < date '" + curdate.strftime("%m/%d/%Y") + "' and \"End_Date\" > date '" + curdate.strftime("%m/%d/%Y") + "'"
    arcpy.AddMessage(where_clause)
    opfile = outfolder + "/" + curdate.strftime("%m%d%Y") + ".shp"
    arcpy.Select_analysis(input, opfile, where_clause)
0 Kudos
JeffPickles
New Contributor III
Excellent. Worked like a charm. Thanks much!

Dumb question:
How do I alter this to use today's date as end?

Like so?:
# Fun stuff
start = datetime.datetime.strptime(start,"%m/%d/%Y")
end = time.strftime(end,"%m/%d/%Y")

0 Kudos
DarrenWiens2
MVP Honored Contributor
Cool. Change it to:

end = datetime.today()

...or possibly datetime.datetime.today()
0 Kudos
JeffPickles
New Contributor III
After sleeping on it, it would be better to have all of the dates in one shapefile, after all. I didn't realize the Directional Distribution tool can run seperate incarnations based on a single field. So if there were a way to put all of the individual dates into one shapefile, that would be ideal. Sorry for the run-around.
0 Kudos