I've got a grid of polygons, each square is equal in size but has an attribute stored against it with a numeric value. I want to group grid squares together based on a sum of that attribute, but that sum needs to be as close to an upper limit as possible. So for example in the picture below all 'like' coloured grid squares are collected into groups where their attribute sum does not exceed 60,000. As close to 60,000 as possible would also be acceptable. I can't have disparate geographic groups, it has to be one block of squares, albeit that block doesn't have to be completely uniform.
When I started looking into this I thought, this must have a "name", an existing algorithm and "I bet there's already a custom transformer"... Well, I haven't found anything yet. Can anyone help put me out of my misery? This feels like a loop might be required, but the issue is the proximity, the grid squares might need to be pre-sorted geographically first? Any help gratefully received. Thanks, Dave