Skip to main content
Solved

Stratified random point in FME


hoa_le
Contributor
Forum|alt.badge.img+5
Hi everybody,

 

 

I have a polygon file which has ID code. I want to create random point for each ID code.

 

 

For example: each ID create 10 random points.

 

 

Could you tell me how can I do ? Thank you

Best answer by takashi

One approach.

 

Create aggregate polygons grouped by the forest status attribute (e.g. "status").

 

Create random points of enough large number within the bounding box for each aggregate feature.

 

Select the points which are inside of the original polygons based on their spatial relationships.

 

Select randomly points of required number (e.g. 10 or 15) from the points.

 

 

The point is how to compute the enough large number. My approach is to compute it based on area ratio between the original polygons and its bounding box.

 

Assuming that the required number is given as a user parameter named "NUM_POINTS":

 

 

1. Create random points within bounding box

 

1) Aggregator; Group By: status

 

2) AreaCaluculator; Area Attribute: _area1

 

3) BoundingBoxReplacer

 

4) AreaCalculator; Area Attribute: _area2

 

5) BoundsExtractor

 

6) 2DPointReplacer; XValue: _xmin, YValue: _ymin

 

7) Cloner; Number of Copies:

 

@int($(NUM_POINTS)*$(NUM_POINTS)*@double(@Value(_area2))/@Value(_area1)+0.5)

 

8) Offsetter (randomize location of every point)

 

X Offset: @rand()*(@Value(_xmax)-@Value(_xmin))

 

Y Offset: @rand()*(@Value(_ymax)-@Value(_ymin))

 

 

2. Filter the points

 

Then, filter the points by the original polygons using SpatialFilter (Group By: status).

 

 

3. Select randomly points of required number

 

Finally, select randomly points of required number from the filtered points.

 

1) Add a random number to every point (ExpressionEvaluator; use @rand function).

 

2) Sort the points by the random number.

 

3) Select first NUM_POINTS points for each status by Sampler.

 

Group By: status

 

Sampling Type: First N Features

 

Sampling Amount: $(NUM_POINTS)

 

 

Hope this works as expected.
View original
Did this help you find an answer to your question?
This post is closed to further activity.
It may be a question with a best answer, an implemented idea, or just a post needing no comment.
If you have a follow-up or related question, 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.

30 replies

gio
Contributor
Forum|alt.badge.img+15
  • Contributor
  • September 12, 2014
Hi,

 

 

U can use a vertextcreator set to a location (for instance x=0,y=0)

 

a cloner and a affiner.

 

 

 

replace creator in my picture by a vertexcreator.

hoa_le
Contributor
Forum|alt.badge.img+5
  • Author
  • Contributor
  • September 15, 2014
Hi Gio,

 

 

I am using FME 2013. It has not vertexcreator tool. It's only creator tool.

 

 

Are they the same?

gio
Contributor
Forum|alt.badge.img+15
  • Contributor
  • September 15, 2014
Hi U can use 2DPointReplacer in FME 2013 (this is now (part of) vertexcreator.

hoa_le
Contributor
Forum|alt.badge.img+5
  • Author
  • Contributor
  • September 15, 2014
Hi,

 

 

My input is polygon. I need create straitified random point for ldlr field. I want each filed has 10 random point.

 

 

I tried you asked, but I don't understand about processing . Could you explain for me? Thank you

 

 

My processing:

 

 

 

takashi
Influencer
  • September 15, 2014
Hi Hoa,

 

 

Do you mean creating random 10 points within each polygon?

 

I'm not sure how the ldlr field will be related to the required points.

 

Could you please draw a picture illustrating the expected output?

 

 

Takashi

gio
Contributor
Forum|alt.badge.img+15
  • Contributor
  • September 15, 2014
@ Takashi

 

 

 

Stratified Random Sampling is a statistical sampling strategy.

 

 

This site explains it for free:

 

https://explorable.com/probability-sampling

 

 

Greets

takashi
Influencer
  • September 15, 2014
Thanks Gio. Yes, I know stratified random sampling method.

 

But I cannot understand why/how the method can be applied to create random points in this case. In my understanding, that is a sampling method for statistic survey. I'm not sure what is the stratification for creating random points based on a polygon...

gio
Contributor
Forum|alt.badge.img+15
  • Contributor
  • September 15, 2014
A sampling method can also be spatial.

 

The files could be considered as strata for the sampling.

 

So all Hoa needs is a disproportional sampling of the polygons in the files.

 

 

In this case it appears to be 10 for each polygon file.  Not 10 per polygon.

 

So i suggested either using the ldlr range to pick from, or just the number of objects.

 

 

 

I assumed she is talking about files (with geometrytype polygon). 

 

A dynamic reader of sorts is required to proces all files of course.

 

 

But i do not know what "ldlr" is. So it might as wel be that the Ldlr attribute is a list or string of values, who knows? Well, Hoa does ;)

 

But to be sure, we would need some sample data i guess...;)

 

 

I also don't know which is surname Hoa or Le?

hoa_le
Contributor
Forum|alt.badge.img+5
  • Author
  • Contributor
  • September 15, 2014
hi Gio, Takashi

 

 

I'm glad because i get support to you. in my queson, maybe my english skill is not good so you cant understand. now, i will give an example to more understanding.

 

 

For example: I have a forest status (polygon) and it has 3 attribute such as rich forest, poor forest, barren land. now, i want to create a pointl file that have 10 random point rich f?rest, 10 point poor forest and 10 points barren land (attribute of point file get from forest status).

 

 

 

you can call me Hoa. hi

takashi
Influencer
  • September 15, 2014
Maybe you need to do random sampling to select 10 polygons for each forest status (rich / poor / barren). And then, the sampled polygons have to be transformed into representative points.

 

Is it correct?

 

 

If yes, the following is a possible way. Assuming that the polygon features have an attribute named "status" which stores the forest status.

 

 

1) Compute a random number (e.g. "_rand") with the ExpressionEvaluator for every forest polygon. @rand function can be used.

 

2) Sort the features by "_rand" (Sorter).

 

3) Select first 10 features for each status with the Sampler.

 

Group By: status

 

Sampling Type: First N Features

 

Sampling Amount: 10

 

 

Then, where should the representative points be located?

 

If the points should be inside of sampled forest polygons, consider using the InsidePointReplacer transformer.

hoa_le
Contributor
Forum|alt.badge.img+5
  • Author
  • Contributor
  • September 16, 2014
Hi, Takashi

 

 

Thank you for your suport. I think that's a method. BUT I see in case, if I have 3 polygons  rich forest, I use sampler tool it only create 3 polygons sample. But actualy, I need 10 points (It means 10 polygon rich forest). 

takashi
Influencer
  • September 16, 2014
No one can sample 10 polygons if there were 3 polygons only. Where can you pick the missing 7 polygons from?

hoa_le
Contributor
Forum|alt.badge.img+5
  • Author
  • Contributor
  • September 16, 2014
If in parameter sampler tool, I choice create 10 polygon for each status. But some status, I have only less 10 polygon (2,5,7...). In this case, output will less 10 polygon with that status.

 

 

When I used Quantum Gis to do that. Firstly, I split all polygon based on status. Then, I created random point for each status (For example: 15points). And I will assign attribute for them.

 

Finally, I merged all points file to one file which has information one point.

 

 

But in FME, how solution I can do that?

 

 

I draw a picture:

 

 

 


takashi
Influencer
  • September 16, 2014
In the output image, there seems to be some polygons containing 2 or more points.

 

Sorry, I cannot understand the rule that should be applied to create the points.

hoa_le
Contributor
Forum|alt.badge.img+5
  • Author
  • Contributor
  • September 16, 2014
Hi,

 

 

Yes. In my image, there are two status forest (multiple polygon).

 

 

In other GIS software (Arcgis, Qgis...), they have a tool to create random point for polygon.

 

It means if you want to create 10 points for polygon, they will product a point file which has 10 points.

 

 

In FME, I can't do that :(((((  

hoa_le
Contributor
Forum|alt.badge.img+5
  • Author
  • Contributor
  • September 16, 2014
Hi Gio, 

 

 

I tried as you talk. But I only do step cloner. Because I dont understand next step Cloner (pick (10) randomly: @round(@rand*@Value(_count)) or @round(@rand*@Value(_max)) ) how do it. Could you tell me how I used function to do that?

 

 

When I finish cloner function, I get result in picture. It has table which I want (It means one status has 10 points. But it has not geometry??? And it is not point???

 

 

Could you tell me how can I do? Thank you

 

 

 

 

 

 

 


takashi
Influencer
  • Best Answer
  • September 16, 2014
One approach.

 

Create aggregate polygons grouped by the forest status attribute (e.g. "status").

 

Create random points of enough large number within the bounding box for each aggregate feature.

 

Select the points which are inside of the original polygons based on their spatial relationships.

 

Select randomly points of required number (e.g. 10 or 15) from the points.

 

 

The point is how to compute the enough large number. My approach is to compute it based on area ratio between the original polygons and its bounding box.

 

Assuming that the required number is given as a user parameter named "NUM_POINTS":

 

 

1. Create random points within bounding box

 

1) Aggregator; Group By: status

 

2) AreaCaluculator; Area Attribute: _area1

 

3) BoundingBoxReplacer

 

4) AreaCalculator; Area Attribute: _area2

 

5) BoundsExtractor

 

6) 2DPointReplacer; XValue: _xmin, YValue: _ymin

 

7) Cloner; Number of Copies:

 

@int($(NUM_POINTS)*$(NUM_POINTS)*@double(@Value(_area2))/@Value(_area1)+0.5)

 

8) Offsetter (randomize location of every point)

 

X Offset: @rand()*(@Value(_xmax)-@Value(_xmin))

 

Y Offset: @rand()*(@Value(_ymax)-@Value(_ymin))

 

 

2. Filter the points

 

Then, filter the points by the original polygons using SpatialFilter (Group By: status).

 

 

3. Select randomly points of required number

 

Finally, select randomly points of required number from the filtered points.

 

1) Add a random number to every point (ExpressionEvaluator; use @rand function).

 

2) Sort the points by the random number.

 

3) Select first NUM_POINTS points for each status by Sampler.

 

Group By: status

 

Sampling Type: First N Features

 

Sampling Amount: $(NUM_POINTS)

 

 

Hope this works as expected.

gio
Contributor
Forum|alt.badge.img+15
  • Contributor
  • September 16, 2014
Hi Hoa,

 

 

I made a workspace to pick the polygons.

 

At the end u can atach a Insidepointreplacer.

 

 

 

 

 

 

 

This does the sampling and will always return 10 unique choices per stratum, if the stratum has more thne samplingsize objects.

 

 

It is a bit compex(ish) as it uses variablesetter- and retriever. These are to ensure selection of (in your case 10) unique samples from each stratum.

 

 

attach your data and set the Stratum_Attribute=@Value(ldlr ())

 

 

The output you can now use to create random points.

 

 

Do i understand correctly if i say each polygon is one object in the file?

 

Then u might as wel use a insidepointreplacer to create the points.

 

Or does your file contain overlappingforestareas? (then maybe a AreaOnArea would be an idea)

hoa_le
Contributor
Forum|alt.badge.img+5
  • Author
  • Contributor
  • September 17, 2014
Hi, Takashi

 

 

I do that to step 6. In the step 7: Cloner; Number of Copies:

 

@int($(NUM_POINTS)*$(NUM_POINTS)*@double(@Value(_area2))/@Value(_area1)+0.5)

 

NUM_POINTS means variable? I tried copy it to Number of Copies, the button OK is invisible.

 

 

 


takashi
Influencer
  • September 17, 2014
Check:

 

Did you define a user parameter named "NUM_POINTS"?

 

Did you specify attribute names "_area1" and "_area2" in the AreaCalculators?

 

Did you connect the Cloner to the data flow including the AreaCaluclators?

 

 

As I mentioned in the previous post, the steps are assuming that a user parameter named "NUM_POINTS" has been defined in the workspace. I intended that the parameter indicates the sampling amount. e.g. 10 or 15.

takashi
Influencer
  • September 17, 2014
See here to learn more about User Parameters.

 

Working with User Parameters (http://docs.safe.com/fme/html/FME_Workbench/Default.htm#published_private_parameters.htm)

hoa_le
Contributor
Forum|alt.badge.img+5
  • Author
  • Contributor
  • September 17, 2014
Hi, Takashi

 

 

I tried to do that. It's excellent...So great :). Thank you very much. 

 

 

I will try Gio's approach :)

 

 

Thank you

hoa_le
Contributor
Forum|alt.badge.img+5
  • Author
  • Contributor
  • September 17, 2014
Hi Takashi,

 

 

I checked and I saw some points is over polygon. I think , maybe replace spatialFilter by clip?

 

 

How do u feel? 

takashi
Influencer
  • September 17, 2014
Did you specify the "Group By" parameter for the SpatialFilter?

hoa_le
Contributor
Forum|alt.badge.img+5
  • Author
  • Contributor
  • September 17, 2014
Yes. I did that

 

 

 

 


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