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
asked May 27, 2015 at 1:12
-
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– Michael Stimson2015年05月27日 01:26:38 +00:00Commented May 27, 2015 at 1:26
-
@Michael I want to use python, since I'm currently using arcpy packages.brentiemapper– brentiemapper2015年05月27日 01:40:15 +00:00Commented May 27, 2015 at 1:40
-
1To 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.Michael Stimson– Michael Stimson2015年05月27日 01:46:00 +00:00Commented May 27, 2015 at 1:46
1 Answer 1
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
-
I think you misunderstood the question. The OP asks for exporting only selected features. Not layers which are visible.Fezter– Fezter2015年05月27日 03:34:59 +00:00Commented May 27, 2015 at 3:34
-
1FeatureClassToFeatureClass_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– Learner2015年05月27日 03:36:02 +00:00Commented May 27, 2015 at 3:36
-
@SIslam It worked! I was exploring the arcpy.da.SearchCusor but this answer definitely solved my problem.brentiemapper– brentiemapper2015年05月27日 03:56:22 +00:00Commented May 27, 2015 at 3:56
-
I edited the answer as asked the question.Learner– Learner2015年05月27日 03:57:55 +00:00Commented 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– Fezter2015年05月27日 06:03:42 +00:00Commented May 27, 2015 at 6:03
Explore related questions
See similar questions with these tags.