# Measuring compactness in Python [closed]

I have set of polygons whom I would like to check their compactness.

I'm working in Jupyter Notebook and my geometry is GeoPandas.
I would like to try Polsby-Popper test and Schwartzberg test (and other test that are here: https://fisherzachary.github.io/public/r-output.html ) but I can't find any simple way to do it.

I don't find way to create the necessary circles, e.g "circle whose area is equal to the area of the polygon " or "circle whose circumference is equal to the perimeter of the polygon ".

I have found script to do find smallest enclosing circle but it seems like it works with points and I have polygon in GeoPandas (https://www.nayuki.io/res/smallest-enclosing-circle/smallestenclosingcircle.py).

If anyone knows any library/package/ any idea of how I can create circles from given perimeter or area. It's important to say that I have around 70k polygons to check so I also have the memory issue.

My end goal is to get ideas how can I calculate this in Python in Jupyter Notebook.

Edit: I have found this library but struggling with install it/use it (https://jblindsay.github.io/wbt_book/available_tools/gis_analysis_patch_shape_tools.html) (https://github.com/jblindsay/whitebox-tools/blob/master/src/tools/gis_analysis/related_circumscribing_circle.rs)

• You dont need the circle, at least for PP test gis.stackexchange.com/questions/271966/…
– BERA
Sep 17, 2020 at 13:20
• @BERA I'm forbbiden from use arcgis for this task Sep 17, 2020 at 13:22
• Ofc translate the formula to geopandas. You need geometry area and perimiter length
– BERA
Sep 17, 2020 at 13:22
• @BERA I need to calculate the circumscribing circle , otherwise is just the compactness ratio and not Polsby-Popper test or Schwartzberg Sep 17, 2020 at 13:35

You don't need to create circles. The formula is derived from the ratio you mentioned.

Use the following script. You can apply the other formulas easily.

``````import geopandas as gpd
from math import pi, sqrt

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

gdf["Polsby_Popper"] = gdf.geometry.apply(pp_compactness)
gdf["Schwartzberg"] = gdf.geometry.apply(s_compactness)

print(gdf)

#      geometry            Polsby_Popper   Schwartzberg
#  0   POLYGON ((552...    0.351956        0.593259
#  1   POLYGON ((552...    0.550202        0.741756
#  ..               ...         ...             ...
#  130 POLYGON ((553...    0.434469        0.659142
#  131 POLYGON ((553...    0.706016        0.840248
``````
• thank you for your answer, if I understand from you, I missunderstood the calclation and the calculation is the polygon with itself and not with circles? Sep 17, 2020 at 15:03
• The formula is derived from the ratio you mentioned. Don't worry about the ratio. There is a similar derivation in this post. Sep 17, 2020 at 15:08