Skip to content

Commit

Permalink
Rename parameters to make consistent with find_nearest_bus and reuse …
Browse files Browse the repository at this point in the history
…in find_nearest_bus
  • Loading branch information
birgits committed Aug 21, 2024
1 parent 962c9e3 commit c7525d5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 21 deletions.
4 changes: 2 additions & 2 deletions edisgo/network/topology.py
Original file line number Diff line number Diff line change
Expand Up @@ -2488,7 +2488,7 @@ def handle_voltage_level_6():
def handle_voltage_level_7():
if allow_mv_connection:
mv_buses = self.buses_df.loc[self.mv_grid.buses_df.index]
mv_buses = geo.calculate_distance_to_buses_df(mv_buses, geolocation)
mv_buses = geo.calculate_distance_to_buses_df(geolocation, mv_buses)
mv_buses_masked = mv_buses.loc[
mv_buses.distance < max_distance_from_target_bus
]
Expand Down Expand Up @@ -2519,7 +2519,7 @@ def handle_voltage_level_7():
lv_buses = lv_buses.loc[~lv_buses.in_building]
lv_buses = lv_buses.loc[lv_loads.bus]

lv_buses = geo.calculate_distance_to_buses_df(lv_buses, geolocation)
lv_buses = geo.calculate_distance_to_buses_df(geolocation, lv_buses)
lv_buses_masked = lv_buses.loc[
lv_buses.distance < max_distance_from_target_bus
].copy()
Expand Down
40 changes: 21 additions & 19 deletions edisgo/tools/geo.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,12 +215,9 @@ def find_nearest_bus(point, bus_target):
Tuple that contains the name of the nearest bus and its distance in km.
"""
bus_target["dist"] = [
geodesic((point.y, point.x), (y, x)).km
for (x, y) in zip(bus_target["x"], bus_target["y"])
]
bus_target = calculate_distance_to_buses_df(point, bus_target)

return bus_target["dist"].idxmin(), bus_target["dist"].min()
return bus_target["distance"].idxmin(), bus_target["distance"].min()


def find_nearest_conn_objects(grid_topology, bus, lines, conn_diff_tolerance=0.0001):
Expand Down Expand Up @@ -329,28 +326,33 @@ def mv_grid_gdf(edisgo_obj: EDisGo):
)


def calculate_distance_to_buses_df(bus_df: pd.DataFrame, geom: Point) -> pd.DataFrame:
def calculate_distance_to_buses_df(
point: Point, busses_df: pd.DataFrame
) -> pd.DataFrame:
"""
Calculate the distance between a bus and a given geometry.
Calculate the distance between buses and a given geometry.
Parameters
----------
bus_df : pandas.DataFrame
Data of bus.
DataFrame has same rows as columns of
point : :shapely:`shapely.Point<Point>`
Geolocation to calculate distance to.
busses_df : :pandas:`pandas.DataFrame<DataFrame>`
Dataframe with buses and their positions given in 'x' and 'y'
columns. The dataframe has the same format as
:attr:`~.network.topology.Topology.buses_df`.
geom : shapely.geometry.Point
Geometry to calculate distance to.
Returns
-------
pandas.DataFrame
Data of bus with additional column 'distance' containing the distance
to the given geometry
:pandas:`pandas.DataFrame<DataFrame>`
Data of `bus_df` with additional column 'distance' containing the distance
to the given geometry in km.
"""
distances = bus_df.apply(
lambda row: geopy.distance.distance((row["x"], row["y"]), (geom.x, geom.y)).km,
distances = busses_df.apply(
lambda row: geopy.distance.distance(
(row["x"], row["y"]), (point.x, point.y)
).km,
axis=1,
)
bus_df.loc[:, "distance"] = distances
return bus_df
busses_df.loc[:, "distance"] = distances
return busses_df

0 comments on commit c7525d5

Please sign in to comment.