I want to make some clusters from a bunch of small polygons with the constraint that each basic unit (polygon) in the resultant cluster must share a portion of its boundary with at least one other basic unit (polygon) in the same cluster. This is becuase of the reason that i want the resultant clusters contagious.
Solved! Go to Solution.
You can pull out polygons intersecting at least one other polygon using the following script:
>>> polys = [] ... outPolys = [] ... with arcpy.da.SearchCursor("YOUR_POLYGON_LAYER_HERE","SHAPE@") as cursor: ... for row in cursor: ... polys.append(row[0]) ... for i in range(len(polys)): ... overlap = 0 ... for j in range(len(polys)): ... if polys.distanceTo(polys) == 0: ... overlap = overlap + 1 ... if overlap > 1: ... outPolys.append(polys) ... arcpy.CopyFeatures_management(outPolys,'in_memory\polys')
How do you want to deal with overlapping polygons (e.g. area of smaller polygon may fully overlap, but boundary may be far inside the larger polygon)?
i have only polygons which touching EACH OTHER'S boundry
You can pull out polygons intersecting at least one other polygon using the following script:
>>> polys = [] ... outPolys = [] ... with arcpy.da.SearchCursor("YOUR_POLYGON_LAYER_HERE","SHAPE@") as cursor: ... for row in cursor: ... polys.append(row[0]) ... for i in range(len(polys)): ... overlap = 0 ... for j in range(len(polys)): ... if polys.distanceTo(polys) == 0: ... overlap = overlap + 1 ... if overlap > 1: ... outPolys.append(polys) ... arcpy.CopyFeatures_management(outPolys,'in_memory\polys')
I haven't tested, but
p.disjoint(q)
intead of
p.distanceTo(q)==0
may runs faster particularly when there are a bunch of polygon.
your method is O(n^2) so may have trouble with large number of polygons...
Darren's method should work, and this is an alternative.
# make table of pair of zero distance arcpy.GenerateNearTable_analysis(fcOrig, fcOrig, tblNear, "0", "NO_LOCATION", "NO_ANGLE", "CLOSEST", "0") if int(arcpy.GetCount_management(tblNear).getOutput(0)) > 0: # grab ids that showed up in the table ids = set([ _[0] for _ in arcpy.da.SearchCursor(tblNear,('IN_FID'))]) # grab shape oid = arcpy.Describe(fcOrig).OIDFieldName outpoly = [_[0] for _ in arcpy.da.SearchCursor(fcOrig, [ 'SHAPE@, oid]) if _[1] in ids ] # save arcpy.CopyFeatures_management(outpoly, fcTouching)