def merge(polylines, mx_dist=<NUM_LIT:4>):
l = len(polylines)<EOL>to_remove = set()<EOL>for n in range(l - <NUM_LIT:1>, -<NUM_LIT:1>, -<NUM_LIT:1>):<EOL><INDENT>if n not in to_remove:<EOL><INDENT>c = polylines[n]<EOL>for p0, p1 in zip(c[:-<NUM_LIT:1>], c[<NUM_LIT:1>:]):<EOL><INDENT>l0 = p0[<NUM_LIT:0>], p0[<NUM_LIT:1>], p1[<NUM_LIT:0>], p1[<NUM_LIT:1>]<EOL>for m in range(l - <NUM_LIT:1>, -<NUM_LIT:1>, -<NUM_LIT:1>):<EOL><INDENT>if m not in to_remove:<EOL><INDENT>if n == m:<EOL><INDENT>continue<EOL><DEDENT>remove = False<EOL>cc = polylines[m]<EOL>ind = np.zeros(shape=cc.shape[<NUM_LIT:0>], dtype=bool)<EOL>for o in range(len(cc) - <NUM_LIT:1>, -<NUM_LIT:1>, -<NUM_LIT:1>):<EOL><INDENT>p = cc[o]<EOL>if line.segmentDistance(l0, p) < mx_dist:<EOL><INDENT>remove = True<EOL>ind[o] = True<EOL><DEDENT><DEDENT>if remove:<EOL><INDENT>polylines[n] = np.append(c, cc[ind], axis=<NUM_LIT:0>)<EOL>ind = ~ind<EOL>s = ind.sum()<EOL>if s < <NUM_LIT:2>:<EOL><INDENT>to_remove.add(m)<EOL><DEDENT>else:<EOL><INDENT>polylines[m] = cc[ind]<EOL><DEDENT><DEDENT><DEDENT><DEDENT><DEDENT><DEDENT><DEDENT>to_remove = sorted(to_remove)<EOL>to_remove.reverse()<EOL>for i in to_remove:<EOL><INDENT>polylines.pop(i)<EOL><DEDENT>
point by line segment comparison
merge polylines if points are close