Last active
September 29, 2021 03:48
-
-
Save knu2xs/6126b424d4d341c763c0d26853063f7e to your computer and use it in GitHub Desktop.
Calculate Polsby-Popper as measure of compactness
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
from arcgis.geometry import Polygon | |
def get_simplified_polygon(geometry: Polygon, simplification_factor: float = 0.1) -> Polygon: | |
# ensure working with a polygon | |
assert isinstance(geometry, Polygon) | |
# get a slice of the diameter if the area were a circle | |
smpl_dst = math.sqrt(geometry.area / math.pi) * simplification_factor | |
# generalize with no more deflection than a slice of the most compact geometry, a circle | |
smpl_poly = geometry.generalize(smpl_dst) | |
return smpl_poly | |
def get_polsby_popper_score(geometry: Polygon) -> float: | |
# ensure it is a polygon | |
assert isinstance(geometry, Polygon) | |
# calculate the Polsby-Popper score | |
pp_scr = 4 * math.pi * geometry.area / geometry.length ** 2 | |
return pp_scr | |
def get_simplified_polsby_popper_score(geometry: Polygon, simplification_factor: float = 0.1) - float: | |
# make sure it is a polygon | |
assert isinstance(geometry, Polygon) | |
# simplify the geometry | |
smpl_poly = get_simplified_polygon(geometry, simplification_factor) | |
# get the Polsby-Popper score for the simplified polygon | |
pp = get_polsby_popper_score(smpl_poly) | |
return pp |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment