First, PostGIS doesn't have a notion of a circle for the purposes of math. It's a polygon. You can store circles as
- circlestrings
- or centroids (points) with radius
I assume you're storing them as centroids. If so, it's easy use ST_DWithin()
. Assuming you're using geography, the radius is in meters this will find all the points that are uniquely in one circle (don't overlap).
SELECT points.id, count(*)
FROM poi
JOIN points
ON ST_DWithin( poi.geom::geog, points.geom::geog, radiusInMeters )
GROUP BY points.id
HAVING count(*) = 1;
This will find all circles that have the most non-overlapping points.. We wrap the above in EXISTS
to find the ones that don't overlap.
SELECT poi.id, count(*)
FROM poi
JOIN points
ON ST_DWithin( poi.geom::geog, points.geom::geog, radiusInMeters )
GROUP BY poi.id
WHERE EXISTS (
SELECT 1
FROM poi
JOIN points AS p2
ON ST_DWithin( poi.geom::geog, p2.geom::geog, radiusInMeters )
GROUP BY p2.id
HAVING count(*) = 1
WHERE p2.id = points.id // points.id is the outter.
);