Select to view content in your preferred language

Export Selected Features from Layer within ArcMap

7154
13
10-19-2015 11:18 AM
mpboyle
Regular Contributor

I'm trying to create a python script that will export selected features from a layer within ArcMap.  The general workflow would be the following:

1) User opens ArcMap

2) User manually selects features from layer

3) User runs script that exports ONLY selected features from layer

This seems pretty straight-forward but I'm not used to dealing with interactive selections.  I'm also wondering if it's necessary to add in some logic for variances in layer names since users may alter the layer name...?  Can this be made as a parameter of the script?

I'm also wondering if it would be best to have users run this as a python tool from a toolbox or if a python add-in is possible so it's a button click...?

0 Kudos
13 Replies
DarrenWiens2
MVP Honored Contributor

Most tools respect selections. If you run Copy Features while there is a selection, only the selected features will be copied (otherwise, all features will be copied).

LukeSturtevant
Occasional Contributor III

Is there any reason you wouldn't just use Copy Features ArcGIS Help (10.2, 10.2.1, and 10.2.2) ? I'm just trying to figure out what the purpose of creating a script tool would be when there are multiple standard ways to do an export operation within ArcMap? I suppose having a python add-in on mouse click or selection would be the most user friendly way to go about it. Is this for users who are not familiar with ArcMap?

0 Kudos
mpboyle
Regular Contributor

This is the initial step of the script.  I have several other geoprocessing functions to run after I get the selection set exported to a working feature class (ex: Near, Join, etc...)  I'm just looking to get started on the best way to have a script export out a selected set of features from a map doc.

0 Kudos
IanMurray
Frequent Contributor

Conversely from what Darren posted, if you don't want them mucking about with geoprocessing tools at all, you could have them right click the layer and Data>Export Data.  The drop down allows for only selected features to be exported to a new feature class.

This is an easy enough task I'm having a hard time finding a reason to automate such a small portion.

0 Kudos
mpboyle
Regular Contributor

Again...from the reply above, this is just the first step in a longer process.

0 Kudos
LukeSturtevant
Occasional Contributor III

Python Add-Ins are definitely handy if you can get the coding to work. Whether you go with an add-in or just a standard script tool you'll want to set it up with a selection check like so:

import arcpy
feature =  arcpy.GetParameterAsText(0)
outputFeature =  arcpy.GetParameterAsText(1)


selectionCheck = arcpy.Describe(feature).FIDSet
if not selectionCheck:
   arcpy.AddError("\nYou must have a selection.\n")
else:
    arcpy.CopyFeatures_management(feature, outputFeature)
0 Kudos
mpboyle
Regular Contributor

Luke,

Maybe I'm not understanding the GetParameterAsText method, I'm assuming it's just a user-entered value?  If I use your code snippet, I get the error shown below.  Your code makes sense to me...enter a layer name, enter an output feature class, check to see if the layer has a set of OBJECTIDs, if not raise an error, if so, copy to output.

Not sure why I'm getting this error...

error.JPG

0 Kudos
DarrenWiens2
MVP Honored Contributor

arcpy.GetParameterAsText() grabs the value from the tool dialog. Since you're running this from the Python window, there is no tool dialog, thus no value to describe. Until you're ready to run this within a tool, you can debug with a hardcoded value:

lyr = "THE_NAME_OF_YOUR_LAYER_IN_THE_TOC"

WesMiller
Regular Contributor III

You use GetParameterAsText as a way to pass parameters to a script. For your test set your lyr = "YourLayersName" and it should run.