PROBLEM
In ArcGIS Pro, when retrieving point features intersecting polygon features using ArcPy, both intersecting and non-intersecting point features are returned. In this example, the script returns all point features displayed on the map instead of only the intersecting point features.

The CopyFeatures function references the initial point features instead of the intersecting point features identified through the spatial selection created by the SelectLayerByLocation function. As a result, the CopyFeatures function returns the initial point features instead of only the intersecting point features.
import arcpy
def main():
arcpy.env.workspace = arcpy.env.scratchGDB
arcpy.env.overwriteOutput = True
poly = r"<polygon_path>" point = r"<point_path>"
arcpy.MakeFeatureLayer_management(point, "<point_layer>") arcpy.MakeFeatureLayer_management(poly, "<polygon_layer>")
total_points = int(arcpy.management.GetCount(point)[0])
total_points = int(arcpy.management.GetCount(point)[0])
arcpy.SelectLayerByLocation_management(
"point_lyr",
"WITHIN",
"poly_lyr",
"",
"NEW_SELECTION",
"INVERT"
)
non_intersecting_points = int(
arcpy.management.GetCount("point_lyr")[0]
)
intersecting_points = total_points - non_intersecting_points
print(f"Total point features: {total_points}")
print(f"Total intersecting point features: {intersecting_points}")
point_select = "point_select"
arcpy.CopyFeatures_management(
"point_lyr",
point_select
)
if __name__ == "__main__":
main()
The code below shows the sample script.
import arcpy
def main():
arcpy.env.workspace = arcpy.env.scratchGDB
arcpy.env.overwriteOutput = True
poly = r"test.gdb\test_poly"
point = r"test.gdb\test_point"
arcpy.MakeFeatureLayer_management(point, "point_lyr")
arcpy.MakeFeatureLayer_management(poly, "poly_lyr")
total_points = int(arcpy.management.GetCount(point)[0])
arcpy.SelectLayerByLocation_management(
"point_lyr",
"WITHIN",
"poly_lyr",
"",
"NEW_SELECTION",
"INVERT"
)
non_intersecting_points = int(
arcpy.management.GetCount("point_lyr")[0]
)
intersecting_points = total_points - non_intersecting_points
print(f"Total point features: {total_points}")
print(f"Total intersecting point features: {intersecting_points}")
point_select = "point_select"
arcpy.CopyFeatures_management(
"point_lyr",
point_select
)
if __name__ == "__main__":
main()
The output message displays the number of point features intersecting polygons.

Article ID: 000043115
Get help from ArcGIS experts
Start chatting now