TransWikia.com

AttributeError: 'NoneType' object has no attribute 'length' when running function for compactness

Geographic Information Systems Asked on November 29, 2020

I want to calculate compactness indices for geometries I have in a geodataframe.
I’m using the following function as was mentioned here (Measuring compactness in Python) by Kadir in order to calculate different shapes compactness. This is the function:

def pp_compactness(geom): # Polsby-Popper
    p = geom.length
    a = geom.area    
    return (4*pi*a)/(p*p)
    
def s_compactness(geom): # Schwartzberg
    p = geom.length
    a = geom.area    
    return 1/(p/(2*pi*sqrt(a/pi)))

df['polsby_popper']=df['geometry'].apply(pp_compactness)
df['Schwartzberg'] = df['geometry'].apply(s_compactness)

This is my dataframe sample:

>>>ID  geometry                  centroid              class        perimeter
0  43 POLYGON((9.956 9.12...)   POINT (9.95 9.10)      Forest       521.347
1  46 POLYGON((8.941 8.54...)   POINT (8.5 9.44)       Desert       345.123
...

I have tried to apply the function on the geometry column as following:



f['polsby_popper']=df['geometry'].apply(pp_compactness)
df['Schwartzberg'] = df['geometry'].apply(s_compactness)

but I’m getting the following error message:

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

I have tried to set the geoemtry before and run again:

df=df.set_geometry('geometry')

but still got the same error message.

I have no idea why it is missing the length attribute,

My end goal is to use this function to calculate compactness in geometries I have as GeoPandas .

EDIT: I have changed the function instead of geom.length and geom.area to geometry.length and geometry.area (because that’s the name of the geometry column) but that raised the same error.

def pp_compactness(geometry): # Polsby-Popper
    p = geometry.length
    a = geometry.area    
    return (4*pi*a)/(p*p)
    
def s_compactness(geometry): # Schwartzberg
    p = geometry.length
    a = geometry.area    
    return 1/(p/(2*pi*sqrt(a/pi)))

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