0

I have a df that has geoms as ogr geom in 3D. I was trying to convert to 2D but it empties out the geom.

ipdb> df['default_geom'][0]
        <osgeo.ogr.Geometry; proxy of <Swig Object of type 'OGRGeometryShadow *' at 0x7f83a9cb27b0> >

ipdb> df['default_geom'][0].ExportToWkt()
        'POLYGON ((-115.240767591292496 36.065560195680817 0,-115.241348136946002 36.065425084438168 0,-115.241322113923204 36.063269315122703 0,-115.240267897305003 36.063317147453262 0,-115.240208999282899 36.061408728686892 0,-115.241500777625902 36.061240907431618 0,-115.241619980337006 36.059512559015232 0,-115.242585075577907 36.059428687379608 0,-115.242674563454599 36.057736323718117 0,-115.241768917543595 36.057640168979759 0,-115.241902964798498 36.055959788158681 0,-115.238532725427604 36.05610326488739 0,-115.2381920799033 36.0615402938325 0,-115.237090968987005 36.061588194891307 0,-115.236986303475405 36.063349460793027 0,-115.239179174362903 36.063360808859777 0,-115.239134171002902 36.066013257150068 0,-115.240767591292496 36.065560195680817 0))'

ipdb> df['default_geom'][0].FlattenTo2D()

ipdb> df['default_geom'][0].FlattenTo2D().ExportToWkt()
            *** AttributeError: 'NoneType' object has no attribute 'ExportToWkt'
1

FlattenTo2D changes geometry inplace and returns None:

In [2]: g = ogr.CreateGeometryFromWkt('POLYGON ((0 0 0, 10 0 0, 10 10 0, 0 10 0, 0 0 0))')                                                                                                                  

In [3]: g.ExportToWkt()                                                                                                                                                                                     
Out[3]: 'POLYGON ((0 0 0,10 0 0,10 10 0,0 10 0,0 0 0))'

In [4]: g.FlattenTo2D()                                                                                                                                                                                     

In [5]: g.ExportToWkt()                                                                                                                                                                                     
Out[5]: 'POLYGON ((0 0,10 0,10 10,0 10,0 0))'

So you need to change line

ipdb> df['default_geom'][0].FlattenTo2D().ExportToWkt()

to

ipdb> df['default_geom'][0].FlattenTo2D()
ipdb> df['default_geom'][0].ExportToWkt()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.