0

I need help using the arcpy mapping module to export multiple layers (with the same name) in my table of contents to new shapefiles with unique names. Here's a screenshot of my TOC:

enter image description here

I want to export each SGravityMain layer and append the last character in the group layer name (a b or c) to the new shapefile. I am stuck.

Here is my code:

import os, arcpy

folder = "path\to\folder"

#Get name of vector layers in the TOC
mxd = arcpy.mapping.MapDocument("current")
df = arcpy.mapping.ListDataFrames(mxd)[0]
layers = [f.name for f in arcpy.mapping.ListLayers(mxd, "*", df) if f.isFeatureLayer]


for layer in layers:
    #Join output folder to layer name and append .shp
    shp_out = os.path.join(folder, "{}.shp".format(layer))
    arcpy.CopyFeatures_management(layer, shp_out)

This is just a sub set of data and I actually have around 300 group layers in my MXD.

Can someone please help?

2
  • What happens when you run that code snippet? Please edit your question to include details about what it does/doesn't do, and any errors you encounter. – Midavalo Mar 6 '17 at 17:59
  • I think your problem is that you are using arcpy in a map document and will not be able to get the path basename in list layers. I think you need to do a search in the main folder C:\users\smw outside without arcpy.mapping (use os) reference a list of common layer names (i.e. SGravityMain) and have the script look for these names in each subfolder and export any files with these names to your new location while appending the basename to the output. – geoJshaun Mar 6 '17 at 18:17
1

You could add a _1, _2 etc. to the output filename.

I've added a check that the layer name is SGravityMain.

i = 1
for layer in layers:
    if layer == 'SGravityMain':
        #Join output folder to layer name and append .shp
        shp_out = os.path.join(folder, "{0}_{1}.shp".format(layer, i))
        arcpy.CopyFeatures_management(layer, shp_out)
        i += 1

This should output your layers using the last character of the group layer name as a suffix on the shapefile name.

#Get name of vector layers in the TOC
mxd = arcpy.mapping.MapDocument("current")
df = arcpy.mapping.ListDataFrames(mxd)[0]
layers = [f for f in arcpy.mapping.ListLayers(mxd, "*", df) if f.isFeatureLayer]

for layer in layers:
    if layer.name == 'SGravityMain':
        subName = layer.longName.split("\\")[-2]
        #Join output folder to layer name and append .shp
        shp_out = os.path.join(folder, "{0}_{1}.shp".format(layer.name, subName))
        arcpy.CopyFeatures_management(layer, shp_out)
3
  • ah just saw your note about adding the group-layer name. I'll update my code soon – Midavalo Mar 6 '17 at 18:03
  • Thats ok. I just needed it to be unique. THANK YOU VERY MUCH! – smw Mar 6 '17 at 18:10
  • 1
    @smw I have updated my answer – Midavalo Mar 6 '17 at 18:15

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.