简体   繁体   English

POSTGIS:查找多边形内的所有点

[英]POSTGIS: find all the points within a polygon

I have a table containing point 我有一个包含点的表

CREATE TABLE Points
{
  pointID BIGSERIAL PRIMARY KEY NOT NULL,
  thePoint GEOGRAPHY(POINT)
}

CREATE TABLE Polygons
{
  polygonID BIGSERIAL PRIMARY KEY NOT NULL,
  aPolygon GEOGRAPHY(POLYGON) NOT NULL,
}

I wish to find all the points that are contained in each polygon. 我希望找到每个多边形中包含的所有点。 ie the result should look like 即结果应该看起来像

polygonID| pointID
-------------------
1        | 1
1        | 2
1        | 5
1        | 7
2        | 2
2        | 3
...

I managed to go point by point and to figure out if it's in the polygon using ST_CoveredBy(thePoint, aPolygon) . 我设法使用ST_CoveredBy(thePoint, aPolygon)一点一点地找出它是否在多边形中。 Based on that the naive solution is going in a nested loop over all points and polygons but for sure there is a more efficient and correct way to achieve this. 基于此,幼稚的解决方案将在所有点和多边形上进行嵌套循环,但是可以肯定的是,有一种更有效,更正确的方法来实现这一目标。

Here's one way, which works on geography types. 这是一种对地理类型起作用的方法。 BTW, might be worth reading the manual on geometry and geography data types. 顺便说一句,也许值得阅读有关几何和地理数据类型的手册 As far as I understand it, there are many more functions available for geometries, but you have to get involved with projections. 据我了解,还有更多可用于几何的功能,但是您必须参与投影。 The best choice depends on what you're doing... 最好的选择取决于您在做什么...

SELECT polygonID, pointID
  FROM Points INNER JOIN Polygons 
  ON ST_covers(polygons.aPolygon,Points.thePoint  );

PostgreSQL有polygon @> point

select * from points join polygons on polygons.aPolygon @> points.thePoint;

自从我对PostGIS进行任何操作以来已经过去了一段时间,但是我将尝试一下。

SELECT polygonID, pointID FROM Points, Polygons WHERE ST_CONTAINS(Points.thePoint , polygonID.aPolygon);

The answer was sort-of in your question: "within". 答案是在您的问题中:“在...内”。 Use the ST_DWithin operator . 使用ST_DWithin运算符

SELECT polygonID, pointID
FROM Points
JOIN Polygons ON ST_DWithin(Points.thePoint, polygons.aPolygon, 0);

The last argument 0 is the distance within the polygon. 最后一个参数0是多边形内的距离。 This is useful to also select points that are, for example, within 10 m of the polygon, which is useful if there are positioning errors in the source data. 这对于选择例如位于多边形10 m以内的点也很有用,如果源数据中存在定位错误,这将很有用。

ST_Intersects(Points.thePoint, polygons.aPolygon) should also work. ST_Intersects(Points.thePoint, polygons.aPolygon)也应该起作用。

See DE-9IM if you want to learn more on what these operators mean, but not all have geography type equivalents. 如果您想了解更多关于这些运算符的含义的信息,请参阅DE-9IM ,但并非所有人都具有对应的地理位置类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM