Select to view content in your preferred language

Loop Layers in Modelbuilder

6703
8
Jump to solution
03-22-2013 06:37 AM
NinaRihn
Frequent Contributor
Is there an easy way to loop through layers and perform an action on each one in Modelbuilder without writing a script?
0 Kudos
1 Solution

Accepted Solutions
MatthewPayne
Esri Contributor
Nina,

You can not loop through layers in the TOC of ArcMap using Model Builder because it would not have access to the data frame object as Model Builder was not designed for advanced processes.   This would need to be done using scripting.  A quick, simple example going through the Python window of ArcPad would be:


>>> from arcpy import env
>>> mxd = arcpy.mapping.MapDocument("CURRENT")
>>> layers = arcpy.mapping.ListLayers(mxd)
>>> print layers
[<map layer u'CP_Test'>, <map layer u'points'>, <map layer u'polys'>, <map layer u'testlines'>, <map layer u'points'>, <map layer u'Arizona_Cities'>, <map layer u'Arizona_Cities'>]


Matt

View solution in original post

0 Kudos
8 Replies
MatthewPayne
Esri Contributor
Hi Nina,

When you mention looping though layers, are you referring to layers in the table of contents of ArcMap or features within a geodatabase or shapefiles within a folder?  If you are referring to layers in the table of contents, are these layers in the same workspace or do they come from different workspaces?

Matt
0 Kudos
NinaRihn
Frequent Contributor
they are layers in the ArcMap TOC that are in different workspaces.
0 Kudos
MatthewPayne
Esri Contributor
Nina,

You can not loop through layers in the TOC of ArcMap using Model Builder because it would not have access to the data frame object as Model Builder was not designed for advanced processes.   This would need to be done using scripting.  A quick, simple example going through the Python window of ArcPad would be:


>>> from arcpy import env
>>> mxd = arcpy.mapping.MapDocument("CURRENT")
>>> layers = arcpy.mapping.ListLayers(mxd)
>>> print layers
[<map layer u'CP_Test'>, <map layer u'points'>, <map layer u'polys'>, <map layer u'testlines'>, <map layer u'points'>, <map layer u'Arizona_Cities'>, <map layer u'Arizona_Cities'>]


Matt
0 Kudos
MatthewPayne
Esri Contributor
Meant to type "ArcMap" and not "ArcPad" in the above post.

Matt
0 Kudos
MatthewPayne
Esri Contributor
Furthermore, the looping process with Python will be unique to your specific purpose.  In full, here is the example including a looping process that loops through and prints each layer:

>>> from arcpy import env
>>> mxd = arcpy.mapping.MapDocument("CURRENT")
>>> layers = arcpy.mapping.ListLayers(mxd)
>>> print layers
[<map layer u'CP_Test'>, <map layer u'points'>, <map layer u'polys'>, <map layer u'testlines'>, <map layer u'points'>, <map layer u'Arizona_Cities'>, <map layer u'Arizona_Cities'>]
>>> for lyr in layers:
...     print lyr
...    
CP_Test
points
polys
testlines
points
Arizona_Cities
Arizona_Cities


Matt
0 Kudos
NinaRihn
Frequent Contributor
thanks Matt!   Looks like I better take some Python classes!
0 Kudos
MatthewPayne
Esri Contributor
No problem, Nina, anytime.

You can find a lot of great examples of syntax for geoprocessing tools and arcpy functions from within our Resource Center.  Here are just a couple examples but you can use the search box to find much, much more:


arcpy.mapping.ListLayers
http://resources.arcgis.com/en/help/main/10.1/index.html#//00s30000002n000000

arcpy.mapping.MapDocument
http://resources.arcgis.com/en/help/main/10.1/index.html#//00s30000000n000000


I suggest to start with these two introduction pages below then use the navigational tree on the left side in both links to branch out into further topics:


What is Python?
http://resources.arcgis.com/en/help/main/10.1/index.html#/What_is_Python/002z00000001000000/

What is ArcPy?
http://resources.arcgis.com/en/help/main/10.1/index.html#/What_is_ArcPy/000v000000v7000000/


Matt
0 Kudos
TarunJaiswal
Esri Contributor
Greetings Nina!

In addition to all the excellent resources Matt pointed out here are some more resources on Python which might interest you:

1) Free training from Esri on Using Python in ArcGIS Desktop 10

2) Python for ArcGIS

3) A blog on 10 Easy Ways to Tame Python Scripting in ArcGIS

Happy learning.

Thanks!
0 Kudos