Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Dynamic Exposure
Legacy
gde_calculations_prototype
Commits
a6ab783a
Commit
a6ab783a
authored
May 06, 2021
by
Cecilia Nievas
Browse files
Added function get_relative_area_range and its test
parent
b769d799
Changes
2
Hide whitespace changes
Inline
Side-by-side
GDE_TOOLS_create_industrial_cells.py
View file @
a6ab783a
...
...
@@ -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
tests/test_GDE_TOOLS_create_industrial_cells.py
View file @
a6ab783a
...
...
@@ -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
)
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment