Code Duplication    Length = 50-55 lines in 2 locations

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

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

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

@@ 282-336 (lines=55) @@
279
    )
280
281
282
def get_foreign_gas_bus_id(scn_name="eGon2035", carrier="CH4"):
283
    """Calculate the etrago bus id based on the geometry for eGon2035
284
285
    Map node_ids from TYNDP and etragos bus_id
286
287
    Parameters
288
    ----------
289
    scn_name : str
290
        Name of the scenario
291
    carrier : str
292
        Name of the carrier
293
294
    Returns
295
    -------
296
    pandas.Series
297
        List of mapped node_ids from TYNDP and etragos bus_id
298
299
    """
300
    sources = config.datasets()["gas_neighbours"]["sources"]
301
302
    bus_id = db.select_geodataframe(
303
        f"""
304
        SELECT bus_id, ST_Buffer(geom, 1) as geom, country
305
        FROM grid.egon_etrago_bus
306
        WHERE scn_name = '{scn_name}'
307
        AND carrier = '{carrier}'
308
        AND country != 'DE'
309
        """,
310
        epsg=3035,
311
    )
312
313
    # insert installed capacities
314
    file = zipfile.ZipFile(f"tyndp/{sources['tyndp_capacities']}")
315
316
    # Select buses in neighbouring countries as geodataframe
317
    buses = pd.read_excel(
318
        file.open("TYNDP-2020-Scenario-Datafile.xlsx").read(),
319
        sheet_name="Nodes - Dict",
320
    ).query("longitude==longitude")
321
    buses = gpd.GeoDataFrame(
322
        buses,
323
        crs=4326,
324
        geometry=gpd.points_from_xy(buses.longitude, buses.latitude),
325
    ).to_crs(3035)
326
327
    buses["bus_id"] = 0
328
329
    # Select bus_id from etrago with shortest distance to TYNDP node
330
    for i, row in buses.iterrows():
331
        distance = bus_id.set_index("bus_id").geom.distance(row.geometry)
332
        buses.loc[i, "bus_id"] = distance[
333
            distance == distance.min()
334
        ].index.values[0]
335
336
    return buses.set_index("node_id").bus_id
337