Skip to content

Commit

Permalink
Create a geojson parser definition from arctic_config.py file.
Browse files Browse the repository at this point in the history
  • Loading branch information
SoloWayG committed Nov 24, 2023
1 parent 0af93fd commit 0f759e9
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 44 deletions.
70 changes: 26 additions & 44 deletions cases/breakwaters/arctic_config/arctic_config.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import json
import pickle
from pathlib import Path

import numpy as np
from shapely.geometry import shape
from breakwaters.breakwaters_utils import parse_arctic_geojson

from gefest.core.configs.optimization_params import OptimizationParams
from gefest.core.configs.tuner_params import TunerParams
Expand All @@ -13,48 +12,24 @@
from gefest.tools.estimators.simulators.swan.swan_interface import Swan

root_path = Path(__file__).parent.parent.parent.parent
with open(
f"{root_path}/cases/breakwaters/newdata/result_PwiOsA2HE2igZUel.geojson", "r"
) as file:
res_list = json.load(file)
with open(
f"{root_path}/cases/breakwaters/newdata/border_PwiOsA2HE2igZUel.geojson", "r"
) as file:
border_dict = json.load(file)


border = shape(border_dict["features"][0]["geometry"])
water = [i for i in res_list["features"] if i["properties"]["type"] == "water"]
water_coord = [p["geometry"]["coordinates"] for p in water]
cargo_piers = [
i for i in res_list["features"] if i["properties"]["type"] == "cargo_pier"
]
passenger_pier = [
i for i in res_list["features"] if i["properties"]["type"] == "passenger_pier"
]
piers = [
i
for i in res_list["features"]
if (i["properties"]["type"] == "passenger_pier")
or (i["properties"]["type"] == "cargo_pier")
]
piers_coords = [x[0] for x in [i["geometry"]["coordinates"] for i in piers]]
piers_line = [max(p, key=lambda i: i[1]) for p in piers_coords]
unique_types = np.unique([i["properties"]["type"] for i in res_list["features"]])
allow_water = [
i
for i in water_coord[0][0]
if (i[0] > 74.8) and (i[1] < 67.942) and (i[1] > 67.915)
]
result_path = 'cases/breakwaters/newdata/result_PwiOsA2HE2igZUel.geojson'
border_path = 'cases/breakwaters/newdata/border_PwiOsA2HE2igZUel.geojson'


allow_water = parse_arctic_geojson(
result_path=result_path,
border_path=border_path,
root_path=root_path
)
allow_area = [[74.80, 67.92], [74.80, 67.94]] + allow_water + [[74.80, 67.92]]
grid_resolution_x = 17 # Number of points on x-axis
grid_resolution_y = 31 # Number of points on y-axis
coord_Y = np.linspace(
coord_y = np.linspace(
min([p[1] for p in allow_area]) * 500,
max([p[1] for p in allow_area]) * 500,
grid_resolution_y + 1,
) # X coordinate for spatial grid
coord_X = np.linspace(
coord_x = np.linspace(
min([p[0] for p in allow_area]) * 500,
max([p[0] for p in allow_area]) * 500,
grid_resolution_x + 1,
Expand All @@ -66,10 +41,18 @@
# WINDWIND 19.1 225
# targets = [[14,10],[16,10],[18,10]]
# # # Metrics # # #


def load_file_from_path(path: str):
"""Func to load pickle file.
:param path:
:return:
"""
with open(path, "rb") as f:
_file = pickle.load(f)
f.close()

return _file


Expand All @@ -81,11 +64,11 @@ def load_file_from_path(path: str):

domain_cfg = Domain(
allowed_area=[
(min(coord_X), min(coord_Y)),
(min(coord_X), max(coord_Y)),
(max(coord_X), max(coord_Y)),
(max(coord_X), min(coord_Y)),
(min(coord_X), min(coord_Y)),
(min(coord_x), min(coord_y)),
(min(coord_x), max(coord_y)),
(max(coord_x), max(coord_y)),
(max(coord_x), min(coord_y)),
(min(coord_x), min(coord_y)),
],
name="main",
min_poly_num=1,
Expand Down Expand Up @@ -119,6 +102,7 @@ def load_file_from_path(path: str):


class BreakWatersFitness(Objective):
"""Class to init Objective for BreakWater case."""
def __init__(self, domain, estimator):
super().__init__(domain, estimator)
self.estimator = estimator
Expand All @@ -128,9 +112,7 @@ def _evaluate(self, ind: Structure):
return fitness


# fitness estimator
estimator = BreakWatersFitness(domain_cfg, swan_estimator)

opt_params = OptimizationParams(
optimizer="gefest_ga",
domain=domain_cfg,
Expand Down
28 changes: 28 additions & 0 deletions cases/breakwaters/breakwaters_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import json

# import numpy as np
# from shapely.geometry import shape


def parse_arctic_geojson(result_path, border_path, root_path):
"""Function for parsing data for breakwaters case.
:param result_path: path to result data
:param border_path: path to border info data
:param root_path: root path
:return:
"""
with open(
f"{root_path}/{result_path}", "r"
) as file:
res_list = json.load(file)

water = [i for i in res_list["features"] if i["properties"]["type"] == "water"]
water_coord = [p["geometry"]["coordinates"] for p in water]
allow_water = [
i
for i in water_coord[0][0]
if (i[0] > 74.8) and (i[1] < 67.942) and (i[1] > 67.915)
]

return allow_water

0 comments on commit 0f759e9

Please sign in to comment.