I have a polygon layer of watersheds (*N* = 1950) for South Africa. Within each watershed is a value (number of people; POPULATION field). I need to find a way to automatically group contiguous watersheds so that when the summed values of a group of watersheds falls between 50 and 100, those watersheds are assigned a unique ID in a new attribute field called GROUP. The code that I'm looking for would need to run through the entire polygon layer, essentially assigning ID's (i.e., groups) to each watershed in the layer. I'd imagine that in a polygon layer of 1950 watersheds, there would be ~300 unique groups. Exactly the same question has been posted previously (http://gis.stackexchange.com/questions/80325/clustering-polygons-by-sum-of-values) but unfortunately they never posted their code.