Skip to content

Instantly share code, notes, and snippets.

@jaantollander
Last active April 17, 2017 07:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaantollander/e17bb572ae42355868f21243b81458bd to your computer and use it in GitHub Desktop.
Save jaantollander/e17bb572ae42355868f21243b81458bd to your computer and use it in GitHub Desktop.
Converting shapely geometries to other objects
"""Convert shapely geometries to other objects
Other interfaces
- matplotlib: PathPatch
- pyqtgraph: PlotItem
- bokeh: Glyph
- scikit-image: Indices on a grid
References
- https://bitbucket.org/sgillies/descartes/
"""
from shapely.geometry.base import BaseMultipartGeometry
from shapely.geometry.linestring import LineString
from shapely.geometry.point import Point
from shapely.geometry.polygon import Polygon
class Adapter(object):
def __init__(self, geom):
self.geom = geom
class PointAdapter(Adapter):
"""Adapt shapely points"""
pass
class LineStringAdapter(Adapter):
"""Adapt shapely linestrings"""
pass
class PolygonAdapter(Adapter):
"""Adapt shapely polygons
* interior
* exterior
"""
pass
def converter(geom):
if isinstance(geom, Point):
yield PointAdapter(geom)
elif isinstance(geom, LineString):
yield LineStringAdapter(geom)
elif isinstance(geom, Polygon):
yield PolygonAdapter(geom)
elif isinstance(geom, BaseMultipartGeometry):
for gen in (converter(geo) for geo in geom):
yield from gen
else:
raise TypeError
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment