3

I have got lots of different layers in the Table of Content panel in ArcMap. I want to export them all in shp format in one go. How can I do that? So far I have been doing it one by one by right clicking on the layer-->data-->export as shp. and it is taking longer time. I want to do them all together.

  • Even shape file has this capability that stores different types of shape(polygon,point,line,... all in same files) but ESRI doesn't use it,so you have to have at least one shape file for each geometry type of layers in TOC.Including different attribute sets of several layers is another challenge but if you let it slide and save only geometry field in each shape file,it's possible to do it by programming. – Reza Aug 9 '14 at 6:19
  • Sorry Reza if I misunderstood you, My problem is not storing different feature types in a same file. My problem is how can I export all the layers in shp format (could be point/line/polygon- however in my case all the selected layers are point feature type with X,Y coordinate) in one go? – user26161 Aug 9 '14 at 6:51
  • For this I believe you will need to find/write a Python/ArcPy script. There is one here but it converts raster layers to images so you would need to modify it to do vector layers to shapefiles. – PolyGeo Aug 9 '14 at 7:34
5

This could easily be converted into a toolbox script or run directly from the python window in Arcmap. It can be improved by validating the layer name before copying, duplicate naming conventions, drilling into group layers, using different data frames, etc.

But this should get you started for a simple TOC:

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)
  • 1
    What a great argument for eschewing .NET/ArcObjects in favor of Python! Which code will you be able to read and use five years from now! – Curtis Price Aug 9 '14 at 17:37
3

Sure,I understood the problem and only mentioned some complications in general case.But for a simple solution it is possible to write an ArcMap solution(sorry I don't know much about Python/ArcPy so I use C#) which only implements a command button and this button can be dragged into one ArcMap toolbar from ArcMap/Tools/Options.

In the Click event of this button this code opens shape file,start editing for it and then create a feature for all features in all layers which has point type geometries

public void OnClick()
{
   IWorkSpaceFactory ipShapeWF= new ShapefileWorkspaceFactoryClass();
   IWorkSpace ipShapeWS=ipShapeWF.OpenFromFile(@"C:\",0);
   IFeatureClass ipShapeFC=ipWS.OpenFeatureClass("MyShapeFileNameWithoutExtention");

   IWorkspaceEdit ipWE=(IWorkspaceEdit)ipShapeWS;
   ipWE.StartEditing(true);
   ipWE.StartEditOperation();

   UID uid = new UIDClass();
   uid.Value = "{E156D7E5-22AF-11D3-9F99-00C04F6BC78E}";
   //Suppose ipDoc is IMxDocument which has been initialized in OnCreate event
   IEnumLayer ipLayers = ipDoc.FocusMap.get_Layers(uid, true);
   Marshal.ReleaseComObject(uid);
   ipLayers.Reset();
   ILayer ipL;
   while ((ipL = ipLayers.Next()) != null)
   {
       if (!(ipL is IFeatureLayer))
                    continue;
       IFeatureLayer ipFL = ipL as IFeatureLayer;
       if (ipFL.FeatureClass.ShapeType != esriGeometryType.esriGeometryPoint)
                    continue;
       IFeatureCursor ipFU=ipFL.FeatureClass.Search(null,true);
       IFeature ipF;
       while((ipF=ipFU.NextFeature())!=null)
       {
           IPoint ipP=new PointClass();
           ipP.X=((IPoint)ipF.Shape).X;
           ipP.Y=((IPoint)ipF.Shape).Y;

           IFeature ipNewShapeFeature=ipShapeFC.NewFeature();
           ipNewShapeFeature.Shape=ipP;
           //We set geometry field,here is the place for setting any other common field in                 all layers
           ipNewShapeFeature.Store();
       }
       Marshal.ReleaesComObject(ipFU);
   }

   //All layers finished
   ipWE.StopEditOperation();
   ipWE.StopEditing(true);
   Marshal.ReleaseComObject(ipLayers);
}

I supposed you take care of matching coordinate systems in source and target and also I assumed there is a data member in this class ipDoc of type IMxDocument which is initialized in OnCreate procedure of this class like this:

public void OnCreate(object hook)
{
   if (hook is IApplication)
   {
       IApplication ipApp = hook as IApplication;
       ipDoc = ipApp.Document as IMxDocument;
   }
}

Hope this helps event I could not put all class codes here.Using VS 2012,ArcGIS Desktop 10.2 .NET SDK,you can use ArcGIS project Wizard in Visual Studio to create this class.

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.