Well … I can provide some hints on that (In fact I have something that does that but it’s done using solely C#).
Assume that you have a collection of spaces (shown all equal for clarity and also shown in a random state of (user controlled) density):
Assume that you have 2 goups (or more) of classifications applied for each space. These can being viewed as rows/columns in a matrix like the image of yours.
Assume that you assign to each space a row and a column meaning that a given space belongs to a x class from the classes in the rows and also belongs to a y class in the columns. Then you can create a classification ttree where the first item is the N of row and the second the N of column. As a matrix this has as follows (where each matrix item represends how many spaces have this row and that column assigned):
Asume that you find the max item in the matrix and then calculate a “container” as square as possible. Assume that you create these “containers’” as a collection of empty “pockets”.Then the only thing remaining is to populate the pockets :
See another combo with different matrix size: