Commit a6ab783a authored by Cecilia Nievas's avatar Cecilia Nievas
Browse files

Added function get_relative_area_range and its test

parent b769d799
......@@ -1175,3 +1175,31 @@ def get_distance_centroids(cells_gdf, col_lon, col_lat):
max_dist = np.array(dist).max()
return max_dist
def get_relative_area_range(cells_gdf):
"""This function calculates a measure of the variability of the area of the geometries
contained in cells_gdf, by subtracting the minimum area value from the maximum one and
normalising it by the mean.
Args:
cells_gdf (GeoDataFrame): GeoPandas GeoDataFrame with at least a geometry
column.
Returns:
areas_range_relative (float): [max(areas) - min(areas)] / mean(areas), where
areas is the area of the polygons in the geometry
column of cells_gdf.
"""
# Check that in_gdf has the needed columns and terminate otherwise:
if "geometry" not in cells_gdf.columns:
print("ERROR!! geometry missing as column of cells_gdf")
return np.inf
areas = [geom.area for geom in cells_gdf["geometry"].values]
areas = np.array(areas)
areas_range = areas.max() - areas.min()
areas_range_relative = areas_range / areas.mean()
return areas_range_relative
......@@ -1262,3 +1262,32 @@ def test_get_distance_centroids():
function_out = gdet_cr_ind.get_distance_centroids(dummy_gdf, "LONGITUDE", "LATITUDE")
assert np.isinf(function_out)
def test_get_relative_area_range():
"""
The original function is very simple. The test case is built within this test function.
"""
# Build a GeoPandas DataFrame for testing:
d = {
"geometry": [
Polygon([(22.0, 37.8), (23.5, 37.8), (23.5, 38.8), (22.0, 38.8)]),
Polygon([(-18.0, 36.8), (-17.0, 36.8), (-17.0, 38.8), (-18.0, 38.8)]),
Polygon([(22.5, -41.8), (23.5, -41.8), (23.5, -40.9), (22.5, -40.9)]),
Polygon([(-18.5, -41.9), (-16.5, -41.9), (-16.5, -40.9), (-18.5, -40.9)]),
]
} # Areas are: 1.5, 2.0, 0.9, 2.0
dummy_gdf = gpd.GeoDataFrame(d)
# Call function to test:
function_out = gdet_cr_ind.get_relative_area_range(dummy_gdf)
assert "{:.4f}".format(function_out) == "0.6875"
# Rename the columns and test again that the output is inf if there is no geometry column
# int he input GeoDataFrame:
dummy_gdf = dummy_gdf.rename(columns={"geometry": "geom"})
function_out = gdet_cr_ind.get_relative_area_range(dummy_gdf)
assert np.isinf(function_out)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment