Skip to content

Commit aa0c112

Browse files
committed
WIP
1 parent 8b513e4 commit aa0c112

1 file changed

Lines changed: 1 addition & 240 deletions

File tree

examples/example_analyze_14a_ding0_grids.py

Lines changed: 1 addition & 240 deletions
Original file line numberDiff line numberDiff line change
@@ -1368,243 +1368,4 @@ def plot_network(
13681368
"""
13691369
if __name__ == "__main__":
13701370
edisgo = main()
1371-
"""
1372-
1373-
1374-
# ============================================================================
1375-
# TEMPORARY TEST FOR EV FUNCTIONS
1376-
# ============================================================================
1377-
GRID_BASE_PATH = "/home/paul/eDisGo/examples/30879"
1378-
EXPORT_DIR = Path("/home/paul/eDisGo/examples/test_ev_exports")
1379-
EXPORT_DIR.mkdir(parents=True, exist_ok=True)
1380-
1381-
# Load grid
1382-
print("\n1. Loading ding0 grid...")
1383-
edisgo = EDisGo(ding0_grid=GRID_BASE_PATH, legacy_ding0_grids=False)
1384-
1385-
# Set timeindex
1386-
timeindex = pd.date_range("2035-01-15 00:00:00", periods=24*7, freq="h")
1387-
edisgo.set_timeindex(timeindex)
1388-
1389-
# Import generators
1390-
print("2. Importing generators...")
1391-
scenario = "eGon2035"
1392-
edisgo.import_generators(generator_scenario=scenario)
1393-
1394-
# ============================================================================
1395-
# EV PART
1396-
# ============================================================================
1397-
# Import + distribute + integrate together
1398-
edisgo.import_electromobility(scenario=scenario)
1399-
1400-
# Apply charging strategy (writes timeseries for the integrated CP load IDs from eDisGo)
1401-
edisgo.apply_charging_strategy(strategy="dumb")
1402-
1403-
# ============================================================================
1404-
# Checks
1405-
# ============================================================================
1406-
print("\n3. Checking imported EV data...")
1407-
1408-
# Charging points
1409-
cp_df = edisgo.topology.charging_points_df.copy()
1410-
print("Charging points shape:", cp_df.shape)
1411-
print(cp_df.head())
1412-
1413-
# Charging processes
1414-
cp_process_df = edisgo.electromobility.charging_processes_df.copy()
1415-
print("Charging processes shape:", cp_process_df.shape)
1416-
print(cp_process_df.head())
1417-
1418-
# Charging point time series
1419-
loads_df = edisgo.timeseries.loads_active_power.copy()
1420-
print("Loads active power shape:", loads_df.shape)
1421-
print(loads_df.head())
1422-
1423-
# ============================================================================
1424-
# Export charging point time series
1425-
# ============================================================================
1426-
print("\n5. Exporting charging point time series...")
1427-
1428-
timeseries_csv = EXPORT_DIR / "charging_points_timeseries.csv"
1429-
loads_df.to_csv(timeseries_csv, index=True)
1430-
1431-
print(f"Charging point time series exported to: {timeseries_csv}")
1432-
1433-
# Optional: export only 5 sample charging points
1434-
sample_cols = loads_df.columns[:10]
1435-
df_sample = loads_df[sample_cols]
1436-
1437-
sample_csv = EXPORT_DIR / "charging_points_timeseries_sample_5.csv"
1438-
df_sample.to_csv(sample_csv, index=True)
1439-
1440-
print(f"Sample 5 charging point time series exported to: {sample_csv}")
1441-
1442-
# ============================================================================
1443-
# Show full output for 5 charging points and plot
1444-
# ============================================================================
1445-
print("\n6. Full output for 5 charging points...")
1446-
# with pd.option_context(
1447-
# "display.max_rows", None,
1448-
# "display.max_columns", None,
1449-
# "display.width", None
1450-
# ):
1451-
# print(df_sample)
1452-
1453-
# Plot 5 charging point time series
1454-
df_sample.plot(figsize=(12, 5))
1455-
plt.title("Charging profiles (5 sample CPs)")
1456-
plt.ylabel("Power")
1457-
plt.xlabel("Time")
1458-
plt.tight_layout()
1459-
plt.show()
1460-
1461-
# ============================================================================
1462-
# Export shp files of whole grid including charging points
1463-
# ============================================================================
1464-
from pathlib import Path
1465-
import pandas as pd
1466-
from shapely.geometry import LineString
1467-
1468-
EXPORT_DIR = Path("/home/paul/eDisGo/examples/test_ev_exports/network_shp")
1469-
EXPORT_DIR.mkdir(parents=True, exist_ok=True)
1470-
1471-
# Helper: detect coordinate columns in buses_df
1472-
def find_xy_columns(df):
1473-
candidates_x = ["x", "X", "lon", "longitude"]
1474-
candidates_y = ["y", "Y", "lat", "latitude"]
1475-
1476-
x_col = next((c for c in candidates_x if c in df.columns), None)
1477-
y_col = next((c for c in candidates_y if c in df.columns), None)
1478-
1479-
if x_col is None or y_col is None:
1480-
raise ValueError(
1481-
f"Could not find coordinate columns in buses_df. Columns are: {list(df.columns)}"
1482-
)
1483-
return x_col, y_col
1484-
1485-
# Base topology tables
1486-
buses_df = edisgo.topology.buses_df.copy()
1487-
lines_df = edisgo.topology.lines_df.copy()
1488-
loads_df = edisgo.topology.loads_df.copy()
1489-
generators_df = edisgo.topology.generators_df.copy()
1490-
transformers_df = edisgo.topology.transformers_df.copy()
1491-
1492-
x_col, y_col = find_xy_columns(buses_df)
1493-
1494-
# Optional: set CRS
1495-
# Replace with the correct CRS of your ding0 grid if you know it
1496-
CRS = "EPSG:4326"
1497-
1498-
# 1) Buses -> Point shapefile
1499-
buses_export = buses_df.reset_index().rename(columns={"index": "bus_id"})
1500-
buses_gdf = gpd.GeoDataFrame(
1501-
buses_export,
1502-
geometry=gpd.points_from_xy(buses_export[x_col], buses_export[y_col]),
1503-
crs=CRS,
1504-
)
1505-
buses_gdf.to_file(EXPORT_DIR / "buses.shp", driver="ESRI Shapefile")
1506-
1507-
# 2) Loads -> Point shapefile (including EV charging points)
1508-
if "bus" in loads_df.columns:
1509-
loads_export = loads_df.reset_index().rename(columns={"index": "load_id"})
1510-
loads_export = loads_export.merge(
1511-
buses_df[[x_col, y_col]],
1512-
left_on="bus",
1513-
right_index=True,
1514-
how="left"
1515-
)
1516-
loads_gdf = gpd.GeoDataFrame(
1517-
loads_export,
1518-
geometry=gpd.points_from_xy(loads_export[x_col], loads_export[y_col]),
1519-
crs=CRS,
1520-
)
1521-
loads_gdf.to_file(EXPORT_DIR / "loads.shp", driver="ESRI Shapefile")
1522-
1523-
# 3) Charging points only -> Point shapefile
1524-
cp_df = edisgo.topology.charging_points_df.copy()
1525-
if "bus" in cp_df.columns:
1526-
cp_export = cp_df.reset_index().rename(columns={"index": "charging_point_id"})
1527-
cp_export = cp_export.merge(
1528-
buses_df[[x_col, y_col]],
1529-
left_on="bus",
1530-
right_index=True,
1531-
how="left"
1532-
)
1533-
cp_gdf = gpd.GeoDataFrame(
1534-
cp_export,
1535-
geometry=gpd.points_from_xy(cp_export[x_col], cp_export[y_col]),
1536-
crs=CRS,
1537-
)
1538-
cp_gdf.to_file(EXPORT_DIR / "charging_points.shp", driver="ESRI Shapefile")
1539-
else:
1540-
# fallback: if charging_points_df already has geometry
1541-
if "geometry" in cp_df.columns:
1542-
cp_gdf = gpd.GeoDataFrame(cp_df.reset_index(), geometry="geometry", crs=CRS)
1543-
cp_gdf.to_file(EXPORT_DIR / "charging_points.shp", driver="ESRI Shapefile")
1544-
1545-
# 4) Generators -> Point shapefile
1546-
if "bus" in generators_df.columns:
1547-
gen_export = generators_df.reset_index().rename(columns={"index": "generator_id"})
1548-
gen_export = gen_export.merge(
1549-
buses_df[[x_col, y_col]],
1550-
left_on="bus",
1551-
right_index=True,
1552-
how="left"
1553-
)
1554-
gen_gdf = gpd.GeoDataFrame(
1555-
gen_export,
1556-
geometry=gpd.points_from_xy(gen_export[x_col], gen_export[y_col]),
1557-
crs=CRS,
1558-
)
1559-
gen_gdf.to_file(EXPORT_DIR / "generators.shp", driver="ESRI Shapefile")
1560-
1561-
# 5) Lines -> LineString shapefile
1562-
required_line_cols = {"bus0", "bus1"}
1563-
if required_line_cols.issubset(lines_df.columns):
1564-
line_records = []
1565-
for line_id, row in lines_df.iterrows():
1566-
bus0 = row["bus0"]
1567-
bus1 = row["bus1"]
1568-
1569-
if bus0 in buses_df.index and bus1 in buses_df.index:
1570-
x0, y0 = buses_df.loc[bus0, [x_col, y_col]]
1571-
x1, y1 = buses_df.loc[bus1, [x_col, y_col]]
1572-
1573-
geom = LineString([(x0, y0), (x1, y1)])
1574-
rec = row.to_dict()
1575-
rec["line_id"] = line_id
1576-
rec["geometry"] = geom
1577-
line_records.append(rec)
1578-
1579-
lines_gdf = gpd.GeoDataFrame(line_records, geometry="geometry", crs=CRS)
1580-
lines_gdf.to_file(EXPORT_DIR / "lines.shp", driver="ESRI Shapefile")
1581-
1582-
# 6) Transformers -> simple connection lines between buses
1583-
# Adjust column names if your dataframe uses different ones
1584-
possible_hv_cols = ["bus0", "hv_bus", "bus_hv"]
1585-
possible_lv_cols = ["bus1", "lv_bus", "bus_lv"]
1586-
1587-
hv_col = next((c for c in possible_hv_cols if c in transformers_df.columns), None)
1588-
lv_col = next((c for c in possible_lv_cols if c in transformers_df.columns), None)
1589-
1590-
if hv_col and lv_col:
1591-
trafo_records = []
1592-
for trafo_id, row in transformers_df.iterrows():
1593-
bus_hv = row[hv_col]
1594-
bus_lv = row[lv_col]
1595-
1596-
if bus_hv in buses_df.index and bus_lv in buses_df.index:
1597-
x0, y0 = buses_df.loc[bus_hv, [x_col, y_col]]
1598-
x1, y1 = buses_df.loc[bus_lv, [x_col, y_col]]
1599-
1600-
geom = LineString([(x0, y0), (x1, y1)])
1601-
rec = row.to_dict()
1602-
rec["transformer_id"] = trafo_id
1603-
rec["geometry"] = geom
1604-
trafo_records.append(rec)
1605-
1606-
if trafo_records:
1607-
trafos_gdf = gpd.GeoDataFrame(trafo_records, geometry="geometry", crs=CRS)
1608-
trafos_gdf.to_file(EXPORT_DIR / "transformers.shp", driver="ESRI Shapefile")
1609-
1610-
print(f"Shapefiles exported to: {EXPORT_DIR}")
1371+
"""

0 commit comments

Comments
 (0)