Code Duplication    Length = 50-55 lines in 2 locations

src/egon/data/datasets/electrical_neighbours.py 1 location

@@ 741-790 (lines=50) @@
738
    }
739
740
741
def get_foreign_bus_id():
742
    """Calculte the etrago bus id from Nodes of TYNDP based on the geometry
743
744
    Returns
745
    -------
746
    pandas.Series
747
        List of mapped node_ids from TYNDP and etragos bus_id
748
749
    """
750
751
    sources = config.datasets()["electrical_neighbours"]["sources"]
752
753
    bus_id = db.select_geodataframe(
754
        """SELECT bus_id, ST_Buffer(geom, 1) as geom, country
755
        FROM grid.egon_etrago_bus
756
        WHERE scn_name = 'eGon2035'
757
        AND carrier = 'AC'
758
        AND v_nom = 380.
759
        AND country != 'DE'
760
        AND bus_id NOT IN (
761
            SELECT bus_i
762
            FROM osmtgmod_results.bus_data)
763
        """,
764
        epsg=3035,
765
    )
766
767
    # insert installed capacities
768
    file = zipfile.ZipFile(f"tyndp/{sources['tyndp_capacities']}")
769
770
    # Select buses in neighbouring countries as geodataframe
771
    buses = pd.read_excel(
772
        file.open("TYNDP-2020-Scenario-Datafile.xlsx").read(),
773
        sheet_name="Nodes - Dict",
774
    ).query("longitude==longitude")
775
    buses = gpd.GeoDataFrame(
776
        buses,
777
        crs=4326,
778
        geometry=gpd.points_from_xy(buses.longitude, buses.latitude),
779
    ).to_crs(3035)
780
781
    buses["bus_id"] = 0
782
783
    # Select bus_id from etrago with shortest distance to TYNDP node
784
    for i, row in buses.iterrows():
785
        distance = bus_id.set_index("bus_id").geom.distance(row.geometry)
786
        buses.loc[i, "bus_id"] = distance[
787
            distance == distance.min()
788
        ].index.values[0]
789
790
    return buses.set_index("node_id").bus_id
791
792
793
def calc_capacities():

src/egon/data/datasets/gas_neighbours/gas_abroad.py 1 location

@@ 233-287 (lines=55) @@
230
    )
231
232
233
def get_foreign_gas_bus_id(scn_name="eGon2035", carrier="CH4"):
234
    """Calculate the etrago bus id based on the geometry for eGon2035
235
236
    Map node_ids from TYNDP and etragos bus_id
237
238
    Parameters
239
    ----------
240
    scn_name : str
241
        Name of the scenario
242
    carrier : str
243
        Name of the carrier
244
245
    Returns
246
    -------
247
    pandas.Series
248
        List of mapped node_ids from TYNDP and etragos bus_id
249
250
    """
251
    sources = config.datasets()["gas_neighbours"]["sources"]
252
253
    bus_id = db.select_geodataframe(
254
        f"""
255
        SELECT bus_id, ST_Buffer(geom, 1) as geom, country
256
        FROM grid.egon_etrago_bus
257
        WHERE scn_name = '{scn_name}'
258
        AND carrier = '{carrier}'
259
        AND country != 'DE'
260
        """,
261
        epsg=3035,
262
    )
263
264
    # insert installed capacities
265
    file = zipfile.ZipFile(f"tyndp/{sources['tyndp_capacities']}")
266
267
    # Select buses in neighbouring countries as geodataframe
268
    buses = pd.read_excel(
269
        file.open("TYNDP-2020-Scenario-Datafile.xlsx").read(),
270
        sheet_name="Nodes - Dict",
271
    ).query("longitude==longitude")
272
    buses = gpd.GeoDataFrame(
273
        buses,
274
        crs=4326,
275
        geometry=gpd.points_from_xy(buses.longitude, buses.latitude),
276
    ).to_crs(3035)
277
278
    buses["bus_id"] = 0
279
280
    # Select bus_id from etrago with shortest distance to TYNDP node
281
    for i, row in buses.iterrows():
282
        distance = bus_id.set_index("bus_id").geom.distance(row.geometry)
283
        buses.loc[i, "bus_id"] = distance[
284
            distance == distance.min()
285
        ].index.values[0]
286
287
    return buses.set_index("node_id").bus_id
288