Skip to content

Commit

Permalink
Merge branch 'geo_accessor' of https://github.com/mfranz13/pandapower
Browse files Browse the repository at this point in the history
…into geo_accessor
  • Loading branch information
Moritz Franz committed Nov 18, 2024
2 parents eb5caf1 + c892162 commit a5bfb88
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def _prepare_series_compensators_cim16(self) -> pd.DataFrame:
ser_comp['gf_pu'] = 0.
ser_comp['bf_pu'] = 0.
ser_comp['gt_pu'] = 0.
ser_comp['bf_pu'] = 0.
ser_comp['bt_pu'] = 0.
ser_comp['in_service'] = ser_comp.connected & ser_comp.connected2
ser_comp = ser_comp.rename(columns={'rdfId_Terminal': sc['t_from'], 'rdfId_Terminal2': sc['t_to'],
'rdfId': sc['o_id'], 'index_bus': 'from_bus', 'index_bus2': 'to_bus'})
Expand Down
2 changes: 1 addition & 1 deletion pandapower/plotting/plotly/simple_plotly.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def simple_plotly(net, respect_switches=True, use_line_geo=None, on_map=False,
**respect_switches** (bool, True) - Respect switches when artificial geodata is created
**use_line_geo** (bool, True) - defines if lines patches are based on
net.line_geodata of the lines (True) or on net.bus_geodata of the connected buses (False)
net.line.geo of the lines (True) or on net.bus.geo of the connected buses (False)
**on_map** (bool, False) - enables using mapbox plot in plotly.
If provided geodata are not real geo-coordinates in lon/lat form, on_map will be set to False.
Expand Down
4 changes: 2 additions & 2 deletions pandapower/plotting/plotly/vlevel_plotly.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ def vlevel_plotly(net, respect_switches=True, use_line_geo=None, colors_dict=Non
OPTIONAL:
**respect_switches** (bool, True) - Respect switches when artificial geodata is created
**use_line_geo** (bool, True) - defines if lines patches are based on net.line_geodata
of the lines (True) or on net.bus_geodata of the connected buses (False)
**use_line_geo** (bool, True) - defines if lines patches are based on net.line.geo
of the lines (True) or on net.bus.geo of the connected buses (False)
*colors_dict** (dict, None) - dictionary for customization of colors for each voltage level
in the form: voltage : color
Expand Down
58 changes: 48 additions & 10 deletions pandapower/test/plotting/test_geo.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import geojson
import pandas as pd
import pytest
from pandas.testing import assert_frame_equal, assert_index_equal

import pandapower.plotting.geo as geo
from pandapower.test.helper_functions import create_test_network
Expand Down Expand Up @@ -54,50 +55,89 @@ def get_network_and_result(net, request):

def test__node_geometries_from_geodata(get_network_and_result):
pytest.importorskip("geopandas")
from geopandas import testing

_net, expected = get_network_and_result
_bus_geojson_to_geodata_(_net)

result = geo._node_geometries_from_geodata(_net.bus_geodata)
testing.assert_geodataframe_equal(result, expected)
# is mostly the same as assert_geodataframe_equal with check_less_precise=True, but the tolerance in the function
# can't be adapted
assert result.shape == expected.shape
assert isinstance(result, type(expected))
assert (result.geom_equals_exact(expected.geometry, tolerance=1 * 10 ** (-6)) |
(result.geometry.is_empty & expected.geometry.is_empty) |
(result.geometry.isna() & expected.geometry.isna())).all()
left2 = result.select_dtypes(exclude="geometry")
right2 = expected.select_dtypes(exclude="geometry")
assert_index_equal(result.columns, expected.columns, exact="equiv", obj="GeoDataFrame.columns")
assert_frame_equal(left2, right2, check_dtype=True, check_index_type="equiv", check_column_type="equiv", obj="GeoDataFrame")


def test__branch_geometries_from_geodata(get_network_and_result):
pytest.importorskip("geopandas")
from geopandas import testing

_net, expected = get_network_and_result

_line_geojson_to_geodata_(_net)

result = geo._branch_geometries_from_geodata(_net.line_geodata)
testing.assert_geodataframe_equal(result, expected)
# is mostly the same as assert_geodataframe_equal with check_less_precise=True, but the tolerance in the function
# can't be adapted
assert result.shape == expected.shape
assert isinstance(result, type(expected))
assert (result.geom_equals_exact(expected.geometry, tolerance=1 * 10 ** (-6)) |
(result.geometry.is_empty & expected.geometry.is_empty) |
(result.geometry.isna() & expected.geometry.isna())).all()
left2 = result.select_dtypes(exclude="geometry")
right2 = expected.select_dtypes(exclude="geometry")
assert_index_equal(result.columns, expected.columns, exact="equiv", obj="GeoDataFrame.columns")
assert_frame_equal(left2, right2, check_dtype=True, check_index_type="equiv", check_column_type="equiv",
obj="GeoDataFrame")


def test__transform_node_geometry_to_geodata(get_network_and_result):
pytest.importorskip("geopandas")
from geopandas import testing

_net, expected = get_network_and_result
_bus_geojson_to_geodata_(_net)

# Transforming to geodata to test the inverse...
_net.bus_geodata = geo._node_geometries_from_geodata(_net.bus_geodata)
result = geo._transform_node_geometry_to_geodata(_net.bus_geodata)
testing.assert_geodataframe_equal(result, expected)
# is mostly the same as assert_geodataframe_equal with check_less_precise=True, but the tolerance in the function
# can't be adapted
assert result.shape == expected.shape
assert isinstance(result, type(expected))
assert (result.geom_equals_exact(expected.geometry, tolerance=1 * 10 ** (-6)) |
(result.geometry.is_empty & expected.geometry.is_empty) |
(result.geometry.isna() & expected.geometry.isna())).all()
left2 = result.select_dtypes(exclude="geometry")
right2 = expected.select_dtypes(exclude="geometry")
assert_index_equal(result.columns, expected.columns, exact="equiv", obj="GeoDataFrame.columns")
assert_frame_equal(left2, right2, check_dtype=True, check_index_type="equiv", check_column_type="equiv",
obj="GeoDataFrame")


def test__transform_branch_geometry_to_coords(get_network_and_result):
pytest.importorskip("geopandas")
from geopandas import testing

_net, expected = get_network_and_result
_line_geojson_to_geodata_(_net)

_net.line_geodata = geo._branch_geometries_from_geodata(_net.line_geodata)
result = geo._transform_branch_geometry_to_coords(_net.line_geodata)
testing.assert_geodataframe_equal(result, expected)
# is mostly the same as assert_geodataframe_equal with check_less_precise=True, but the tolerance in the function
# can't be adapted
assert result.shape == expected.shape
assert isinstance(result, type(expected))
assert (result.geom_equals_exact(expected.geometry, tolerance=1 * 10 ** (-6)) |
(result.geometry.is_empty & expected.geometry.is_empty) |
(result.geometry.isna() & expected.geometry.isna())).all()
left2 = result.select_dtypes(exclude="geometry")
right2 = expected.select_dtypes(exclude="geometry")
assert_index_equal(result.columns, expected.columns, exact="equiv", obj="GeoDataFrame.columns")
assert_frame_equal(left2, right2, check_dtype=True, check_index_type="equiv", check_column_type="equiv",
obj="GeoDataFrame")


def test__convert_xy_epsg():
Expand Down Expand Up @@ -261,7 +301,6 @@ def test_dump_to_geojson():
assert dumps(result, sort_keys=True) == '{"features": [{"geometry": {"coordinates": [[1.0, 2.0], [3.0, 4.0]], "type": "LineString"}, "id": "line-0", "properties": {"c_nf_per_km": 720.0, "df": 1.0, "from_bus": 1, "g_us_per_km": 0.0, "i_from_ka": 7.0, "i_ka": 7.0, "i_to_ka": 7.0, "ices": 0.389985, "in_service": true, "length_km": 1.0, "loading_percent": 7.0, "max_i_ka": 0.328, "name": "line1", "p_from_mw": 7.0, "p_to_mw": 7.0, "parallel": 1, "pl_mw": 7.0, "pp_index": 0, "pp_type": "line", "q_from_mvar": 7.0, "q_to_mvar": 7.0, "ql_mvar": 7.0, "r_ohm_per_km": 0.2067, "std_type": null, "to_bus": 7, "type": null, "va_from_degree": 7.0, "va_to_degree": 7.0, "vm_from_pu": 7.0, "vm_to_pu": 7.0, "x_ohm_per_km": 0.1897522}, "type": "Feature"}], "type": "FeatureCollection"}'



def test_convert_geodata_to_geojson():
pytest.importorskip("geojson")
pytest.importorskip("pandapower")
Expand Down Expand Up @@ -295,6 +334,5 @@ def test_convert_gis_to_geojson():
pytest.skip("Not implemented")



if __name__ == "__main__":
pytest.main(["test_geo.py"])
2 changes: 1 addition & 1 deletion pandapower/toolbox/data_modification.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ def create_continuous_elements_index(net, start=0, add_df_to_reindex=set()):

if et in net and isinstance(net[et], pd.DataFrame):
if et in ["bus_geodata", "line_geodata"]:
logger.info(et + " don't need to bo included to 'add_df_to_reindex'. It is " +
logger.info(et + " don't need to be included to 'add_df_to_reindex'. It is " +
"already included by et=='" + et.split("_")[0] + "'.")
else:
reindex_elements(net, et, new_index)
Expand Down

0 comments on commit a5bfb88

Please sign in to comment.