Solved

Equal parts


Userlevel 1
Badge +5

Hi, 

Is there a way to divide records into equal parts based on the value of an attribute? In concrete terms, I want to divide 400 areas into 3 pieces, each of which is the same size.

Thx

icon

Best answer by geomancer 8 May 2024, 16:50

View original

8 replies

Userlevel 6
Badge +34

How I solved this:

  • Grid the area using a Tiler.
  • Calculate the area for each tile using an AreaCalculator.
  • Using an AttributeCreator with Adjacent Features and sum the area of the current feature with the area of the previous feature until I got the area I needed, then started over again.

The disadvantage of this method is that the resulting polyons are long in one direction, but it works.

Userlevel 1
Badge +5

Thx for the answer but I guess I wasn't clear :-) It's about areas with lines and dots in them. For each area I have calculate the amount of lines and points into an attribute. We need to load those areas with those lines and points in an application, but it's too much to do in one go. That's why we want to divide those areas into 3 days, but I always want a combination of areas that is approximately the same size as the next combination.. 

It’s like this 

the total count is 2639199

My goal for the result is to have 3 groups with codes who have together approximately 1/3 of the total count. 

 

 

 
Userlevel 6
Badge +34

Ah lol. Depends on the data, but how about sorting descending on count and give the records group id 1,2,3,1,2,3,1,2,3 etc?

Userlevel 1
Badge +5

Manual? This was a sample, i have 1500 records :-)

Userlevel 5
Badge +36

Nice simple hack from @nielsgerrits !

But why do things manually when you can let FME do the dirty work for you? 😃

Calculate Group as @fmod(@Count(),3) (or maybe expand to @fmod(@Count()+1,3)+1, which gives the results shown below)

 

Userlevel 6
Badge +34

Manual? This was a sample, i have 1500 records :-)

Hell no :)

Attached sample workspace.

Userlevel 5
Badge +36

The approach provided above will always put considerably more 'weight’ in group 1 than in group 3.

It would be better to assign the sorted features consecutively to Group 1, Group  2, Group 3, Group 3, Group 2, Group 1, and so on.

After sorting, give each feature a number using @Count(). Then determine to which group to assign each feature, based on this number.

This can be done with a Conditional Value:

It is also possible (and much more fun 😀) to put these conditions in the Math Operator x?y:z (if condition x is true then use y, else use z):

@fmod(@int(@Value(Number)/3),2) == 0 ? @fmod(@Value(Number),3) : 2-@fmod(@Value(Number),3)

The attached workspace illustrates both approaches.

Userlevel 1
Badge +5

Thanks, that works perfectly! I had never come up with that solution myself

Reply