Select to view content in your preferred language

how to make clusters from small polygons with constraint of boundary touching?

4533
5
Jump to solution
03-11-2015 12:06 PM
waqarahmad
Deactivated User

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.

0 Kudos
1 Solution

Accepted Solutions
DarrenWiens2
MVP Honored Contributor

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')

1.png

2.png

View solution in original post

5 Replies
DarrenWiens2
MVP Honored Contributor

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)?

0 Kudos
waqarahmad
Deactivated User

i have only polygons which touching EACH OTHER'S boundry

0 Kudos
DarrenWiens2
MVP Honored Contributor

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')

1.png

2.png

yosukekimura
Occasional Contributor

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...

yosukekimura
Occasional Contributor

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)