TransWikia.com

AttributeError: 'NoneType' object has no attribute 'is_empty' when trying to set new CRS

Geographic Information Systems Asked by Reut on February 7, 2021

I have geodataframe that has two geometry fields.
I’m trying to set new CRS but every time I get the next error message:

AttributeError: ‘NoneType’ object has no attribute ‘is_empty’

when I am able to check the original CRS of the layer:

print('original crs is:',tost.crs)

>>>original crs is: EPSG:4326

but then when I try to set new CRS to the copy:

tast = tost.copy()
tast= tast.to_crs({'init': 'epsg:3857'})
print('projected the copy df to:',tast.crs)

>>>AttributeError: 'NoneType' object has no attribute 'is_empty'

I don’t understand why is this hapenning.
What cause this error?
My goal here is to be able to set the crs

One Answer

What if you don't want to drop any rows?
My solution would be to set the null geometries to be empty geometryCollections.
Then the to_crs() method works without throwing any AttributeError.
My code:

from shapely.geometry.collection import GeometryCollection

tast['geometry'] = tast.geometry.apply(lambda x: x if x else GeometryCollection())

tast.to_crs({'init': 'epsg:3857'})

Answered by Ture Friese on February 7, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP