| Conditions | 4 |
| Total Lines | 87 |
| Code Lines | 38 |
| 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 | """ |
||
| 149 | def create_voronoi(scn_name, carrier): |
||
| 150 | """ |
||
| 151 | Create voronoi polygons for specified carrier in specified scenario. |
||
| 152 | |||
| 153 | Parameters |
||
| 154 | ---------- |
||
| 155 | scn_name : str |
||
| 156 | Name of the scenario |
||
| 157 | carrier : str |
||
| 158 | Name of the carrier |
||
| 159 | """ |
||
| 160 | |||
| 161 | engine = db.engine() |
||
| 162 | |||
| 163 | table_exist = ( |
||
| 164 | len( |
||
| 165 | pd.read_sql( |
||
| 166 | """ |
||
| 167 | SELECT * |
||
| 168 | FROM information_schema.tables |
||
| 169 | WHERE table_schema = 'grid' |
||
| 170 | AND table_name = 'egon_gas_voronoi' |
||
| 171 | LIMIT 1; |
||
| 172 | """, |
||
| 173 | engine, |
||
| 174 | ) |
||
| 175 | ) |
||
| 176 | > 0 |
||
| 177 | ) |
||
| 178 | |||
| 179 | if not table_exist: |
||
| 180 | create_gas_voronoi_table() |
||
| 181 | |||
| 182 | boundary = db.select_geodataframe( |
||
| 183 | """ |
||
| 184 | SELECT id, geometry |
||
| 185 | FROM boundaries.vg250_sta_union; |
||
| 186 | """, |
||
| 187 | geom_col="geometry", |
||
| 188 | ).to_crs(epsg=4326) |
||
| 189 | |||
| 190 | db.execute_sql( |
||
| 191 | f""" |
||
| 192 | DELETE FROM grid.egon_gas_voronoi |
||
| 193 | WHERE "carrier" = '{carrier}' and "scn_name" = '{scn_name}'; |
||
| 194 | """ |
||
| 195 | ) |
||
| 196 | |||
| 197 | buses = db.select_geodataframe( |
||
| 198 | f""" |
||
| 199 | SELECT bus_id, geom |
||
| 200 | FROM grid.egon_etrago_bus |
||
| 201 | WHERE scn_name = '{scn_name}' |
||
| 202 | AND country = 'DE' |
||
| 203 | AND carrier = '{carrier}'; |
||
| 204 | """, |
||
| 205 | ).to_crs(epsg=4326) |
||
| 206 | |||
| 207 | if len(buses) == 0: |
||
| 208 | return |
||
| 209 | |||
| 210 | # generate voronois |
||
| 211 | # For some scenarios it is defined that there is only 1 bus (e.g. gas). It |
||
| 212 | # means that there will be just 1 voronoi covering the entire german |
||
| 213 | # territory. For other scenarios with more buses, voronois are calculated. |
||
| 214 | if len(buses) == 1: |
||
| 215 | gdf = buses.copy() |
||
| 216 | gdf.at[0, "geom"] = boundary.at[0, "geometry"] |
||
| 217 | else: |
||
| 218 | buses["x"] = buses.geometry.x |
||
| 219 | buses["y"] = buses.geometry.y |
||
| 220 | gdf = get_voronoi_geodataframe(buses, boundary.geometry.iloc[0]) |
||
| 221 | gdf.rename_geometry("geom", inplace=True) |
||
| 222 | gdf.drop(columns=["id"], inplace=True) |
||
| 223 | |||
| 224 | # set scn_name and carrier |
||
| 225 | gdf["scn_name"] = scn_name |
||
| 226 | gdf["carrier"] = carrier |
||
| 227 | |||
| 228 | # Insert data to db |
||
| 229 | gdf.set_crs(epsg=4326).to_postgis( |
||
| 230 | f"egon_gas_voronoi", |
||
| 231 | engine, |
||
| 232 | schema="grid", |
||
| 233 | index=False, |
||
| 234 | if_exists="append", |
||
| 235 | dtype={"geom": Geometry}, |
||
| 236 | ) |
||
| 281 |