Skip to content

Instantly share code, notes, and snippets.

@pmbaumgartner
Created December 28, 2021 12:23
Show Gist options
  • Save pmbaumgartner/90ff0a13baf8cfb431bcdfea7d541106 to your computer and use it in GitHub Desktop.
Save pmbaumgartner/90ff0a13baf8cfb431bcdfea7d541106 to your computer and use it in GitHub Desktop.
import random
from typing import List, Union
import shapely.geometry as geo
from tqdm import tqdm
MultiLineStringType = Union[List[geo.LineString], geo.MultiLineString]
def overlap_lines(
lines: MultiLineStringType,
buffer: float,
min_length: float = 0,
sort: bool = True,
shuffle: bool = True,
cap_style: geo.CAP_STYLE = 1,
resolution: int = 16,
):
"""
This function takes a list of lines and adds a visual effect of a
z-layer to make them look overlapping. It will shuffle inputs by default.
"""
lines = [l for l in lines if l.length > min_length]
lines = list(sorted(lines, key=lambda l: l.length, reverse=True)) if sort else lines
if shuffle:
random.shuffle(lines)
last_buffer = lines[0].buffer(buffer, cap_style=cap_style, resolution=resolution)
geometries = []
for line in tqdm(lines, desc="Buffering & Unioning"):
new_geo = line.difference(last_buffer)
if new_geo.is_empty:
continue
line_buffer = line.buffer(buffer, cap_style=cap_style, resolution=resolution)
last_buffer = last_buffer.union(line_buffer)
geometries.append(new_geo)
return geometries
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment