Last active
April 17, 2017 07:21
-
-
Save jaantollander/e17bb572ae42355868f21243b81458bd to your computer and use it in GitHub Desktop.
Converting shapely geometries to other objects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""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