def connect(polylines, max_dist=<NUM_LIT:10>):
ll = len(polylines)<EOL>remove = []<EOL>for n in range(ll - <NUM_LIT:1>, -<NUM_LIT:1>, -<NUM_LIT:1>):<EOL><INDENT>c = polylines[n]<EOL>if len(c) > <NUM_LIT:1>:<EOL><INDENT>for d0, p0 in enumerate((c[<NUM_LIT:0>, <NUM_LIT:0>], c[-<NUM_LIT:1>, <NUM_LIT:0>])):<EOL><INDENT>for m in range(len(polylines) - <NUM_LIT:1>, -<NUM_LIT:1>, -<NUM_LIT:1>):<EOL><INDENT>if n == m:<EOL><INDENT>continue<EOL><DEDENT>cc = polylines[m]<EOL>for d1, p1 in enumerate((cc[<NUM_LIT:0>, <NUM_LIT:0>], cc[-<NUM_LIT:1>, <NUM_LIT:0>])):<EOL><INDENT>ndist = norm(p0 - p1)<EOL>if ndist < max_dist:<EOL><INDENT>is_same, c = _connect(c, cc, d0, d1)<EOL>if is_same:<EOL><INDENT>if m not in remove:<EOL><INDENT>remove.append(m)<EOL><DEDENT>polylines[n] = c<EOL>break<EOL><DEDENT><DEDENT><DEDENT><DEDENT><DEDENT><DEDENT><DEDENT>remove.sort()<EOL>remove.reverse()<EOL>for r in remove:<EOL><INDENT>polylines.pop(r)<EOL><DEDENT>
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