Skip to main content
Solved

Random grid selection

  • November 5, 2020
  • 4 replies
  • 51 views

Forum|alt.badge.img

Hello!

I need to randomly select 7 grids from the grid net including condition that selected grids cannot be self-intersecting and cannot overlap each other. See image below.

 

How to approach it in the most efficient way?

FME_grid

Best answer by ebygomm

And because looping blocking transformers are so horrible to work with an alternative solution using a spatial relator and some python

Capture 

import fme
import fmeobjects
import random
 
class chooseSquares(object):
    def __init__(self):
        #create blank list
        self.mylist=[]
    def input(self,feature):
        #build list containing feature, id and relationships
        id = feature.getAttribute('_count')
        touches = feature.getAttribute('_relationships{}._count')
        self.mylist.append((feature,id,touches))
    def close(self):     
        for x in range(0,7):
            #shuffle list
            random.shuffle(self.mylist)
            #choose grid from first feature inlist
            newfeature = self.mylist[0][0]
            #get features that interact
            exclude = self.mylist[0][2]
            #remove from list any features that intersect chosen gridsquare
            self.mylist = [(f,i,r) for f,i,r in self.mylist if str(i) not in exclude]
            #outputchosengridsquare
            self.pyoutput(newfeature)

 

 

This post is closed to further activity.
It may be an old question, an answered question, an implemented idea, or a notification-only post.
Please check post dates before relying on any information in a question or answer.
For follow-up or related questions, please post a new question or idea.
If there is a genuine update to be made, please contact us and request that the post is reopened.

4 replies

redgeographics
Celebrity
Forum|alt.badge.img+62

Use a Sampler to randomly grab one tile, then a SpatialFilter to select all tiles not touching that randomly selected tile. Then use that result set as the input for the next iteration, and so on.

Screenshot 2020-11-05 at 16.15.05I've done 2 iterations here, you can simply copy & paste the rest, or try and do it through looping in a custom transformer.


ebygomm
Influencer
Forum|alt.badge.img+46
  • Influencer
  • November 5, 2020

Looping transformers in FME are fairly horrible to work with when you've got blocking transformers, but it can be done. This is more or less the same workflow as @redgeographics within the transformer but just handles the looping

Capture 

 


ebygomm
Influencer
Forum|alt.badge.img+46
  • Influencer
  • Best Answer
  • November 5, 2020

And because looping blocking transformers are so horrible to work with an alternative solution using a spatial relator and some python

Capture 

import fme
import fmeobjects
import random
 
class chooseSquares(object):
    def __init__(self):
        #create blank list
        self.mylist=[]
    def input(self,feature):
        #build list containing feature, id and relationships
        id = feature.getAttribute('_count')
        touches = feature.getAttribute('_relationships{}._count')
        self.mylist.append((feature,id,touches))
    def close(self):     
        for x in range(0,7):
            #shuffle list
            random.shuffle(self.mylist)
            #choose grid from first feature inlist
            newfeature = self.mylist[0][0]
            #get features that interact
            exclude = self.mylist[0][2]
            #remove from list any features that intersect chosen gridsquare
            self.mylist = [(f,i,r) for f,i,r in self.mylist if str(i) not in exclude]
            #outputchosengridsquare
            self.pyoutput(newfeature)

 

 


Forum|alt.badge.img
  • Author
  • November 6, 2020

Thanks for providing all the suggestions! it worked smoothly!