Postgis多边形内的随机点

发布于 2024-09-11 18:01:08 字数 45 浏览 1 评论 0原文

如果我在 Postgis 中有一个多边形,我如何找到/计算多边形内的随机点?

If i have a polygon in Postgis how can i find/calculate random points inside the polygon?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无边思念无边月 2024-09-18 18:01:08

@Mike 引用的链接没有代码,但 Dr.JTS 提供了很好的线索:
“点密度”地图...“本质上,这涉及创建一组位于给定多边形内的 N 个随机放置的点”。函数执行此操作:输入是多边形,输出是随机点。

这些链接具有相同的 SQL/PostGIS 函数 RandomPoint(Geometry)sorokine 2011osgeo.org/postgis/维基百科。第二个链接 (wiki) 更完整,解释并显示了示例,以及作为问题答案的函数 RandomPointsInPolygon(geometry,integer)

扩展 solion 以输入“每个区域的点密度”,或点之间的平均距离:

 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 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;
寂寞清仓 2024-09-18 18:01:08

Postgis 2.3.0 及更高版本有一个新函数,可以将点生成到多边形ST_GeneratePoints

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文