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

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","","","")
6
  • I think you misunderstood the question. The OP asks for exporting only selected features. Not layers which are visible.
    – Fezter
    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.
    – Learner
    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.
    – Learner
    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.
    – Fezter
    Commented May 27, 2015 at 6:03

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.