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.
Asked
Active
Viewed 313 times
0
-
Have a look at http://gis.stackexchange.com/questions/131841/divide-polygons-into-n-number-of-groups-of-equal-counts-with-arcgis-10-2/132393#132393 – FelixIP Mar 11 '15 at 22:30
-
@FelixIP that's a question I posted that isn't quite the same thing as what is being asked here, if I'm understanding Alinaa's question correctly. My question doesn't incorporate shared boundaries. – Emil Brundage Mar 12 '15 at 05:30
-
Approach is exactly the same. It includes very minor change in spatial join setup to create what I call links table. If she is talking about something similar to your illustration simple dissolve with no case field will do. – FelixIP Mar 12 '15 at 06:29
-
@EmilBrungade forgot the hash – FelixIP Mar 12 '15 at 06:35
2 Answers
1
Making use of feature layers, selections, and a while loop to select neighboring features until no new features are selected is the way to go, if you just want to cluster all features that touch together. The script below adds a field and updates it with a cluster number. The clusters represent all touching features.
import arcpy
#In feature class
fc = r"C:\Users\Emil\Documents\ArcGIS\Default.gdb\testpolygon"
#New cluster field name
fieldname = "Group"
#Add field
arcpy.AddField_management (fc, fieldname, "SHORT")
#Make feature layer to allow for selection
arcpy.MakeFeatureLayer_management (fc, "lyr")
#Get layer OID field name for use in SQL
OIDfieldName = arcpy.Describe ("lyr").OIDFieldName
#Create list for OIDs that have been selected
allselectedOIDs = []
#Variable to designate clusters
Cluster = 1
#cursor to iterate through features
cursor = arcpy.da.SearchCursor("lyr", "OID@")
for row in cursor:
#Check if OID has been selected and assigned to a cluster
if row[0] in allselectedOIDs:
#skip iteration
continue
#SQL to select single feature
sql = '"{0}" = {1}'.format (OIDfieldName, row[0])
#Set old selection count to 0
OldSelectionCount = 0
#Select feature
arcpy.SelectLayerByAttribute_management ("lyr", "", sql)
#set new selection count to 1
NewSelectionCount = 1
#While loop
while NewSelectionCount != OldSelectionCount:
#Update old selection count
OldSelectionCount = NewSelectionCount
#Select neighbors
arcpy.SelectLayerByLocation_management ("lyr", "", "lyr")
#Get new selection count
NewSelectionCount = int(arcpy.GetCount_management("lyr").getOutput(0))
#Get list of selected feature OIDs with list comprehension
selectedOIDs = [r[0] for r in arcpy.da.SearchCursor("lyr", "OID@")]
#Add selected feature OIDs to all OIDs list
allselectedOIDs += selectedOIDs
#Update layer field with cluster number
arcpy.CalculateField_management ("lyr", fieldname, Cluster)
print Cluster
#Add 1 to cluster variable
Cluster += 1
#Clean up
del row
del cursor
Results:

I hope this is along the lines of what you're looking for!
Emil Brundage
- 13,859
- 3
- 26
- 62
0
Thanks for your answers.. Done that using this script
# 1.a Checking Polygon is Touching or not
polys = []
selected_polygon = []
rowcount = 0
with arcpy.da.SearchCursor("Census_data","Shape@") as cursor:
for row in cursor:
polys.append(row[0])
rowcount = rowcount + 1
if(rowcount >= 1):
with arcpy.da.SearchCursor("Census_data","Shape@") as cursor:
for row in cursor:
selected_polygon.append(row[0])
for i in range(len(selected_polygon)):
overlap = 0
for j in range(len(polys)):
if selected_polygon[i].distanceTo(polys[j]) == 0:
overlap = overlap + 1
if overlap >= 1:
print ("Touching")
else:
print ("Not Touching")
break
Waqar ahmad
- 101
- 9
-
1Why is Aggregate Polygons http://resources.arcgis.com/en/help/main/10.1/index.html#//00700000000s000000 with a very small tolerance not suitable? I think the word you wanted was Contiguous (close together) not Contagious (like a disease you catch) - damn spellcheck! I usually use Dissolve followed by Multipart to Single Part to turn clusters into larger polygons but the boundaries must be exactly the same or they wont dissolve, the Aggregate will be more forgiving about very close vs shared boundary. – Michael Stimson May 28 '15 at 00:05