<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Breaking Out a Time Enabled Layer into sub-layers organized by Month Year in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/breaking-out-a-time-enabled-layer-into-sub-layers/m-p/436489#M34336</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thought I'd post some code I just wrote.&amp;nbsp; Situation is I have a master index feature class showing footprints of imagery collects over the last 8 years. I'm going to publish these as a pdf document, but many of the collects are over the same areas, so I wanted to break out the collects by month and year, so that in the table of contents end-users could turn layers on and off without being overwhelmed by all of the polygons. I had a time field in the feature class and so i figured why should i have to go through and manually create a layer for each month. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The following code takes an existing layer, in my case I named it as 'defaultLayer' in the arcmap TOC, makes a copy, sets the definition query, and then moves it to the group folder for the year.&amp;nbsp; All of the year calcs are based on the time field. I created the group year folders manually. See the attached image and code below. Note, it is a simple script, there is no error catching. so just use it as inspiration for what you need to get done. It is intended to be run from within ArcMap. When looking at the code. the time field is 'Date_Completed_AsDate'.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;George&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;
from datetime import date
import calendar

def jump_by_month(start_date, end_date, month_step=1):
&amp;nbsp;&amp;nbsp;&amp;nbsp; current_date = start_date
&amp;nbsp;&amp;nbsp;&amp;nbsp; while current_date &amp;lt; end_date:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; yield current_date
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; carry, new_month = divmod(current_date.month - 1 + month_step, 12)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; new_month += 1
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; current_date = current_date.replace(year=current_date.year + carry, month=new_month)

mxd = arcpy.mapping.MapDocument("Current")
mmf = arcpy.mapping.ListDataFrames(mxd,"*Main*")[0]
defaultLayer = arcpy.mapping.ListLayers(mxd,"defaultLayer",mmf)[0]
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
for layerMonth in jump_by_month(defaultLayer.time.startTime, defaultLayer.time.endTime):&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Make a copy of the the layer by adding the layer back to the top, then get the new reference to it
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.mapping.AddLayer(mmf,defaultLayer,"TOP")
&amp;nbsp;&amp;nbsp;&amp;nbsp; addLayer = arcpy.mapping.ListLayers(mxd,"defaultLayer",mmf)[0]

&amp;nbsp;&amp;nbsp;&amp;nbsp; # Change the Name of the Layer
&amp;nbsp;&amp;nbsp;&amp;nbsp; addLayer.name = layerMonth.strftime("%b %Y")
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Setup the Definition Query
&amp;nbsp;&amp;nbsp;&amp;nbsp; begMonth = layerMonth
&amp;nbsp;&amp;nbsp;&amp;nbsp; endMonth = layerMonth + datetime.timedelta(calendar.monthrange(begMonth.year,begMonth.month)[1]) - datetime.timedelta(0,1)
&amp;nbsp;&amp;nbsp;&amp;nbsp; print "begMonth: " + begMonth.strftime("%Y-%m-%d %H:%M:%S") + "\t endMonth : " + endMonth.strftime("%Y-%m-%d %H:%M:%S")
&amp;nbsp;&amp;nbsp;&amp;nbsp; defQuery = """ "Date_Completed_AsDate" &amp;gt;= date ' """ + begMonth.strftime("%Y-%m-%d %H:%M:%S") + """' AND&amp;nbsp; "Date_Completed_AsDate" &amp;lt;= date '""" + endMonth.strftime("%Y-%m-%d %H:%M:%S") + "'"
&amp;nbsp;&amp;nbsp;&amp;nbsp; addLayer.definitionQuery = defQuery

&amp;nbsp;&amp;nbsp;&amp;nbsp; # Add it to the Group Layer&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; targetGroupLayer = arcpy.mapping.ListLayers(mxd, layerMonth.year ,mmf)[0]
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.mapping.AddLayerToGroup(mmf, targetGroupLayer, addLayer, "BOTTOM")

&amp;nbsp;&amp;nbsp;&amp;nbsp; # Remove the copy made at the top
&amp;nbsp;&amp;nbsp;&amp;nbsp; removeLayer = arcpy.mapping.ListLayers(mxd,layerMonth.strftime("%b %Y"),mmf)[0]
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.mapping.RemoveLayer(mmf, removeLayer)
&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;[ATTACH=CONFIG]21558[/ATTACH]&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 07 Feb 2013 14:58:45 GMT</pubDate>
    <dc:creator>GeorgeNewbury</dc:creator>
    <dc:date>2013-02-07T14:58:45Z</dc:date>
    <item>
      <title>Breaking Out a Time Enabled Layer into sub-layers organized by Month Year</title>
      <link>https://community.esri.com/t5/python-questions/breaking-out-a-time-enabled-layer-into-sub-layers/m-p/436489#M34336</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thought I'd post some code I just wrote.&amp;nbsp; Situation is I have a master index feature class showing footprints of imagery collects over the last 8 years. I'm going to publish these as a pdf document, but many of the collects are over the same areas, so I wanted to break out the collects by month and year, so that in the table of contents end-users could turn layers on and off without being overwhelmed by all of the polygons. I had a time field in the feature class and so i figured why should i have to go through and manually create a layer for each month. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The following code takes an existing layer, in my case I named it as 'defaultLayer' in the arcmap TOC, makes a copy, sets the definition query, and then moves it to the group folder for the year.&amp;nbsp; All of the year calcs are based on the time field. I created the group year folders manually. See the attached image and code below. Note, it is a simple script, there is no error catching. so just use it as inspiration for what you need to get done. It is intended to be run from within ArcMap. When looking at the code. the time field is 'Date_Completed_AsDate'.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;George&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;
from datetime import date
import calendar

def jump_by_month(start_date, end_date, month_step=1):
&amp;nbsp;&amp;nbsp;&amp;nbsp; current_date = start_date
&amp;nbsp;&amp;nbsp;&amp;nbsp; while current_date &amp;lt; end_date:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; yield current_date
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; carry, new_month = divmod(current_date.month - 1 + month_step, 12)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; new_month += 1
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; current_date = current_date.replace(year=current_date.year + carry, month=new_month)

mxd = arcpy.mapping.MapDocument("Current")
mmf = arcpy.mapping.ListDataFrames(mxd,"*Main*")[0]
defaultLayer = arcpy.mapping.ListLayers(mxd,"defaultLayer",mmf)[0]
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
for layerMonth in jump_by_month(defaultLayer.time.startTime, defaultLayer.time.endTime):&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Make a copy of the the layer by adding the layer back to the top, then get the new reference to it
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.mapping.AddLayer(mmf,defaultLayer,"TOP")
&amp;nbsp;&amp;nbsp;&amp;nbsp; addLayer = arcpy.mapping.ListLayers(mxd,"defaultLayer",mmf)[0]

&amp;nbsp;&amp;nbsp;&amp;nbsp; # Change the Name of the Layer
&amp;nbsp;&amp;nbsp;&amp;nbsp; addLayer.name = layerMonth.strftime("%b %Y")
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Setup the Definition Query
&amp;nbsp;&amp;nbsp;&amp;nbsp; begMonth = layerMonth
&amp;nbsp;&amp;nbsp;&amp;nbsp; endMonth = layerMonth + datetime.timedelta(calendar.monthrange(begMonth.year,begMonth.month)[1]) - datetime.timedelta(0,1)
&amp;nbsp;&amp;nbsp;&amp;nbsp; print "begMonth: " + begMonth.strftime("%Y-%m-%d %H:%M:%S") + "\t endMonth : " + endMonth.strftime("%Y-%m-%d %H:%M:%S")
&amp;nbsp;&amp;nbsp;&amp;nbsp; defQuery = """ "Date_Completed_AsDate" &amp;gt;= date ' """ + begMonth.strftime("%Y-%m-%d %H:%M:%S") + """' AND&amp;nbsp; "Date_Completed_AsDate" &amp;lt;= date '""" + endMonth.strftime("%Y-%m-%d %H:%M:%S") + "'"
&amp;nbsp;&amp;nbsp;&amp;nbsp; addLayer.definitionQuery = defQuery

&amp;nbsp;&amp;nbsp;&amp;nbsp; # Add it to the Group Layer&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; targetGroupLayer = arcpy.mapping.ListLayers(mxd, layerMonth.year ,mmf)[0]
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.mapping.AddLayerToGroup(mmf, targetGroupLayer, addLayer, "BOTTOM")

&amp;nbsp;&amp;nbsp;&amp;nbsp; # Remove the copy made at the top
&amp;nbsp;&amp;nbsp;&amp;nbsp; removeLayer = arcpy.mapping.ListLayers(mxd,layerMonth.strftime("%b %Y"),mmf)[0]
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.mapping.RemoveLayer(mmf, removeLayer)
&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;[ATTACH=CONFIG]21558[/ATTACH]&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 07 Feb 2013 14:58:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/breaking-out-a-time-enabled-layer-into-sub-layers/m-p/436489#M34336</guid>
      <dc:creator>GeorgeNewbury</dc:creator>
      <dc:date>2013-02-07T14:58:45Z</dc:date>
    </item>
  </channel>
</rss>

