Skip to content

Instantly share code, notes, and snippets.

@sorokine
Last active December 30, 2015 06:38
Show Gist options
  • Save sorokine/7790185 to your computer and use it in GitHub Desktop.
Save sorokine/7790185 to your computer and use it in GitHub Desktop.

Revisions

  1. sorokine revised this gist Dec 4, 2013. 1 changed file with 7 additions and 7 deletions.
    14 changes: 7 additions & 7 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -1,24 +1,24 @@
    # Random point in polygon in PostGIS

    Existing function for generating of randon point in polygon in PostGIS may take a very long time in narow and long polygons because the functions have to test lots of points that fall within bounding rectangle but outside actual polygon.
    Existing function for generating of random point in polygon in PostGIS may take a very long time in narrow and long polygons because the functions have to test lots of points that fall within bounding rectangle but outside actual polygon. Examples are [here](https://github.com/pedrogit/postgisaddons/) and [here](http://sorokine.blogspot.com/2011/05/postgis-function-for-random-point.html).

    The idea is to use a funtion to subdivide bounding rectangle recursively and process only tiles that ovrlap with the polygon:
    The idea is to use a function to subdivide bounding rectangle recursively and process only tiles that overlap with the polygon:

    1. subdivide the bounding rectangle into smaller rectangles (2, 4, or more, whichever is easier)
    2. randomly pick a rectangle
    3. create an interesection of the rectangle with the original polygon, return to the previous step if intersection is empty
    4. check if the ratio between the areas of the rectangle and interesection is big enough
    * ratio 1:10 means that search for a ppoint in polygon will take roughly 10 interations
    3. create an intersection of the rectangle with the original polygon, return to the previous step if intersection is empty
    4. check if the ratio between the areas of the rectangle and intersection is big enough
    * ratio 1:10 means that search for a point in polygon will take roughly 10 iterations
    5. if ratio is small enough return the result of normal random point function

    One more idea is to modify the point-in-polygon algorithm:

    1. Drop a random point inside the bounding rectangle
    2. if it is inside the polygon return the result, if not go to the next step
    3. create a straight line between the original point and a random point on the boundary of the binding rectangle (this can be done by either choosing a random direction and then finding intersection with the binfing rectangle or by selecting a point on the boundary rectnagle within a random distance from one of the rectangle corners)
    3. create a straight line between the original point and a random point on the boundary of the binding rectangle (this can be done by either choosing a random direction and then finding intersection with the binding rectangle or by selecting a point on the boundary rectangle within a random distance from one of the rectangle corners)
    4. clip the line with the polygon, repeat the previous step if clipped line is empty
    5. random select a segment of the clipped line
    6. create a point on the segment at a random distance from the segment starting vertex (is there a finction in PostGIS for that?)
    6. create a point on the segment at a random distance from the segment starting vertex (is there a function in PostGIS for that?)

    Or is it possible to rig polygon centroid function to produce a random point instead of the centroid?

  2. sorokine revised this gist Dec 4, 2013. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -20,4 +20,6 @@ One more idea is to modify the point-in-polygon algorithm:
    5. random select a segment of the clipped line
    6. create a point on the segment at a random distance from the segment starting vertex (is there a finction in PostGIS for that?)

    Or is it possible to rig polygon centroid function to produce a random point instead of the centroid?
    Or is it possible to rig polygon centroid function to produce a random point instead of the centroid?

    Plus there should be an option to create many random points inside a polygon in one step.
  3. sorokine revised this gist Dec 4, 2013. 1 changed file with 5 additions and 3 deletions.
    8 changes: 5 additions & 3 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,8 @@
    # Random point in polygon in PostGIS

    Existing function for generating of randon point in polygon in PostGIS may take a very long time in narow and long polygons because they have to test lots of point that fall within bounding rectangle but outside those polygon.
    Existing function for generating of randon point in polygon in PostGIS may take a very long time in narow and long polygons because the functions have to test lots of points that fall within bounding rectangle but outside actual polygon.

    The idea is to use a recursive funtion that would
    The idea is to use a funtion to subdivide bounding rectangle recursively and process only tiles that ovrlap with the polygon:

    1. subdivide the bounding rectangle into smaller rectangles (2, 4, or more, whichever is easier)
    2. randomly pick a rectangle
    @@ -11,11 +11,13 @@ The idea is to use a recursive funtion that would
    * ratio 1:10 means that search for a ppoint in polygon will take roughly 10 interations
    5. if ratio is small enough return the result of normal random point function

    One more idea:
    One more idea is to modify the point-in-polygon algorithm:

    1. Drop a random point inside the bounding rectangle
    2. if it is inside the polygon return the result, if not go to the next step
    3. create a straight line between the original point and a random point on the boundary of the binding rectangle (this can be done by either choosing a random direction and then finding intersection with the binfing rectangle or by selecting a point on the boundary rectnagle within a random distance from one of the rectangle corners)
    4. clip the line with the polygon, repeat the previous step if clipped line is empty
    5. random select a segment of the clipped line
    6. create a point on the segment at a random distance from the segment starting vertex (is there a finction in PostGIS for that?)

    Or is it possible to rig polygon centroid function to produce a random point instead of the centroid?
  4. sorokine revised this gist Dec 4, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    # Random point in polygon in PostGIS

    Existing function for generating of randon point in polygon in PostGIS may take a very long time in narow and long polygons because they jave to test lots of point that fall within bounding rectangle but outside those polygon.
    Existing function for generating of randon point in polygon in PostGIS may take a very long time in narow and long polygons because they have to test lots of point that fall within bounding rectangle but outside those polygon.

    The idea is to use a recursive funtion that would

  5. sorokine created this gist Dec 4, 2013.
    21 changes: 21 additions & 0 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    # Random point in polygon in PostGIS

    Existing function for generating of randon point in polygon in PostGIS may take a very long time in narow and long polygons because they jave to test lots of point that fall within bounding rectangle but outside those polygon.

    The idea is to use a recursive funtion that would

    1. subdivide the bounding rectangle into smaller rectangles (2, 4, or more, whichever is easier)
    2. randomly pick a rectangle
    3. create an interesection of the rectangle with the original polygon, return to the previous step if intersection is empty
    4. check if the ratio between the areas of the rectangle and interesection is big enough
    * ratio 1:10 means that search for a ppoint in polygon will take roughly 10 interations
    5. if ratio is small enough return the result of normal random point function

    One more idea:

    1. Drop a random point inside the bounding rectangle
    2. if it is inside the polygon return the result, if not go to the next step
    3. create a straight line between the original point and a random point on the boundary of the binding rectangle (this can be done by either choosing a random direction and then finding intersection with the binfing rectangle or by selecting a point on the boundary rectnagle within a random distance from one of the rectangle corners)
    4. clip the line with the polygon, repeat the previous step if clipped line is empty
    5. random select a segment of the clipped line
    6. create a point on the segment at a random distance from the segment starting vertex (is there a finction in PostGIS for that?)