code: def connect(polylines, max_dist=10):
      
      ll = len(polylines)
      remove = []
      for n in range(ll - 1, -1, -1):
          c = polylines[n]
          if len(c) > 1:
              for d0, p0 in enumerate((c[0, 0], c[-1, 0])):
                  for m in range(len(polylines) - 1, -1, -1):
                      # combine all x all polylines
                      if n == m:
                          continue
                      cc = polylines[m]
  
                      for d1, p1 in enumerate((cc[0, 0], cc[-1, 0])):
                          # for end points of other polylines:
                          # measure closest distance for current polyline
                          ndist = norm(p0 - p1)
                          if ndist < max_dist:
                              is_same, c = _connect(c, cc, d0, d1)
                              if is_same:
                                  if m not in remove:
                                      remove.append(m)
                                  polylines[n] = c
                                  break
      # remove those which are already in connected to other polyline:
      remove.sort()
      remove.reverse()
      for r in remove:
          polylines.pop(r)
docs: connect polylines that are close and have a similar orientation
       o---o  <->  o---o  ==> o----o--o----o
      TODO: max_dist as function of cell size