Skip to content

Instantly share code, notes, and snippets.

@ThomasG77
Last active January 9, 2020 08:58
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ThomasG77/cad711667942826edc70 to your computer and use it in GitHub Desktop.
Save ThomasG77/cad711667942826edc70 to your computer and use it in GitHub Desktop.
Just one method to convert 2D to 3D geometry with shapely
# -*- 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
@fhchl
Copy link

fhchl commented Sep 13, 2017

This is converting 3D to 2D and not the other way, right?

@DeoLeung
Copy link

This should be 3D -> 2D, like a hack, the to_wkt method is deprecated

@Gijs-Koot
Copy link

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