team-3/src/c3nav/routing/utils/mpl.py

30 lines
874 B
Python
Raw Normal View History

2016-12-03 19:09:39 +01:00
from matplotlib.path import Path
2016-12-07 16:11:33 +01:00
from c3nav.mapdata.utils.geometry import assert_multipolygon
2016-12-03 19:09:39 +01:00
def polygon_to_mpl_paths(polygon):
"""
convert a shapely Polygon or Multipolygon to a matplotlib Path
:param polygon: shapely Polygon or Multipolygon
:return: matplotlib Path
"""
paths = []
for polygon in assert_multipolygon(polygon):
paths.append(linearring_to_mpl_path(polygon.exterior))
for interior in polygon.interiors:
paths.append(linearring_to_mpl_path(interior))
return paths
def linearring_to_mpl_path(linearring):
vertices = []
codes = []
coords = list(linearring.coords)
vertices.extend(coords)
vertices.append(coords[0])
codes.append(Path.MOVETO)
codes.extend([Path.LINETO] * (len(coords)-1))
codes.append(Path.CLOSEPOLY)
return Path(vertices, codes, readonly=True)