Skip to content

Instantly share code, notes, and snippets.

@chadcooper
Last active December 16, 2015 19:09
Show Gist options
  • Save chadcooper/5482872 to your computer and use it in GitHub Desktop.
Save chadcooper/5482872 to your computer and use it in GitHub Desktop.
import arcpy
in_polys = "NcCountiesT.shp"
in_points = "NcNatRegPts.shp"
desc = arcpy.Describe(in_polys)
oid_fld = desc.OIDFieldName
# Make featurelayers for input into SelectByLocation
arcpy.MakeFeatureLayer_management(in_polys, "polys_lyr")
arcpy.MakeFeatureLayer_management(in_points, "points_lyr")
cursor = arcpy.SearchCursor("polys_lyr")
for row in cursor:
# Get row id number
row_id = row.getValue(oid_fld)
# Select just the current polygon
arcpy.SelectLayerByAttribute_management("polys_lyr", "NEW_SELECTION",
'"{0}" = {1}'.format(oid_fld, row_id))
# Run select by location on just the currently selected polygon
pts = arcpy.SelectLayerByLocation_management("points_lyr", "intersect", "polys_lyr")
matchcount = int(arcpy.GetCount_management('points_lyr').getOutput(0))
print str(matchcount)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment