point by line segment comparison
    merge polylines if points are close

def merge(polylines, mx_dist=4):
    """
    point by line segment comparison
    merge polylines if points are close
    """
    l = len(polylines)
    to_remove = set()
    for n in range(l - 1, -1, -1):
        if n not in to_remove:
            c = polylines[n]
            for p0, p1 in zip(c[:-1], c[1:]):
                # create a line from any subsegment:
                l0 = p0[0], p0[1], p1[0], p1[1]

                # for every other polyline:
                for m in range(l - 1, -1, -1):
                    if m not in to_remove:
                        if n == m:
                            continue
                        remove = False
                        cc = polylines[m]
                        ind = np.zeros(shape=cc.shape[0], dtype=bool)

                        # for every point p in this polyline:
                        for o in range(len(cc) - 1, -1, -1):
                            p = cc[o]

                            if line.segmentDistance(l0, p) < mx_dist:
                                remove = True
                                ind[o] = True

                        if remove:
                            polylines[n] = np.append(c, cc[ind], axis=0)
                            ind = ~ind
                            s = ind.sum()
                            if s < 2:
                                to_remove.add(m)
                            else:
                                polylines[m] = cc[ind]

    to_remove = sorted(to_remove)
    to_remove.reverse()
    for i in to_remove:
        polylines.pop(i)