简体   繁体   English

Postgis - 与ST_Within相反

[英]Postgis – Opposite of ST_Within

I'm trying to get all points that are NOT inside a few polygons: 我试图获得不在几个多边形内的所有点:

SELECT pt.geom 
FROM points pt, polygons po
WHERE ST_Within(pt.geom, po.geom) = false;

But doesn't work – I get all the points instead. 但是不起作用 - 我得到所有的积分。

The reason why you see "all the points" is that you have more than one point and more than one polygon. 您看到“所有点”的原因是您有多个点和多个多边形。 If you had only one of either, it would work. 如果你只有其中一个,它会工作。

There are actually several ways to solve this one, but this is probably the most simple and quickest. 实际上有几种方法可以解决这个问题,但这可能是最简单,最快捷的方法。

If you have a primary or unique key (eg gid ): 如果您有主键或唯一键(例如gid ):

SELECT pt.*
FROM points pt
WHERE pt.gid NOT IN (
    SELECT pt.gid FROM points pt, polygons po
    WHERE ST_Within(pt.geom, po.geom)
);

note that it will also return any points with NULL geometries. 请注意,它还将返回任何具有NULL几何的点。

Another method is to use EXCEPT : 另一种方法是使用EXCEPT

SELECT pt.*
FROM points pt
EXCEPT SELECT pt.*
FROM points  pt, polygons po
WHERE ST_Within(pt.geom, po.geom);

And yet another method is to use ST_Union to fuse all the polygons into one large geometry, but this method would be much much slower. 另一种方法是使用ST_Union将所有多边形融合成一个大的几何体,但这种方法要慢得多。

SELECT pt.geom 
FROM points pt, polygons po
WHERE ST_Within(pt.geom, po.geom)

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

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