shapely <-> paths

This commit is contained in:
Michael Fogleman 2018-02-26 21:42:49 -05:00
parent 7c86ce20d2
commit c3ca0b65bc
2 changed files with 25 additions and 0 deletions

View File

@ -9,7 +9,9 @@ from .paths import (
load_paths,
path_length,
paths_length,
paths_to_shapely,
quadratic_path,
shapely_to_paths,
simplify_path,
simplify_paths,
sort_paths,

View File

@ -156,3 +156,26 @@ def expand_quadratics(path):
else:
raise Exception('invalid point: %r' % point)
return result
def paths_to_shapely(paths):
# TODO: Polygons for closed paths?
return geometry.MultiLineString(paths)
def shapely_to_paths(g):
if isinstance(g, geometry.Point):
return []
elif isinstance(g, geometry.LineString):
return [list(g.coords)]
elif isinstance(g, (geometry.MultiPoint, geometry.MultiLineString, geometry.MultiPolygon, geometry.collection.GeometryCollection)):
paths = []
for x in g:
paths.extend(shapely_to_paths(x))
return paths
elif isinstance(g, geometry.Polygon):
paths = []
paths.append(list(g.exterior.coords))
for interior in g.interiors:
paths.extend(shapely_to_paths(interior))
return paths
else:
raise Exception('unhandled shapely geometry: %s' % type(g))