PostGIS-在直线或多边形内获取点


10

我需要获取直线或多边形的中心点才能制作marker我的应用。这样,当您单击标记时,就会显示几何图形(直线或多边形)。我曾经ST_Centroid使它工作。

结果是我所期望的,使某些多边形或线的质心超出了几何形状。我想要这些线或多边形是在几何体内部获得“最中心点”。

我怎样才能做到这一点?有解决方案吗?

Answers:



7

就我而言,我在离散表中有每个几何。我所做的是:

  1. 对于线 -> ST_LineInterpolatePoint()0.5因子。
  2. 对于多边形 ->测试是否ST_Centroid()在其几何形状内。如果是这样,ST_Centroid()则是最佳选择,否则,我会选择PointOnSurface()

这是查询:

SELECT
    CASE WHEN (SELECT the_geom FROM points WHERE gid = d.gid) IS NOT NULL
    THEN (SELECT the_geom FROM points WHERE gid = d.gid)
    WHEN (SELECT the_geom FROM lines WHERE gid = d.gid) IS NOT NULL
    THEN ST_LineInterpolatePoint((SELECT the_geom FROM lines WHERE gid = d.gid), 0.5)
    WHEN (SELECT the_geom FROM polygons WHERE gid = d.gid AND ST_Intersects(ST_Centroid(the_geom),the_geom)) IS NOT NULL
    THEN ST_Centroid((SELECT the_geom FROM polygons WHERE gid = d.gid))
    ELSE ST_PointOnSurface((SELECT the_geom FROM polygons WHERE gid = d.gid))
    END AS center
FROM someTable d

5
&&运算符仅检查bbox。您可能要使用ST_intersects()。
WKT

你是对的。我错了。我将更新我的答案。谢谢!
何塞·埃莫西利亚·罗德里戈
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.