I have polygon feature and want to be able to generate points inside it. I need this for one classification task.
Generating random points until one is inside the polygon wouldn't work because it's really unpredictable the time it takes.
|
|
Start by decomposing the polygon into triangles, then generate points inside those. (For a uniform distribution, weight each triangle by its area.) |
|||||||||||||||
|
|
As you put a QGIS tag on this question: Random Points tool can be used with a boundary layer.
If you are looking for code, the underlying plugin source code should be of help. |
|||
|
|
|
You could determine the extent of the polygon, then constrain the random number generation for X and Y values within those extents. Basic process: 1) Determine maxx, maxy, minx, miny of polygon vertices, 2) Generate random points using these values as bounds 3) Test each point for intersection with your polygon, 4) Stop generating when you have enough points satisfying the intersection test Here is an algorithm (C#) for the intersection test:
|
||||
|
|
|
If R is an option, see |
|||
|
|
I would like to offer a solution that requires very little in terms of GIS analysis. In particular, it does not require triangulating any polygons. The following algorithm, given in pseudocode, refers to some simple operations in addition to basic list handling capabilities (create, find length, append, sort, extract sublists, and concatenate) and generation of random floats in the interval [0, 1):
These are all available in almost any GIS or graphics programming environment (and easy to code if not). Procedure SRS can be "tuned" by changing the parameter
The next procedure calls itself recursively if necessary. The mysterious expression
|
||||
|
|