I have a set of location points and I want to find clusters within the set. This is fine. However, I don't want to get two points in the same cluster that are farther apart than the maximum distance, even if those points are close enough to other points in the set.
As a fairly trivial example, I have this query:
WITH testvals (geom) AS (SELECT unnest(ARRAY[ ST_MakePoint(1, 1), ST_MakePoint(2, 1), ST_MakePoint(5, 1), ST_MakePoint(9, 1), ST_MakePoint(10, 1)])), clusters(geom) AS (SELECT ST_CollectionExtract(unnest(ST_ClusterWithin(geom, 4)), 1) FROM testvals) select ST_astext(geom) from clusters;
This gives the result of
st_astext .................................. MULTIPOINT(1 1,2 1,5 1,9 1,10 1)
But the point (1,1) and (10,1) are way more than 4 units away from each other, even if the distance can be bridged by walking to points that are 4 or less units away.
Is there a way to generate clusters with PostGIS that don't have points that are too far away from any other point in the cluster?