From eedf4a2a97d2cb79fb119d3a8da122a542e02bc5 Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Wed, 19 Jan 2022 13:27:48 +0000 Subject: [PATCH 1/4] Restyled by black --- scripts/build_bus_regions.py | 121 +++++++++++++++++++---------------- 1 file changed, 67 insertions(+), 54 deletions(-) diff --git a/scripts/build_bus_regions.py b/scripts/build_bus_regions.py index 3ac3e45e1..d712dc45b 100644 --- a/scripts/build_bus_regions.py +++ b/scripts/build_bus_regions.py @@ -73,10 +73,7 @@ def save_to_geojson(df, fn): pass -def custom_voronoi_partition_pts(points, - outline, - add_bounds_shape=True, - multiplier=5): +def custom_voronoi_partition_pts(points, outline, add_bounds_shape=True, multiplier=5): """ Compute the polygons of a voronoi partition of `points` within the polygon `outline` @@ -122,17 +119,20 @@ def custom_voronoi_partition_pts(points, # to avoid any network positions outside all Voronoi cells, append # the corners of a rectangle framing these points vor = Voronoi( - np.vstack(( - points, - [ - [xmin - multiplier * xspan, ymin - multiplier * yspan], - [xmin - multiplier * xspan, ymax + multiplier * yspan], - [xmax + multiplier * xspan, ymin - multiplier * yspan], - [xmax + multiplier * xspan, ymax + multiplier * yspan], - ], - ))) - - polygons_arr = np.empty((len(points), ), "object") + np.vstack( + ( + points, + [ + [xmin - multiplier * xspan, ymin - multiplier * yspan], + [xmin - multiplier * xspan, ymax + multiplier * yspan], + [xmax + multiplier * xspan, ymin - multiplier * yspan], + [xmax + multiplier * xspan, ymax + multiplier * yspan], + ], + ) + ) + ) + + polygons_arr = np.empty((len(points),), "object") for i in range(len(points)): poly = Polygon(vor.vertices[vor.regions[vor.point_region[i]]]) @@ -149,25 +149,29 @@ def custom_voronoi_partition_pts(points, def get_gadm_shape(onshore_locs, gadm_shapes): def locate_bus(coords): try: - return gadm_shapes[gadm_shapes.contains( - Point(coords["x"], coords["y"]))].item() + return gadm_shapes[ + gadm_shapes.contains(Point(coords["x"], coords["y"])) + ].item() except ValueError: # return 'not_found' - gadm_shapes[gadm_shapes.contains(Point(-9, 32))].item( - ) # TODO !!Fatal!! assigning not found to a random shape + gadm_shapes[ + gadm_shapes.contains(Point(-9, 32)) + ].item() # TODO !!Fatal!! assigning not found to a random shape def get_id(coords): try: - return gadm_shapes[gadm_shapes.contains( - Point(coords["x"], coords["y"]))].index.item() + return gadm_shapes[ + gadm_shapes.contains(Point(coords["x"], coords["y"])) + ].index.item() except ValueError: # return 'not_found' - gadm_shapes[gadm_shapes.contains(Point(-9, 32))].index.item( - ) # TODO !!Fatal!! assigning not found to a random shape + gadm_shapes[ + gadm_shapes.contains(Point(-9, 32)) + ].index.item() # TODO !!Fatal!! assigning not found to a random shape sas = [] sas.append(onshore_locs[["x", "y"]].apply(locate_bus, axis=1).values) - ss = numpy.empty((len(sas), ), "object") + ss = numpy.empty((len(sas),), "object") ss[:] = sas regions = onshore_locs[["x", "y"]].apply(locate_bus, axis=1) ids = onshore_locs[["x", "y"]].apply(get_id, axis=1) @@ -186,12 +190,15 @@ def get_id(coords): n = pypsa.Network(snakemake.input.base_network) - country_shapes = gpd.read_file( - snakemake.input.country_shapes).set_index("name")["geometry"] - offshore_shapes = gpd.read_file( - snakemake.input.offshore_shapes).set_index("name")["geometry"] - gadm_shapes = gpd.read_file( - snakemake.input.gadm_shapes).set_index("GADM_ID")["geometry"] + country_shapes = gpd.read_file(snakemake.input.country_shapes).set_index("name")[ + "geometry" + ] + offshore_shapes = gpd.read_file(snakemake.input.offshore_shapes).set_index("name")[ + "geometry" + ] + gadm_shapes = gpd.read_file(snakemake.input.gadm_shapes).set_index("GADM_ID")[ + "geometry" + ] onshore_regions = [] offshore_regions = [] @@ -210,17 +217,21 @@ def get_id(coords): shape_id = get_gadm_shape(onshore_locs, gadm_shapes)[1] else: onshore_geometry = custom_voronoi_partition_pts( - onshore_locs.values, onshore_shape) + onshore_locs.values, onshore_shape + ) shape_id = 0 # Not used onshore_regions.append( - gpd.GeoDataFrame({ - "name": onshore_locs.index, - "x": onshore_locs["x"], - "y": onshore_locs["y"], - "geometry": onshore_geometry, - "country": country, - "shape_id": shape_id, - })) + gpd.GeoDataFrame( + { + "name": onshore_locs.index, + "x": onshore_locs["x"], + "y": onshore_locs["y"], + "geometry": onshore_geometry, + "country": country, + "shape_id": shape_id, + } + ) + ) # These two logging could be commented out if country not in offshore_shapes.index: @@ -233,25 +244,27 @@ def get_id(coords): _logger.warning(f"No off-shore substations found for {country}") continue else: - offshore_locs = n.buses.loc[c_b & n.buses.substation_off, - ["x", "y"]] + offshore_locs = n.buses.loc[c_b & n.buses.substation_off, ["x", "y"]] shape_id = 0 # Not used offshore_geometry = custom_voronoi_partition_pts( - offshore_locs.values, offshore_shape) - offshore_regions_c = gpd.GeoDataFrame({ - "name": offshore_locs.index, - "x": offshore_locs["x"], - "y": offshore_locs["y"], - "geometry": offshore_geometry, - "country": country, - "shape_id": shape_id, - }) - offshore_regions_c = offshore_regions_c.loc[ - offshore_regions_c.area > 1e-2] + offshore_locs.values, offshore_shape + ) + offshore_regions_c = gpd.GeoDataFrame( + { + "name": offshore_locs.index, + "x": offshore_locs["x"], + "y": offshore_locs["y"], + "geometry": offshore_geometry, + "country": country, + "shape_id": shape_id, + } + ) + offshore_regions_c = offshore_regions_c.loc[offshore_regions_c.area > 1e-2] offshore_regions.append(offshore_regions_c) - save_to_geojson(pd.concat(onshore_regions, ignore_index=True), - snakemake.output.regions_onshore) + save_to_geojson( + pd.concat(onshore_regions, ignore_index=True), snakemake.output.regions_onshore + ) if len(offshore_regions) != 0: offshore_regions = pd.concat(offshore_regions, ignore_index=True) save_to_geojson(offshore_regions, snakemake.output.regions_offshore) From b0ddf8804234a39b18e3c52577f86d9b6b0a3dd8 Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Wed, 19 Jan 2022 13:27:49 +0000 Subject: [PATCH 2/4] Restyled by isort --- scripts/build_bus_regions.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/scripts/build_bus_regions.py b/scripts/build_bus_regions.py index d712dc45b..acd95fd04 100644 --- a/scripts/build_bus_regions.py +++ b/scripts/build_bus_regions.py @@ -48,8 +48,7 @@ import pypsa from _helpers import configure_logging from download_osm_data import create_country_list -from shapely.geometry import Point -from shapely.geometry import Polygon +from shapely.geometry import Point, Polygon from vresutils.graph import voronoi_partition_pts _logger = logging.getLogger(__name__) From ca90a781dcb8309ac2045ffa3066e0a4c11fe341 Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Wed, 19 Jan 2022 13:27:52 +0000 Subject: [PATCH 3/4] Restyled by reorder-python-imports --- scripts/build_bus_regions.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/scripts/build_bus_regions.py b/scripts/build_bus_regions.py index acd95fd04..d712dc45b 100644 --- a/scripts/build_bus_regions.py +++ b/scripts/build_bus_regions.py @@ -48,7 +48,8 @@ import pypsa from _helpers import configure_logging from download_osm_data import create_country_list -from shapely.geometry import Point, Polygon +from shapely.geometry import Point +from shapely.geometry import Polygon from vresutils.graph import voronoi_partition_pts _logger = logging.getLogger(__name__) From 0a9bbe4b7a5964d13b99607fa71a593316b29f73 Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Wed, 19 Jan 2022 13:27:53 +0000 Subject: [PATCH 4/4] Restyled by yapf --- scripts/build_bus_regions.py | 122 ++++++++++++++++------------------- 1 file changed, 55 insertions(+), 67 deletions(-) diff --git a/scripts/build_bus_regions.py b/scripts/build_bus_regions.py index d712dc45b..d07d73b12 100644 --- a/scripts/build_bus_regions.py +++ b/scripts/build_bus_regions.py @@ -73,7 +73,10 @@ def save_to_geojson(df, fn): pass -def custom_voronoi_partition_pts(points, outline, add_bounds_shape=True, multiplier=5): +def custom_voronoi_partition_pts(points, + outline, + add_bounds_shape=True, + multiplier=5): """ Compute the polygons of a voronoi partition of `points` within the polygon `outline` @@ -119,20 +122,17 @@ def custom_voronoi_partition_pts(points, outline, add_bounds_shape=True, multipl # to avoid any network positions outside all Voronoi cells, append # the corners of a rectangle framing these points vor = Voronoi( - np.vstack( - ( - points, - [ - [xmin - multiplier * xspan, ymin - multiplier * yspan], - [xmin - multiplier * xspan, ymax + multiplier * yspan], - [xmax + multiplier * xspan, ymin - multiplier * yspan], - [xmax + multiplier * xspan, ymax + multiplier * yspan], - ], - ) - ) - ) - - polygons_arr = np.empty((len(points),), "object") + np.vstack(( + points, + [ + [xmin - multiplier * xspan, ymin - multiplier * yspan], + [xmin - multiplier * xspan, ymax + multiplier * yspan], + [xmax + multiplier * xspan, ymin - multiplier * yspan], + [xmax + multiplier * xspan, ymax + multiplier * yspan], + ], + ))) + + polygons_arr = np.empty((len(points), ), "object") for i in range(len(points)): poly = Polygon(vor.vertices[vor.regions[vor.point_region[i]]]) @@ -147,31 +147,28 @@ def custom_voronoi_partition_pts(points, outline, add_bounds_shape=True, multipl def get_gadm_shape(onshore_locs, gadm_shapes): + def locate_bus(coords): try: - return gadm_shapes[ - gadm_shapes.contains(Point(coords["x"], coords["y"])) - ].item() + return gadm_shapes[gadm_shapes.contains( + Point(coords["x"], coords["y"]))].item() except ValueError: # return 'not_found' - gadm_shapes[ - gadm_shapes.contains(Point(-9, 32)) - ].item() # TODO !!Fatal!! assigning not found to a random shape + gadm_shapes[gadm_shapes.contains(Point(-9, 32))].item( + ) # TODO !!Fatal!! assigning not found to a random shape def get_id(coords): try: - return gadm_shapes[ - gadm_shapes.contains(Point(coords["x"], coords["y"])) - ].index.item() + return gadm_shapes[gadm_shapes.contains( + Point(coords["x"], coords["y"]))].index.item() except ValueError: # return 'not_found' - gadm_shapes[ - gadm_shapes.contains(Point(-9, 32)) - ].index.item() # TODO !!Fatal!! assigning not found to a random shape + gadm_shapes[gadm_shapes.contains(Point(-9, 32))].index.item( + ) # TODO !!Fatal!! assigning not found to a random shape sas = [] sas.append(onshore_locs[["x", "y"]].apply(locate_bus, axis=1).values) - ss = numpy.empty((len(sas),), "object") + ss = numpy.empty((len(sas), ), "object") ss[:] = sas regions = onshore_locs[["x", "y"]].apply(locate_bus, axis=1) ids = onshore_locs[["x", "y"]].apply(get_id, axis=1) @@ -190,15 +187,12 @@ def get_id(coords): n = pypsa.Network(snakemake.input.base_network) - country_shapes = gpd.read_file(snakemake.input.country_shapes).set_index("name")[ - "geometry" - ] - offshore_shapes = gpd.read_file(snakemake.input.offshore_shapes).set_index("name")[ - "geometry" - ] - gadm_shapes = gpd.read_file(snakemake.input.gadm_shapes).set_index("GADM_ID")[ - "geometry" - ] + country_shapes = gpd.read_file( + snakemake.input.country_shapes).set_index("name")["geometry"] + offshore_shapes = gpd.read_file( + snakemake.input.offshore_shapes).set_index("name")["geometry"] + gadm_shapes = gpd.read_file( + snakemake.input.gadm_shapes).set_index("GADM_ID")["geometry"] onshore_regions = [] offshore_regions = [] @@ -217,21 +211,17 @@ def get_id(coords): shape_id = get_gadm_shape(onshore_locs, gadm_shapes)[1] else: onshore_geometry = custom_voronoi_partition_pts( - onshore_locs.values, onshore_shape - ) + onshore_locs.values, onshore_shape) shape_id = 0 # Not used onshore_regions.append( - gpd.GeoDataFrame( - { - "name": onshore_locs.index, - "x": onshore_locs["x"], - "y": onshore_locs["y"], - "geometry": onshore_geometry, - "country": country, - "shape_id": shape_id, - } - ) - ) + gpd.GeoDataFrame({ + "name": onshore_locs.index, + "x": onshore_locs["x"], + "y": onshore_locs["y"], + "geometry": onshore_geometry, + "country": country, + "shape_id": shape_id, + })) # These two logging could be commented out if country not in offshore_shapes.index: @@ -244,27 +234,25 @@ def get_id(coords): _logger.warning(f"No off-shore substations found for {country}") continue else: - offshore_locs = n.buses.loc[c_b & n.buses.substation_off, ["x", "y"]] + offshore_locs = n.buses.loc[c_b & n.buses.substation_off, + ["x", "y"]] shape_id = 0 # Not used offshore_geometry = custom_voronoi_partition_pts( - offshore_locs.values, offshore_shape - ) - offshore_regions_c = gpd.GeoDataFrame( - { - "name": offshore_locs.index, - "x": offshore_locs["x"], - "y": offshore_locs["y"], - "geometry": offshore_geometry, - "country": country, - "shape_id": shape_id, - } - ) - offshore_regions_c = offshore_regions_c.loc[offshore_regions_c.area > 1e-2] + offshore_locs.values, offshore_shape) + offshore_regions_c = gpd.GeoDataFrame({ + "name": offshore_locs.index, + "x": offshore_locs["x"], + "y": offshore_locs["y"], + "geometry": offshore_geometry, + "country": country, + "shape_id": shape_id, + }) + offshore_regions_c = offshore_regions_c.loc[ + offshore_regions_c.area > 1e-2] offshore_regions.append(offshore_regions_c) - save_to_geojson( - pd.concat(onshore_regions, ignore_index=True), snakemake.output.regions_onshore - ) + save_to_geojson(pd.concat(onshore_regions, ignore_index=True), + snakemake.output.regions_onshore) if len(offshore_regions) != 0: offshore_regions = pd.concat(offshore_regions, ignore_index=True) save_to_geojson(offshore_regions, snakemake.output.regions_offshore)