Monday, May 23, 2011

PostGIS function for a random point inside a polygon -- REVISED

This is a revised random-point-in-polygon function first discussed in my earlier post. It turns out that in some weird cases (multipolygons consisting of a number of small and disperse polygons) the function takes too many iterations to complete. Below is a code for another function called RandomPointMulti that addresses this problem. RandomPointMulti breaks a multipolygon into polygons, randomly chooses a polygon with the probability proportional to the area of the polygon, and then calls original RandomPoint on that polygons. RandomPoint was also modified: some typos have been fixed, exception is thrown if the max number of iterations has been exceeded, and and max number of iterations can be specified as an argument.

No comments: