Skip to content

Commit

Permalink
Merge pull request e2nIEE#620 from dlohmeier/hotfix/pandapower_geojso…
Browse files Browse the repository at this point in the history
…n_messup

Fix pandapower GEOJSON problems
  • Loading branch information
SimonRubenDrauz authored May 7, 2024
2 parents 25d69ae + 3adb3dc commit b6a99b0
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@

import numpy as np
import pandas as pd
from pandapower.plotting.collections import _create_complex_branch_collection, \
add_cmap_to_collection, coords_from_node_geodata
from pandapower.plotting.plotting_toolbox import get_index_array

from pandapipes.plotting.patch_makers import valve_patches
from pandapipes.plotting.plotting_toolbox import coords_from_node_geodata
from pandapower.plotting.collections import (_create_complex_branch_collection,
add_cmap_to_collection)
from pandapower.plotting.plotting_toolbox import get_index_array

try:
import pandaplan.core.pplog as logging
Expand Down
20 changes: 19 additions & 1 deletion src/pandapipes/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

import numpy as np
import pandas as pd
from pandapower.auxiliary import _preserve_dtypes
from pandapower.create import _get_multiple_index_with_check, _get_index_with_check, _set_entries, \
_check_node_element, _check_multiple_node_elements, _set_multiple_entries, \
_add_multiple_branch_geodata, _check_branch_element, _check_multiple_branch_elements
_check_branch_element, _check_multiple_branch_elements

from pandapipes.component_models import Junction, Sink, Source, Pump, Pipe, ExtGrid, \
HeatExchanger, Valve, CirculationPumpPressure, CirculationPumpMass, PressureControlComponent, \
Expand Down Expand Up @@ -1821,6 +1822,23 @@ def _check_std_type(net, std_type, table, function_name):
'one' % (std_type, table))


def _add_multiple_branch_geodata(net, table, geodata, index):
geo_table = f"{table}_geodata"
dtypes = net[geo_table].dtypes
df = pd.DataFrame(index=index, columns=net[geo_table].columns)
# works with single or multiple lists of coordinates
if len(geodata[0]) == 2 and not hasattr(geodata[0][0], "__iter__"):
# geodata is a single list of coordinates
df["coords"] = [geodata] * len(index)
else:
# geodata is multiple lists of coordinates
df["coords"] = geodata

net[geo_table] = pd.concat([net[geo_table],df], sort=False)

_preserve_dtypes(net[geo_table], dtypes)


ALLOWED_EG_TYPES = ["auto", "t", "p", "pt", "tp"]


Expand Down
8 changes: 4 additions & 4 deletions src/pandapipes/plotting/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

import numpy as np
import pandas as pd
from pandapower.plotting.collections import _create_node_collection, \
_create_node_element_collection, _create_line2d_collection, _create_complex_branch_collection, \
add_cmap_to_collection, coords_from_node_geodata
from pandapower.plotting.patch_makers import load_patches, ext_grid_patches
from pandapipes.plotting.patch_makers import valve_patches, source_patches, heat_exchanger_patches, \
pump_patches, pressure_control_patches, compressor_patches, flow_control_patches
from pandapipes.plotting.plotting_toolbox import coords_from_node_geodata
from pandapower.plotting.collections import _create_node_collection, add_cmap_to_collection, \
_create_node_element_collection, _create_line2d_collection, _create_complex_branch_collection
from pandapower.plotting.patch_makers import load_patches, ext_grid_patches
from pandapower.plotting.plotting_toolbox import get_index_array

try:
Expand Down
52 changes: 51 additions & 1 deletion src/pandapipes/plotting/plotting_toolbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@
# and Energy System Technology (IEE), Kassel, and University of Kassel. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

import numpy as np
try:
import pandaplan.core.pplog as logging
except ImportError:
import logging

logger = logging.getLogger(__name__)


def get_collection_sizes(net, junction_size=1.0, ext_grid_size=1.0, sink_size=1.0, source_size=1.0,
valve_size=2.0, pump_size=1.0, heat_exchanger_size=1.0,
pressure_control_size=1.0, compressor_size=1.0, flow_control_size=1.0):
Expand All @@ -10,7 +19,7 @@ def get_collection_sizes(net, junction_size=1.0, ext_grid_size=1.0, sink_size=1.
geocoord so that the collections fit the plot nicely
.. note: This is implemented because if you would choose a fixed values (e.g.\
junction_size = 0.2), the size could be to small for large networks and vice versa
junction_size = 0.2), the size could be too small for large networks and vice versa
:param net: pandapower network for which to create plot
:type net: pandapowerNet
Expand Down Expand Up @@ -46,3 +55,44 @@ def get_collection_sizes(net, junction_size=1.0, ext_grid_size=1.0, sink_size=1.

return sizes


def coords_from_node_geodata(element_indices, from_nodes, to_nodes, node_geodata, table_name,
node_name="Bus", ignore_zero_length=True):
"""
Auxiliary function to get the node coordinates for a number of branches with respective from
and to nodes. The branch elements for which there is no geodata available are not included in
the final list of coordinates.
:param element_indices: Indices of the branch elements for which to find node geodata
:type element_indices: iterable
:param from_nodes: Indices of the starting nodes
:type from_nodes: iterable
:param to_nodes: Indices of the ending nodes
:type to_nodes: iterable
:param node_geodata: Dataframe containing x and y coordinates of the nodes
:type node_geodata: pd.DataFrame
:param table_name: Name of the table that the branches belong to (only for logging)
:type table_name: str
:param node_name: Name of the node type (only for logging)
:type node_name: str, default "Bus"
:param ignore_zero_length: States if branches should be left out, if their length is zero, i.e.\
from_node_coords = to_node_coords
:type ignore_zero_length: bool, default True
:return: Return values are:\
- coords (list) - list of branch coordinates of shape (N, (2, 2))\
- elements_with_geo (set) - the indices of branch elements for which coordinates wer found\
in the node geodata table
"""
have_geo = np.isin(from_nodes, node_geodata.index.values) \
& np.isin(to_nodes, node_geodata.index.values)
elements_with_geo = np.array(element_indices)[have_geo]
fb_with_geo, tb_with_geo = from_nodes[have_geo], to_nodes[have_geo]
coords = [[(x_from, y_from), (x_to, y_to)] for x_from, y_from, x_to, y_to
in np.concatenate([node_geodata.loc[fb_with_geo, ["x", "y"]].values,
node_geodata.loc[tb_with_geo, ["x", "y"]].values], axis=1)
if not ignore_zero_length or not (x_from == x_to and y_from == y_to)]
elements_without_geo = set(element_indices) - set(elements_with_geo)
if len(elements_without_geo) > 0:
logger.warning("No coords found for %s %s. %s geodata is missing for those %s!"
% (table_name + "s", elements_without_geo, node_name, table_name + "s"))
return coords, elements_with_geo

0 comments on commit b6a99b0

Please sign in to comment.