1

I have several layers in an ArcMap Document. All of these layers are stored as shape files.

Specific features of the said layers are selected. I want to export the data as shapefile programatically. How can I do this using arcpy?

Sample view of my arcmap workspace

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked May 27, 2015 at 1:12
3
  • The question is what do you want to use? ModelBuilder, python, C#, VB.net... it is kind of difficult in python/modelbuilder to see if a layer has a selection but not impossible (in python). ArcObjects (C#,VB.net) is easy to see the selection but unless you have some programming experience that's probably not going to help. Commented May 27, 2015 at 1:26
  • @Michael I want to use python, since I'm currently using arcpy packages. Commented May 27, 2015 at 1:40
  • 1
    To determine if a layer has a selection read gis.stackexchange.com/questions/73173/… it's to do with the FIDset... iterate through the map layers then use CopyFeatures to export the selected records. Commented May 27, 2015 at 1:46

1 Answer 1

6

Try..

 ## This script will look for all the layers that have feature selected in them in the TOC and export them in seperated shapefile.
 ##output layer name will be the original name+ _selected e.g. luzon_loss_selected
 import arcpy
 arcpy.env.overwriteOutput = True
 mxd = arcpy.mapping.MapDocument("CURRENT")
 df = arcpy.mapping.ListDataFrames( mxd, "Layers")[0]
 layers = arcpy.mapping.ListLayers(mxd, "*", df)
 output_folder = r'C:\Users\YOUR_USER_NAME\Desktop\test' ##folder path of output
 for layer in layers:
 Sel=arcpy.Describe(layer)
 if Sel.FIDset: 
 arcpy.FeatureClassToFeatureClass_conversion(layer,output_folder,layer.name + "_selected","","","")
answered May 27, 2015 at 3:25
6
  • I think you misunderstood the question. The OP asks for exporting only selected features. Not layers which are visible. Commented May 27, 2015 at 3:34
  • 1
    FeatureClassToFeatureClass_conversion exports only selected records. I used 'visible' property to check if the layer is turned on/off if turned on then it will export only selected records. Commented May 27, 2015 at 3:36
  • @SIslam It worked! I was exploring the arcpy.da.SearchCusor but this answer definitely solved my problem. Commented May 27, 2015 at 3:56
  • I edited the answer as asked the question. Commented May 27, 2015 at 3:57
  • +1 SIslam, the way you've written now is better. However, you're incorrect about FeatureClassToFeatureClass. It will only export selected features if there is a selection. But if there is no selection, it will export the whole feature class. But the way you've rewritten it, it appears you realise that. Commented May 27, 2015 at 6:03

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.