Export Multiple Shapefiles from TOC

6865
8
Jump to solution
01-15-2013 12:36 PM
CraigMcClain
Emerging Contributor
Hello,
I am looking for a script that will export multiple layers (usually points, lines or polygon) that are open in an mxd to shapefiles. Hopefully a little VB script I can run within Visual Basic Editor that export the top 10 layers or something to that effect. Something faster than individually right clicking on each layer and then Data -> Export Data.

I have seen the Export datasets from ArcMap TOC (updated 2.2) - ArcGIS 9.3


Looking for something that works in 10.1

Thanks in advance,
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JonathanQuinn
Esri Notable Contributor
I'm not sure how to do this through VB, but you can look into using Python using the List Layers function and the Feature Class to Feature Class tool:

import arcpy mxd = arcpy.mapping.MapDocument(r"C:\Temp\Project.mxd") df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0] count = 0 for lyr in arcpy.mapping.ListLayers(mxd, "", df):  if count < 3:   count +=1   name = lyr.name   arcpy.FeatureClassToFeatureClass_conversion(lyr, r"C:\Temp", name + ".shp") print "Finished Exporting" del mxd, df, count, name

View solution in original post

0 Kudos
8 Replies
JonathanQuinn
Esri Notable Contributor
I'm not sure how to do this through VB, but you can look into using Python using the List Layers function and the Feature Class to Feature Class tool:

import arcpy mxd = arcpy.mapping.MapDocument(r"C:\Temp\Project.mxd") df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0] count = 0 for lyr in arcpy.mapping.ListLayers(mxd, "", df):  if count < 3:   count +=1   name = lyr.name   arcpy.FeatureClassToFeatureClass_conversion(lyr, r"C:\Temp", name + ".shp") print "Finished Exporting" del mxd, df, count, name
0 Kudos
CraigMcClain
Emerging Contributor
My main goal here is to take 100s of shapefiles that are in my .mxd from many different sources) and save them into one location. at this time preferable a folder.

Thanks
0 Kudos
JonathanQuinn
Esri Notable Contributor
Right.  Update the sample I provided to point to the mxd you're looking to work with, update the data frame name if need be, update the value from the if count < 3: line to correspond to the number of layers starting from the top you want to export, and update the output location.
KeitherPerkins2
New Contributor

Thanks for this script Jonathan.

I am trying to run it with over 1,000 layers in the TOC. I simply removed the lines regarding the Count but am encountering an error that a specific object already exists. I know it is relatively simple to override this but I am not sure how.

I have 182 GDBs loaded into the TOC, all with the 7 same feature classes that I need exported in this fashion. 

I would greatly appreciate your help.

0 Kudos
CraigMcClain
Emerging Contributor
Thanks this works great.
0 Kudos
StephenFortney
New Contributor

what would a python script look like if all of the layers that needed to be exported were rasters? Would it be possible to repost the script so that it reads vertically, on multiple lines? Also, I am confused part of the code: what does the "count=0", "count +=1" and the "name = lyr.name" lines mean?

0 Kudos
JonathanQuinn
Esri Notable Contributor
import arcpy
mxd = arcpy.mapping.MapDocument(r"C:\Temp\Project.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
count = 0 
for lyr in arcpy.mapping.ListLayers(mxd, "", df):
  if count < 3:
    count +=1
    name = lyr.name
    arcpy.FeatureClassToFeatureClass_conversion(lyr, r"C:\Temp", name + ".shp")
print "Finished Exporting"
del mxd, df, count, name‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Not sure why the original comment came out in a single line, but here it is posted correctly.  First, you'd still list the layers in the data frame on line 5.  You could find which are rasters by following the Layer documentation.  The name = lyr.name is from that documentation as well, basically just getting the name of the layer from the TOC.  The original question was how to get the first x layers, so in the example, a variable is set to 0 to act as a counter, then the loop starts through all layers, adding 1 to the counter as it loops through each layer.  Once the number of layers gone through is less than 3, continue on to export the feature class.  To process rasters, switch the arcpy.FeatureClassToFeatureClass toop with the arcpy.CopyRaster tool or whatever other tool you want to use:

import arcpy, os
mxd = arcpy.mapping.MapDocument(r"C:\Temp\Project.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
for lyr in arcpy.mapping.ListLayers(mxd, "", df):
  if lyr.isRaster():
    name = lyr.name
    outRaster = os.path.join(r"C:\Temp","{0}.tiff".format(name))
    arcpy.CopyRaster_management(lyr, outRaster)
print "Finished Exporting"

So line 4 loops through the layers, line 5 checks if it's of type Raster layer, line 6 gets the name of the layer from the TOC, line 7 uses the os.path.join function to construct the path to the output raster using the format function to create the output name, and line 8 runs the tool.

StephenFortney
New Contributor

thank you! your explanation of the original script, as well as the modification of the script to work with rasters are both very helpful. I have run the script on polylines, polygons, and point layers with success.

0 Kudos