Efficient overpass query for clustering nearby elements

Hi, I’m looking for a query that groups nearby elements (public transport stops) to sets.

The query below works but I wonder whether it can be improved because it slows down pretty quickly.

Ideally the foreach loop would not loop through already clustered elements, but from what I know this is currently not possible to avoid. Perhaps there is a “none-loop” approach I haven’t thought of yet.

[out:json][timeout:25][bbox:{{bbox}}];

// get all platforms
nwr[public_transport=platform]->.remaining_platforms;
// loop over every single platform element
foreach.remaining_platforms {
  // make intersection of current platform (default set ._) and .remaining_platforms
  // therefore only if the current platform is in remaining_platforms count will be 1
  // otherwise it has already been merged and therefore removed from remaining_platforms
  nwr._.remaining_platforms;
  if (count(nwr) > 0) {
    // find any nearby platforms
    // this loops for every newly found platform until no new platforms are found
    // 10 is the max number of iterations
    complete(10) -> .grouped {
      nwr.remaining_platforms(around:100);
    }
    // delete .grouped platforms from .remaining_platforms set
    (.remaining_platforms; - .grouped;) -> .remaining_platforms;
    // write to default set because make always reads from default set
    .grouped -> ._;
    // build a cluster element
    make zone 
      name=min(t["name"]),
      ::geom=gcat(geom());
      // output bbox only
    out bb;
  }
};

Thanks in advance.

1 Like