4

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?

  • 1
    I don't think this is possible, and I'm not sure what the end result you're wanting would be. Do you want every point to reside in a cluster? Can points be in their own clusters? Is success the fewest clusters? Or the densest cluster? – Evan Carroll Nov 29 '17 at 17:15
  • couldn't you apply a ST_ClusterDBSCAN on the table, calculating clusterIDs based on max core distance of 4 (eps parameter) and maybe only 1 point to reference to (minpoint parameter)? – ThingumaBob Nov 30 '17 at 8:56
  • Evan Carroll: I don't need every point to be in a cluster. I just need ones that are close to each other to be in a cluster. Basically, the problem is that I need a list of points sorted by proximity to a point P, but there's not enough resolution for certainty and it's easier to say "Well these 4 points are within 0.25 miles of each other so they're basically in the same location". – jyurek Nov 30 '17 at 13:13
  • Thingamabob: I also need distances to centroids,which I can't get from a ST_ClusterDBSCAN – jyurek Nov 30 '17 at 13:14
  • giving it a second thought, ST_ClusterDBSCAN with eps as distance and minpoints = 1 should in fact be equal to ST_ClusterWithin... reading your comment on @EvanCarroll, I have to agree, that is unlikely to work without any form of initial cluster definition other than just being some distance from each other... – ThingumaBob Nov 30 '17 at 18:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.