Last active
January 9, 2020 08:58
-
-
Save ThomasG77/cad711667942826edc70 to your computer and use it in GitHub Desktop.
Just one method to convert 2D to 3D geometry with shapely
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
# -*- coding: utf-8 -*- | |
from shapely.geometry import LineString | |
from shapely import wkt | |
line = LineString([(0, 0, 5), (1, 1, 5)]) | |
# memo for 3D | |
wkt3D = line.wkt | |
# 2D | |
wkt2D = line.to_wkt() | |
geometry2D = wkt.loads(wkt2D) | |
print geometry2D.wkt |
This should be 3D -> 2D, like a hack, the to_wkt
method is deprecated
I think a better approach is to use shapely.ops.transform
.
import shapely.ops
p = Point(2, 3) # no z
pz = shapely.ops.transform(lambda x, y: (x, y, 2), p) # now pz is p with a z coordinate
This can also be used to convert other geometries. And the other way around, to remove the third coordinate:
l = shapely.geometry.LineString(([2, 3, 6], [4, 5, -1]))
shapely.ops.transform(lambda x, y, z=None: (x, y), l).wkt # 'LINESTRING (2 3, 4 5)'
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is converting 3D to 2D and not the other way, right?