Skip to content
GitLab
Menu
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
b769d799
Commit
b769d799
authored
May 06, 2021
by
Cecilia Nievas
Browse files
Added function get_distance_centroids and its test
parent
996a5735
Changes
2
Hide whitespace changes
Inline
Side-by-side
GDE_TOOLS_create_industrial_cells.py
View file @
b769d799
...
...
@@ -1129,3 +1129,49 @@ def auto_adjust_overlaps_gaps(
out_gdf
[
"lat_n"
].
values
[
which_2
[
0
]]
=
newpolygon2
.
bounds
[
3
]
return
out_gdf
,
gaps_found
def
get_distance_centroids
(
cells_gdf
,
col_lon
,
col_lat
):
"""This function calculates the distance between the points defined by (col_lon, col_lat)
and the centroids of the polygons defined in the geometry column of cells_gdf. No projection
is input. It is assumed that col_lon, col_lat and the geometry are all defined in a common
coordinate system and the distance is calculated in that coordinate system, which is treated
as Cartesian. The output is the largest of all these calculated distances.
Args:
cells_gdf (GeoDataFrame): GeoPandas GeoDataFrame with at least three columns:
- col_lon: longitude in degrees (float)
- col_lat: latitude in degrees (float)
- geometry: Shapely polygons
col_lon (str): Name of the column of cells_gdf that contains longitudes.
col_lat (str): Name of the column of cells_gdf that contains latitudes.
Returns:
max_dist (float): Maximum distance between the points defined by (col_lon, col_lat) and
the centroids of the polygons defined in the geometry column of
cells_gdf. No projection assumed. Distance calculated treating the
values of col_lon, col_lat and the definition of geometry as
Cartesian.
"""
# Check that in_gdf has the needed columns and terminate otherwise:
if
(
(
col_lon
not
in
cells_gdf
.
columns
)
or
(
col_lat
not
in
cells_gdf
.
columns
)
or
(
"geometry"
not
in
cells_gdf
.
columns
)
):
print
(
"ERROR!! One or more of col_lon, col_lat, geometry "
"missing as columns of cells_gdf"
)
return
np
.
inf
centroids
=
[
geom
.
centroid
for
geom
in
cells_gdf
[
"geometry"
].
values
]
orig_points
=
[
Point
(
lon_val
,
lat_val
)
for
lon_val
,
lat_val
in
zip
(
cells_gdf
[
col_lon
].
values
,
cells_gdf
[
col_lat
].
values
)
]
dist
=
[
p1
.
distance
(
p2
)
for
p1
,
p2
in
zip
(
centroids
,
orig_points
)]
max_dist
=
np
.
array
(
dist
).
max
()
return
max_dist
tests/test_GDE_TOOLS_create_industrial_cells.py
View file @
b769d799
...
...
@@ -1229,3 +1229,36 @@ def test_auto_adjust_overlaps_gaps():
np
.
testing
.
assert_allclose
(
expected_out_gdf
[
"lat_n"
].
values
,
function_out_gdf
[
"lat_n"
].
values
,
rtol
=
0.0
,
atol
=
1e-08
)
def
test_get_distance_centroids
():
"""
The original function is very simple. The test case is built within this test function.
"""
# Build a GeoPandas DataFrame for testing:
col_lon
=
"LON"
col_lat
=
"LAT"
d
=
{
col_lon
:
[
23.0
,
-
17.5
,
23.0
,
-
17.5
],
col_lat
:
[
38.3
,
38.3
,
-
41.4
,
-
41.4
],
"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
)]),
],
}
# Distances are: 0.25, 0.5, 0.05, 0.0
dummy_gdf
=
gpd
.
GeoDataFrame
(
d
)
# Call function to test:
function_out
=
gdet_cr_ind
.
get_distance_centroids
(
dummy_gdf
,
col_lon
,
col_lat
)
assert
"{:.3f}"
.
format
(
function_out
)
==
"0.500"
# Test that the result is inf when the strings given as col_lon and col_lat are not found
# as column names in the input GeoDataFrame:
# Call function to test:
function_out
=
gdet_cr_ind
.
get_distance_centroids
(
dummy_gdf
,
"LONGITUDE"
,
"LATITUDE"
)
assert
np
.
isinf
(
function_out
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a 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