Python package for calculating metrics related to district shapes.
Requires GDAL (brew install gdal
on Mac OS X with Homebrew).
Then:
pip install mander
from mander.districts import District
from mander.metrics import calculatePolsbyPopper
# Load a district from a GeoJSON or SHP file path
district = District(path='CD_CA_9.geojson')
# Call a metrics function on the District class object
score = calculatePolsbyPopper(district)
The package will accept any projection, but all districts are converted to US National Atlas Equal Area. Equal area projections are best for calculating compactness metrics. This ensures that when calculating area and perimeter measures, each district has a minimal amount of distortion, so districts in different parts of the country can be accurately compared to one another. Therefore, this shouldn't be used for districts outside of the U.S. Future versions will accept local projections.
The Polsby-Popper measure is a ratio of the area of the district to the area of a circle whose circumference is equal to the perimeter of the district.
The formula for calculating the Polsby-Popper score is:
where A is the area of the district and p is the perimeter of the district.
The Area/Convex Hull score is a ratio of the area of the district to the area of the minimum convex polygon that can enclose the district's geometry.
With convex hull polygons generated, the ratio can be calculated using the formula:
where A is the area of the district.
Note: this does not compute the calculation on each multipolygon feature in the case of districts containing islands. See issue #9.
The Reock score is a measure of the ratio of the district to the area of the minimum bounding circle that encloses the district's geometry.
where A is the area of the district.
Note: this does not compute the calculation on each multipolygon feature in the case of districts containing islands. See issue #10.
The Schwartzberg score is a ratio of the perimeter of the district to the circumference of a circle whose area is equal to the area of the district.
To generate the Schwartzberg score, first the circumference of a circle with an equal area of the district must be calculated. To do so, use the formula:
Formula to generate circumference of a circle
where A is the area of the district and r is the radius. With the radius calculated, use the following formula to generate the circumference (perimeter):
Finally, generate the Schwartzberg score using the following ratio:
where P is the perimeter of the district and C is the circumference (perimeter) of the circle with the same area.
Add compactness scores as a property on GeoJSON features.
metricName
should be one of:
'polsbypopper'
'schwarzberg'
'convex_hull'
'reock'
Return JSON object. Optionally, write to a GeoJSon file with path_out
.
To run the metrics test case:
python -m unittest test.metrics