Saturday 22 October 2016

gdal - How do I convert WKT geometry from 3D to 2D


I'm working with kml files using Python gdal.ogr module. When I extract feature's geometry using feature.GetGeometryRef(), I get:


POLYGON ((44.184069865521963 41.971859752507207 0,44.355248797385123 41.976261187945859 0, ...



However, I do not need this Z value - I need just a plain 2D WKT. After hours of searching I found FlattenTo2D() method, but feature.GetGeometryRef().FlattenTo2D() returns None


So how do I convert 3D WKT to 2D?



Answer



Using of FlattenTo2D() method is correct way, but the method does not returns new object (like you expected) - it changes current object. So you should use the method like this:


g = ogr.CreateGeometryFromWkt("POINT(1 1 1)")
g.flattenTo2D()
print(g.ExportToWkt())
>> 'POINT (1 1)'

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...