From c268d595c4330fb109964f66bd1d3b661ec48423 Mon Sep 17 00:00:00 2001 From: Roman Bolgaryn Date: Mon, 21 Feb 2022 11:30:27 +0100 Subject: [PATCH] plotting.geo: add a function to set line_geodata from bus geodata --- pandapower/plotting/geo.py | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/pandapower/plotting/geo.py b/pandapower/plotting/geo.py index fe33fac1f..295385714 100644 --- a/pandapower/plotting/geo.py +++ b/pandapower/plotting/geo.py @@ -4,7 +4,7 @@ # and Energy System Technology (IEE), Kassel. All rights reserved. import sys -from numpy import array +from numpy import array, setdiff1d from pandapower.auxiliary import soft_dependency_error @@ -102,6 +102,33 @@ def _convert_xy_epsg(x, y, epsg_in=4326, epsg_out=31467): return transform(in_proj, out_proj, x, y) +def set_line_geodata_from_bus_geodata(net, line_index=None, overwrite=False): + """ + Sets coordinates in net.line_geodata based on the from_bus and to_bus x,y coordinates + in net.bus_geodata + :param net: pandapowerNet + :param line_index: index of lines, coordinates of which will be set from bus geodata (all lines if None) + :param overwrite: whether the existing coordinates in net.line_geodata must be overwritten + :return: None + """ + line_index = line_index if line_index is not None else net.line.index + if not overwrite: + line_index = setdiff1d(line_index, net.line_geodata.index) + failed = [] + for lidx in line_index: + b1 = net.line.from_bus.at[lidx] + b2 = net.line.to_bus.at[lidx] + if b1 in net.bus_geodata.index and b2 in net.bus_geodata.index: + coords = [(net.bus_geodata.x.at[int(b1)], net.bus_geodata.y.at[int(b1)]), + (net.bus_geodata.x.at[int(b2)], net.bus_geodata.y.at[int(b2)])] + net.line_geodata.loc[lidx, "coords"] = coords + else: + failed.append(lidx) + + if len(failed) > 0: + logger.info(f"failed to set coordinates of {len(failed)} lines") + + def convert_gis_to_geodata(net, node_geodata=True, branch_geodata=True): """ Extracts information on bus and line geodata from the geometries of a geopandas geodataframe.