Conditions | 2 |
Total Lines | 59 |
Code Lines | 27 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | """The central module containing code to create CH4 and H2 voronoi polygons |
||
68 | def create_voronoi(scn_name, carrier): |
||
69 | """ |
||
70 | Create voronoi polygons for specified carrier in specified scenario. |
||
71 | |||
72 | Parameters |
||
73 | ---------- |
||
74 | scn_name : str |
||
75 | Name of the scenario |
||
76 | carrier : str |
||
77 | Name of the carrier |
||
78 | """ |
||
79 | boundary = db.select_geodataframe( |
||
80 | f""" |
||
81 | SELECT id, geometry |
||
82 | FROM boundaries.vg250_sta_union; |
||
83 | """, |
||
84 | geom_col="geometry", |
||
85 | ).to_crs(epsg=4326) |
||
86 | |||
87 | engine = db.engine() |
||
88 | |||
89 | db.execute_sql( |
||
90 | f""" |
||
91 | DELETE FROM grid.egon_gas_voronoi |
||
92 | WHERE "carrier" = '{carrier}' and "scn_name" = '{scn_name}'; |
||
93 | """ |
||
94 | ) |
||
95 | |||
96 | buses = db.select_geodataframe( |
||
97 | f""" |
||
98 | SELECT bus_id, geom |
||
99 | FROM grid.egon_etrago_bus |
||
100 | WHERE scn_name = '{scn_name}' |
||
101 | AND country = 'DE' |
||
102 | AND carrier = '{carrier}'; |
||
103 | """, |
||
104 | ).to_crs(epsg=4326) |
||
105 | |||
106 | if len(buses) == 0: |
||
107 | return |
||
108 | |||
109 | buses["x"] = buses.geometry.x |
||
110 | buses["y"] = buses.geometry.y |
||
111 | # generate voronois |
||
112 | gdf = get_voronoi_geodataframe(buses, boundary.geometry.iloc[0]) |
||
113 | # set scn_name |
||
114 | gdf["scn_name"] = scn_name |
||
115 | gdf["carrier"] = carrier |
||
116 | |||
117 | gdf.rename_geometry("geom", inplace=True) |
||
118 | gdf.drop(columns=["id"], inplace=True) |
||
119 | # Insert data to db |
||
120 | gdf.set_crs(epsg=4326).to_postgis( |
||
121 | f"egon_gas_voronoi", |
||
122 | engine, |
||
123 | schema="grid", |
||
124 | index=False, |
||
125 | if_exists="append", |
||
126 | dtype={"geom": Geometry}, |
||
127 | ) |
||
128 |