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 (e.g. 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.
Another method is to use 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With