开发者

Postgis random point inside a polygon

If i have a polygon in Postgis how can i fin开发者_StackOverflow社区d/calculate random points inside the polygon?


The link cited by @Mike not have a code, but good clues from Dr.JTS: "dot-density" maps... "Essentially this involves creating a set of N randomly-placed points which lie within a given polygon". A function do this: the input is the polygon, the output the random points.

These links have the same SQL/PostGIS function RandomPoint(Geometry): sorokine 2011 and osgeo.org/postgis/wiki. The second link (wiki) is more complete, explaning and showing examples, and a function RandomPointsInPolygon(geometry,integer) that is an answer to the problem.

Extending the solion to input "density of points per area", or average distance between points:

 CREATE OR REPLACE FUNCTION RandomPointsInPolygon(
     geom geometry,                -- the polygon
     avg_dist float DEFAULT 20.0,  -- average of 20 units between points 
     min_pts integer DEFAULT 1,    -- min. of points
     max_pts integer DEFAULT 1000  -- max. of points
 ) RETURNS SETOF geometry AS 
 $$
   SELECT CASE WHEN npts=1 THEN ST_Centroid($1) 
               ELSE RandomPointsInPolygon($1,npts) 
          END
   FROM (
     SELECT CASE WHEN d<$3 THEN $3 WHEN d>$4 THEN $4 ELSE d END AS npts
     FROM (SELECT (st_area($1)/(pi()*($2/2.0)^2))::integer AS d) AS t
   ) AS t2;
 $$ LANGUAGE SQL;


The Postgis version 2.3.0 and upper have a new function to generate points into polygon ST_GeneratePoints.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜