4

How do you create a new feature class from the select by location tool in python? This is my script that works but I just don't know what the next step is.

arcpy.SelectLayerByLocation_management("Buildings", "WITHIN_A_DISTANCE", "Hydrography", "50 feet", "NEW_SELECTION","NOT_INVERT") 
  • 1
    can you display the code please? and is this for ArcGIS/arcpy? – spk578 Nov 25 '15 at 12:19
  • it selects the buildings that are 50 feet from the hydrography but got stuck on how to create a feature class from just the selected features – user62946 Nov 25 '15 at 12:32
  • Please use the edit button beneath your question to revise it with any requested clarifications. – PolyGeo Nov 25 '15 at 12:34
6

To do this with python you need to create some variables first so you can call the selection and with the arcpy.CopyFeatures tool copy the selection to a new feature class.

# Define output feature class location    
fc = "C:\Users\Documents\ArcGIS\Default.gdb\Testers"
# Define Selection criteria
Selection = arcpy.SelectLayerByLocation_management('Points', "WITHIN", 'Trajectory')
# Define output selection and fc
arcpy.CopyFeatures_management(Selection, fc)

This example was used within the Python interpreter in ArcMap. You can see that by using variables it makes everything easier to use and understand.

The example you provide should be something like this:

import arcpy 
#Set geoprocessing environments 
arcpy.env.workspace = "C:/Student/PythonBasics10_0/Westerville.gdb"
arcpy.env.overwriteOutput = True 

# Set name of output fc and select buildings by location
Outputfc =  "BuildingsWithin50ft"
Selection = arcpy.SelectLayerByLocation_management("Buildings", "WITHIN_A_DISTANCE", "Hydrography", "50 feet", "NEW_SELECTION","NOT_INVERT") 

arcpy.CopyFeatures_management(Selection, Outputfc) 
  • ok let me try it and see what iget – user62946 Nov 25 '15 at 12:35
  • have a look at the answer above...I think you need to understand that variables are fed into the functions so I have defined Outputfc a string that will feed through the Copyfeatures function to create a fc called "BuildingsWithin50ft" to the arcpy.env.workspace – spk578 Nov 25 '15 at 12:44
  • No worries tick the answer as correct if it is correct :-) – spk578 Nov 25 '15 at 12:44
  • and it is hard to get the hang of it – user62946 Nov 25 '15 at 12:47
  • You'll get there...it's just practise :-) Good luck! – spk578 Nov 25 '15 at 13:15
3

An alternative approach to creating new fc from selected layer features would be:

  1. Define environmental workspace
  2. Define two selection fc's to use within select by location method
  3. Use Make Feature Layer method (you will have to use this if performing selections via standalone script, outside of ArcGIS)
  4. Use Select Layer by Location method
  5. Use Feature Class to Feature Class method to create new layer from selected features
  • 2
    why does arcgis require you to create a feature layer to use in the select by location? – ziggy Oct 6 '16 at 19:29
  • This is required it the code/layer is run in standalone outside of ArcMap so the layer may be selected against. – artwork21 Jun 15 '18 at 19:04
  • yes but why? you can run other standalone tool scripts using the paths to the actual feature class – ziggy Jun 19 '18 at 13:00
  • When dealing with evaluating intersection between layer feature you have to make it a feature layer. As far as the why may be an Esri developer can expand more. – artwork21 Jun 19 '18 at 16:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy