Skip to main content
More detail
Source Link
Aaron
  • 51.8k
  • 29
  • 157
  • 321

The following arcpy-based script which uses select layer by attributes is a good starting point. I retrofitted the script to work as a script tool in ArcGIS. You can easily substitute polygon FCs for point FCs in the Select Layers section to tailor the script to your specific needs.

# Import system modules
import arcpy
from arcpy import env

# Set the workspace
env.workspace = arcpy.GetParameterAsText(0)
Dir = env.workspace

# Local variables
input = arcpy.GetParameterAsText(1)
polygon = arcpy.GetParameterAsText(2)
expression = arcpy.GetParameterAsText(3)
name = arcpy.GetParameterAsText(4)

# Make a layer from the input feature class
arcpy.MakeFeatureLayer_management(input, "lyr") 
 
# Select all points or polygons which overlap the polygon/s of interest
arcpy.SelectLayerByLocation_management("lyr", "intersect", polygon, 0, "new_selection")

# Within selected features, further select based on a SQL query within the script tool    
arcpy.SelectLayerByAttribute_management("lyr", "SUBSET_SELECTION", expression)
 
# Write the selected features to a new featureclass
arcpy.CopyFeatures_management("lyr", Dir + "\\" + str(name))

Using the following parameters:

enter image description here

In this test example,the output includes only the points 1) within the polygons and 2) with "OBJECTID" > 50.

enter image description here

The following arcpy-based script which uses select layer by attributes is a good starting point. I retrofitted the script to work as a script tool in ArcGIS.

# Import system modules
import arcpy
from arcpy import env

# Set the workspace
env.workspace = arcpy.GetParameterAsText(0)
Dir = env.workspace

# Local variables
input = arcpy.GetParameterAsText(1)
polygon = arcpy.GetParameterAsText(2)
expression = arcpy.GetParameterAsText(3)
name = arcpy.GetParameterAsText(4)

# Make a layer from the input feature class
arcpy.MakeFeatureLayer_management(input, "lyr") 
 
# Select all points or polygons which overlap the polygon/s of interest
arcpy.SelectLayerByLocation_management("lyr", "intersect", polygon, 0, "new_selection")

# Within selected features, further select based on a SQL query within the script tool    
arcpy.SelectLayerByAttribute_management("lyr", "SUBSET_SELECTION", expression)
 
# Write the selected features to a new featureclass
arcpy.CopyFeatures_management("lyr", Dir + "\\" + str(name))

Using the following parameters:

enter image description here

In this test example,the output includes only the points 1) within the polygons and 2) with "OBJECTID" > 50.

enter image description here

The following arcpy-based script which uses select layer by attributes is a good starting point. I retrofitted the script to work as a script tool in ArcGIS. You can easily substitute polygon FCs for point FCs in the Select Layers section to tailor the script to your specific needs.

# Import system modules
import arcpy
from arcpy import env

# Set the workspace
env.workspace = arcpy.GetParameterAsText(0)
Dir = env.workspace

# Local variables
input = arcpy.GetParameterAsText(1)
polygon = arcpy.GetParameterAsText(2)
expression = arcpy.GetParameterAsText(3)
name = arcpy.GetParameterAsText(4)

# Make a layer from the input feature class
arcpy.MakeFeatureLayer_management(input, "lyr") 
 
# Select all points or polygons which overlap the polygon/s of interest
arcpy.SelectLayerByLocation_management("lyr", "intersect", polygon, 0, "new_selection")

# Within selected features, further select based on a SQL query within the script tool    
arcpy.SelectLayerByAttribute_management("lyr", "SUBSET_SELECTION", expression)
 
# Write the selected features to a new featureclass
arcpy.CopyFeatures_management("lyr", Dir + "\\" + str(name))

Using the following parameters:

enter image description here

In this test example,the output includes only the points 1) within the polygons and 2) with "OBJECTID" > 50.

enter image description here

More concise
Source Link
Aaron
  • 51.8k
  • 29
  • 157
  • 321

As an alternative to @egdetti's solution, theThe following arcpy-based script from ESRI, which uses select layer by attributes will beis a good starting point:

# Name: ExtactFeaturesByLocationAndAttribute.py
# Description: Extract features to a new feature class based on a spatial relationships to another layer AND an attribute query
# Author: ESRI
 
# Import system modules
import arcpy

# Set the workspace
env.workspace = "c:/data/mexico.gdb"

# Make a layer from the feature class
arcpy.MakeFeatureLayer_management("cities", "lyr") 
 
# Select all cities which overlap the chihuahua polygon
arcpy.SelectLayerByLocation_management("lyr", "intersect", "chihuahua", 0, "new_selection")

# Within selected features, further select only those cities which have a population > 10,000   
arcpy.SelectLayerByAttribute_management("lyr", "SUBSET_SELECTION", ' "population" > 10000 ')
 
# Write the selected features to a new featureclass
arcpy.CopyFeatures_management("lyr", "chihuahua_10000plus")

From there, try retrofitting. I retrofitted the script to work as a script tool: in ArcGIS.

# Import system modules
import arcpy
from arcpy import env

# Set the workspace
env.workspace = arcpy.GetParameterAsText(0)
Dir = env.workspace

# Local variables
input = arcpy.GetParameterAsText(1)
polygon = arcpy.GetParameterAsText(2)
expression = arcpy.GetParameterAsText(3)
name = arcpy.GetParameterAsText(4)

# Make a layer from the input feature class
arcpy.MakeFeatureLayer_management(input, "lyr") 
 
# Select all points or polygons which overlap the polygon/s of interest
arcpy.SelectLayerByLocation_management("lyr", "intersect", polygon, 0, "new_selection")

# Within selected features, further select based on a SQL query inwithin the script tool    
arcpy.SelectLayerByAttribute_management("lyr", "SUBSET_SELECTION", expression)
 
# Write the selected features to a new featureclass
arcpy.CopyFeatures_management("lyr", Dir + "\\" + str(name))

Using the following parameters:

enter image description here

In this test example,the output includes only the points 1) within the polygons and 2) with "OBJECTID" > 50.

enter image description here

As an alternative to @egdetti's solution, the following script from ESRI, which uses select layer by attributes will be a good starting point:

# Name: ExtactFeaturesByLocationAndAttribute.py
# Description: Extract features to a new feature class based on a spatial relationships to another layer AND an attribute query
# Author: ESRI
 
# Import system modules
import arcpy

# Set the workspace
env.workspace = "c:/data/mexico.gdb"

# Make a layer from the feature class
arcpy.MakeFeatureLayer_management("cities", "lyr") 
 
# Select all cities which overlap the chihuahua polygon
arcpy.SelectLayerByLocation_management("lyr", "intersect", "chihuahua", 0, "new_selection")

# Within selected features, further select only those cities which have a population > 10,000   
arcpy.SelectLayerByAttribute_management("lyr", "SUBSET_SELECTION", ' "population" > 10000 ')
 
# Write the selected features to a new featureclass
arcpy.CopyFeatures_management("lyr", "chihuahua_10000plus")

From there, try retrofitting the script to a script tool:

# Import system modules
import arcpy
from arcpy import env

# Set the workspace
env.workspace = arcpy.GetParameterAsText(0)
Dir = env.workspace

# Local variables
input = arcpy.GetParameterAsText(1)
polygon = arcpy.GetParameterAsText(2)
expression = arcpy.GetParameterAsText(3)
name = arcpy.GetParameterAsText(4)

# Make a layer from the input feature class
arcpy.MakeFeatureLayer_management(input, "lyr") 
 
# Select all points or polygons which overlap the polygon/s of interest
arcpy.SelectLayerByLocation_management("lyr", "intersect", polygon, 0, "new_selection")

# Within selected features, further select based on a SQL query in the script tool    
arcpy.SelectLayerByAttribute_management("lyr", "SUBSET_SELECTION", expression)
 
# Write the selected features to a new featureclass
arcpy.CopyFeatures_management("lyr", Dir + "\\" + str(name))

Using the following parameters:

enter image description here

In this test example,the output includes only the points 1) within the polygons and 2) with "OBJECTID" > 50.

enter image description here

The following arcpy-based script which uses select layer by attributes is a good starting point. I retrofitted the script to work as a script tool in ArcGIS.

# Import system modules
import arcpy
from arcpy import env

# Set the workspace
env.workspace = arcpy.GetParameterAsText(0)
Dir = env.workspace

# Local variables
input = arcpy.GetParameterAsText(1)
polygon = arcpy.GetParameterAsText(2)
expression = arcpy.GetParameterAsText(3)
name = arcpy.GetParameterAsText(4)

# Make a layer from the input feature class
arcpy.MakeFeatureLayer_management(input, "lyr") 
 
# Select all points or polygons which overlap the polygon/s of interest
arcpy.SelectLayerByLocation_management("lyr", "intersect", polygon, 0, "new_selection")

# Within selected features, further select based on a SQL query within the script tool    
arcpy.SelectLayerByAttribute_management("lyr", "SUBSET_SELECTION", expression)
 
# Write the selected features to a new featureclass
arcpy.CopyFeatures_management("lyr", Dir + "\\" + str(name))

Using the following parameters:

enter image description here

In this test example,the output includes only the points 1) within the polygons and 2) with "OBJECTID" > 50.

enter image description here

Updated new script tool comments
Source Link
Aaron
  • 51.8k
  • 29
  • 157
  • 321

As an alternative to @egdetti's solution, the following script from ESRI, which uses select layer by attributes will be a good starting point:

# Name: ExtactFeaturesByLocationAndAttribute.py
# Description: Extract features to a new feature class based on a spatial relationships to another layer AND an attribute query
# Author: ESRI
 
# Import system modules
import arcpy

# Set the workspace
env.workspace = "c:/data/mexico.gdb"

# Make a layer from the feature class
arcpy.MakeFeatureLayer_management("cities", "lyr") 
 
# Select all cities which overlap the chihuahua polygon
arcpy.SelectLayerByLocation_management("lyr", "intersect", "chihuahua", 0, "new_selection")

# Within selected features, further select only those cities which have a population > 10,000   
arcpy.SelectLayerByAttribute_management("lyr", "SUBSET_SELECTION", ' "population" > 10000 ')
 
# Write the selected features to a new featureclass
arcpy.CopyFeatures_management("lyr", "chihuahua_10000plus")

From there, try retrofitting the script to a script tool:

# Import system modules
import arcpy
from arcpy import env

# Set the workspace
env.workspace = arcpy.GetParameterAsText(0)
Dir = env.workspace

# Local variables
input = arcpy.GetParameterAsText(1)
polygon = arcpy.GetParameterAsText(2)
expression = arcpy.GetParameterAsText(3)
name = arcpy.GetParameterAsText(4)

# Make a layer from the input feature class
arcpy.MakeFeatureLayer_management(input, "lyr") 
 
# Select all citiespoints or polygons which overlap the chihuahua polygon/s of interest
arcpy.SelectLayerByLocation_management("lyr", "intersect", polygon, 0, "new_selection")

# Within selected features, further select onlybased thoseon citiesa whichSQL havequery ain populationthe >script 10,000tool    
arcpy.SelectLayerByAttribute_management("lyr", "SUBSET_SELECTION", expression)
 
# Write the selected features to a new featureclass
arcpy.CopyFeatures_management("lyr", Dir + "\\" + str(name))

Using the following parameters:

enter image description here

In this test example,the output includes only the points 1) within the polygons and 2) with "OBJECTID" > 50.

enter image description here

As an alternative to @egdetti's solution, the following script from ESRI, which uses select layer by attributes will be a good starting point:

# Name: ExtactFeaturesByLocationAndAttribute.py
# Description: Extract features to a new feature class based on a spatial relationships to another layer AND an attribute query
# Author: ESRI
 
# Import system modules
import arcpy

# Set the workspace
env.workspace = "c:/data/mexico.gdb"

# Make a layer from the feature class
arcpy.MakeFeatureLayer_management("cities", "lyr") 
 
# Select all cities which overlap the chihuahua polygon
arcpy.SelectLayerByLocation_management("lyr", "intersect", "chihuahua", 0, "new_selection")

# Within selected features, further select only those cities which have a population > 10,000   
arcpy.SelectLayerByAttribute_management("lyr", "SUBSET_SELECTION", ' "population" > 10000 ')
 
# Write the selected features to a new featureclass
arcpy.CopyFeatures_management("lyr", "chihuahua_10000plus")

From there, try retrofitting the script to a script tool:

# Import system modules
import arcpy
from arcpy import env

# Set the workspace
env.workspace = arcpy.GetParameterAsText(0)
Dir = env.workspace

# Local variables
input = arcpy.GetParameterAsText(1)
polygon = arcpy.GetParameterAsText(2)
expression = arcpy.GetParameterAsText(3)
name = arcpy.GetParameterAsText(4)

# Make a layer from the feature class
arcpy.MakeFeatureLayer_management(input, "lyr") 
 
# Select all cities which overlap the chihuahua polygon
arcpy.SelectLayerByLocation_management("lyr", "intersect", polygon, 0, "new_selection")

# Within selected features, further select only those cities which have a population > 10,000   
arcpy.SelectLayerByAttribute_management("lyr", "SUBSET_SELECTION", expression)
 
# Write the selected features to a new featureclass
arcpy.CopyFeatures_management("lyr", Dir + "\\" + str(name))

Using the following parameters:

enter image description here

In this test example,the output includes only the points 1) within the polygons and 2) with "OBJECTID" > 50

enter image description here

As an alternative to @egdetti's solution, the following script from ESRI, which uses select layer by attributes will be a good starting point:

# Name: ExtactFeaturesByLocationAndAttribute.py
# Description: Extract features to a new feature class based on a spatial relationships to another layer AND an attribute query
# Author: ESRI
 
# Import system modules
import arcpy

# Set the workspace
env.workspace = "c:/data/mexico.gdb"

# Make a layer from the feature class
arcpy.MakeFeatureLayer_management("cities", "lyr") 
 
# Select all cities which overlap the chihuahua polygon
arcpy.SelectLayerByLocation_management("lyr", "intersect", "chihuahua", 0, "new_selection")

# Within selected features, further select only those cities which have a population > 10,000   
arcpy.SelectLayerByAttribute_management("lyr", "SUBSET_SELECTION", ' "population" > 10000 ')
 
# Write the selected features to a new featureclass
arcpy.CopyFeatures_management("lyr", "chihuahua_10000plus")

From there, try retrofitting the script to a script tool:

# Import system modules
import arcpy
from arcpy import env

# Set the workspace
env.workspace = arcpy.GetParameterAsText(0)
Dir = env.workspace

# Local variables
input = arcpy.GetParameterAsText(1)
polygon = arcpy.GetParameterAsText(2)
expression = arcpy.GetParameterAsText(3)
name = arcpy.GetParameterAsText(4)

# Make a layer from the input feature class
arcpy.MakeFeatureLayer_management(input, "lyr") 
 
# Select all points or polygons which overlap the polygon/s of interest
arcpy.SelectLayerByLocation_management("lyr", "intersect", polygon, 0, "new_selection")

# Within selected features, further select based on a SQL query in the script tool    
arcpy.SelectLayerByAttribute_management("lyr", "SUBSET_SELECTION", expression)
 
# Write the selected features to a new featureclass
arcpy.CopyFeatures_management("lyr", Dir + "\\" + str(name))

Using the following parameters:

enter image description here

In this test example,the output includes only the points 1) within the polygons and 2) with "OBJECTID" > 50.

enter image description here

More detail
Source Link
Aaron
  • 51.8k
  • 29
  • 157
  • 321
Loading
Source Link
Aaron
  • 51.8k
  • 29
  • 157
  • 321
Loading