[GIS] How to check if one polygon intersects with a feature class

arcpyfeature-classintersect

I dynamically create a polygon from an array like this:

import decimal
lines = ['1|421474.904|4467629.2341','2|421590.35|4467592.7209','3|421611.5036|4467661.6442','4|421611.4173|4467661.7276','5|421606.8376|4467666.1544']
pgn = arcpy.Array()
for i,line in enumerate(lines,1):
    crds = line.split('|')
    pnt = arcpy.Point()
    pnt.ID = int(decimal.Decimal(crds[0]))
    pnt.X = float(decimal.Decimal(crds[1]))
    pnt.Y = float(decimal.Decimal(crds[2]))
    pgn.add(pnt)
sr = arcpy.SpatialReference()
sr.factoryCode = 2100 # ΕΓΣΑ 1987
sr.create()
polyg = arcpy.Polygon(pgn,sr)
del pgn, pnt

and I want to check if this polygon intersects with a specific feature class (it has 130.000 features), i.e.:

DX = ur'D:\DX.gdb\DX'

If it does, I want to get the output of the intersection and add it in an existent polygon feature class, i.e.:

output = ur'D:\Test.gdb\polygons'

If not, I want to get a False statement.
Instead of using the Intersect_Analysis Tool is there a faster way to do this?

Best Answer

Since you have to loop over to 130k polygons, the intersect_analysis tool will be the best and fasted choice. You can use your geometry polygon directly as an input feature.

Write the output to an in memory feature class, count the number of output features and if greater than 0, append to your other features class

arcpy.Intersect_analysis ([DX, polygon], "in_memory/output")
result = arcpy.GetCount_management("in_memory/output")
count = int(result.getOutput(0))
if count > 0:
    arcpy.Append_management ("in_memory/output", output, "NO_TEST")
else:
    pass
Related Question