Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.Sign up
Add functionality to drop 3rd dimension #709
There is an old question on GIS Stack Exchange about converting 3D geometries to 2D: Convert 3D WKT to 2D Shapely Geometry. I think this functionality should be included in the Shapely, so we could use it, for example, like:
>>> from shapely.geometry import Polygon >>> p = Polygon([(0, 0, 0), (1, 0, 0), (1, 1, 0)]) >>> p.wkt 'POLYGON Z ((0 0 0, 1 0 0, 1 1 0, 0 0 0))' >>> p2 = p.drop_z >>> p2.wkt 'POLYGON ((0 0, 1 0, 1 1, 0 0))'
I've seen in one of the answers and in the docs that this operation is not necessary as the 3rd dimension has no effect on geometric analysis. But when implementing my own function to determine left side of a split geometry (this is not yet implemented: #589), I saw that this functionality could come handy:
from shapely.geometry import (LinearRing, LineString, Polygon) def is_left(polygon: Polygon, line: LineString) -> bool: """ Determines if the polygon is on the left side of the line according to: https://stackoverflow.com/questions/50393718/determine-the-left-and-right-side-of-a-split-shapely-geometry """ ring = LinearRing([*line.coords, *polygon.centroid.coords]) return ring.is_ccw
This code will fail for 3D geometries:
p = Polygon([(0, 0, 0), (1, 0, 0), (1, 1, 0)]) l = LineString([(0, 0, 0), (1, 0, 0)]) is_left(p, l)
will give this error:
This is due to the fact that centroid of a
If there was a
Shapely version: 1.6.4.post1, installed from conda.
For other people arriving here from Google, I found a very simple method:
def _to_2d(x, y, z): return tuple(filter(None, [x, y])) new_shape = shapely.ops.transform(_to_2d, shape)