3

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

4
  • try .to_crs(epsg=3857)
    – sutan
    Sep 10, 2020 at 11:25
  • @sutan I am getting the same error message
    – ReutKeller
    Sep 10, 2020 at 11:28
  • 3
    I suspect one of the rows has no geometry. Check if None exists in the geometry column, like this >>> True if None in tost.geometry else False. if this returns True, then there is a None in the geometry column, you should drop the row. or filter the none like this tast = tost[tost.geometry != None], then you can do tast.to_crs(epsg=3857)
    – sutan
    Sep 10, 2020 at 12:10
  • You can also try column.geometry.is_valid to avoid a few potential problems
    – mmann1123
    Sep 20, 2021 at 15:49

1 Answer 1

3

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'})

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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