2

I know model builder will not iterate through the layers in an MXD, which is kind of fundamentally stupid if you ask me, but I still need to do exactly that. I am not a strong coder but i understand Python is how to handle this.

I have seen other questions addressing this, but they seem over-my-head in complexity (i have taken an 'intro to python' university course and did well in it, plus did all the ESRI tutorials, still doesn't help understand how this works). I am hoping someone here can post some simple cut-n-paste python I can use to accomplish this.

I need to iterate through the ToC and export each layer as a shapefile to a specific folder. The folder is always the same, but the ToC will change depending on the application, so I have different MXD's for each subset of layers that need to be exported, which is why i want to script/model this as then i can open any MXD and run a tool to get my outputs.

  • 2
    Please include any arcpy code you've attempted (edit the question and add your code) – Midavalo Feb 13 '16 at 0:03
9

To answer your specific question "How to iterate through layers of an MXD?"

mxd = arcpy.mapping.MapDocument("CURRENT")  # Uses your currently open MXD
df = arcpy.mapping.ListDataFrames(mxd, '')[0] # Chooses the first dataframe
for lyr in arcpy.mapping.ListLayers(mxd, '', df): # Loop through layers
    # Any tools you want to run on each layer go here

Something like this should work - just gets a list of all layers in your current map and outputs them to a single folder as shapefiles.

import arcpy

mxd = arcpy.mapping.MapDocument("CURRENT")  # Uses your currently open MXD
df = arcpy.mapping.ListDataFrames(mxd, '')[0] # Chooses the first dataframe

destPath = r"N:/VisualStudio/Projects/GISSE/Output" # Set your destination folder path

for lyr in arcpy.mapping.ListLayers(mxd, '', df): # Loop through layers
    # Output layer to shapefile
    arcpy.FeatureClassToFeatureClass_conversion(lyr, destPath, "{}.shp".format(lyr), "", "", "") 
  • Could be easily modified to allow you to specify the MXD or to change the output folder as tool parameters – Midavalo Feb 13 '16 at 0:20
  • Thanks for the code snippet, trying to make it work but get an error that the layer is not a feature layer, so wondering how that is set within the script? – user25644 Feb 16 '16 at 19:35
  • Try using if lyr.isFeatureLayer == True: before the arcpy.FeatureClassToFeatureClass. See Layer ArcGIS Desktop Help – Midavalo Feb 16 '16 at 19:43
  • Still getting errors, likely syntax? I have tried a few variants of where to put the IF statement before the output but no luck. Currently have this: destPath = r"V:\iPads\Town GIS data\TEST" # Set your destination folder path for lyr in arcpy.mapping.ListLayers(mxd, '', df): # Loop through layers # Output layer to shapefile if lyr.isFeatureLayer == True: arcpy.FeatureClassToFeatureClass_conversion(lyr, destPath, "{}.shp".format(lyr), "", "", "") – user25644 Feb 16 '16 at 19:55
  • another detail which could be adding to the confusion; the layers in my MXD are in groups. I could take them out of the group to have a flat structure to the ToC without too much detriment though if that's what is keeping this from working. – user25644 Feb 16 '16 at 20:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.