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,136
  • 29
  • 109
  • 338
brentiemapper
  • 752
  • 9
  • 25
  • 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. – Michael Stimson May 27 '15 at 01:26
  • @Michael I want to use python, since I'm currently using arcpy packages. – brentiemapper May 27 '15 at 01:40
  • 1
    To determine if a layer has a selection read http://gis.stackexchange.com/questions/73173/how-do-you-get-all-the-results-from-a-selection-made-by-attribute-or-location-us it's to do with the FIDset... iterate through the map layers then use CopyFeatures to export the selected records. – Michael Stimson May 27 '15 at 01:46

1 Answers1

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","","","")
Learner
  • 3,496
  • 18
  • 34
  • I think you misunderstood the question. The OP asks for exporting only selected features. Not layers which are visible. – Fezter May 27 '15 at 03: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. – Learner May 27 '15 at 03:36
  • @SIslam It worked! I was exploring the arcpy.da.SearchCusor but this answer definitely solved my problem. – brentiemapper May 27 '15 at 03:56
  • I edited the answer as asked the question. – Learner May 27 '15 at 03: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. – Fezter May 27 '15 at 06:03
  • Thanks, sorry, i intended to mean the function of that tool in the scope of my script. – Learner May 27 '15 at 06:10