Skip to main content
Question

Select polygons at random.

  • November 21, 2024
  • 5 replies
  • 78 views

fme_hans
Contributor
Forum|alt.badge.img+5
Hi,I have a shape with polynones. I want to select 30% of the polygons at random. Is that possible?
 

 

 

 

5 replies

liamfez
Influencer
Forum|alt.badge.img+34
  • Influencer
  • November 21, 2024

I think the Advanced Sampler from the Hub would be what you are looking for using the Sampling Rate and Randomize Sampling options.


You could also use the default Sampler also with Randomize Sampling and have a sampling rate of 3 using the every Nth feature sampling type


fme_hans
Contributor
Forum|alt.badge.img+5
  • Author
  • Contributor
  • November 22, 2024
The sampler does indeed work. But if I create a user_parameter that specifies a percentage. If I then use an Expression Evaluator to determine how many surfaces there are, I cannot pass on the result of the expression evaluator to the sampler. It only accepts user_parameter or a number. How can I put a calculated value in the Sampling Rate (n)?

 


geomancer
Evangelist
Forum|alt.badge.img+47
  • Evangelist
  • November 22, 2024

Can you use the AdvancedSampler from FME Hub, like ​@liamfez  suggests? In this transformer you can define a percentage to select, and you can define the selection should be randomized.

 


joepk
Influencer
Forum|alt.badge.img+20
  • Influencer
  • November 22, 2024

If the AdvancedSampler does not work you can try the following:

A RandomNumberGenerator/Sorter combination to randomize the order of your features. Use a Counter to give your features a _count attribute.
Using the total number of features (derived from e.g. joining the results of a StatisticsCalculator back to your input) you can calculate the number of features within your percentage. 
You can then use a Tester to check if feature's_count attribute is less than the number of features in your percentage.

You could probably do the latter part in a PythonCaller as well.

 


fme_hans
Contributor
Forum|alt.badge.img+5
  • Author
  • Contributor
  • November 26, 2024
The advanced sampler does not accept attributes, only user_parameters. And in that respect it is the same as the regular sampler. Because the data comes from an Oracle table, the easiest solution is a SQL query in which "percentage" is a user_parameter.

 

This does the trick for me:

 

SELECT 
    COL_1,
    COL_2,
    etc...
FROM (
    SELECT *
    FROM MY_TABLE
    where COL_1 = some value
    and ...
    ORDER BY DBMS_RANDOM.VALUE
)
WHERE 
    ROWNUM <= ( SELECT CEIL(COUNT(*) * $(Percentage)/100) 
                FROM MY_TABLE
                where COL_1 = some value
                and ...
              )

 

 


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings