[refactor] reduce complexity of get_parsed_bbox_points method
Browse files- src/io/lambda_helpers.py +37 -22
src/io/lambda_helpers.py
CHANGED
@@ -62,40 +62,55 @@ def get_parsed_bbox_points(request_input: RawRequestInput) -> Dict:
|
|
62 |
ne_latlng = [float(ne.lat), float(ne.lng)]
|
63 |
sw_latlng = [float(sw.lat), float(sw.lng)]
|
64 |
new_zoom = int(request_input.zoom)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
new_prompt_list = []
|
66 |
-
for prompt in
|
67 |
app_logger.debug(f"current prompt: {type(prompt)}, value:{prompt}.")
|
68 |
new_prompt = {"type": prompt.type.value}
|
69 |
if prompt.type == "point":
|
70 |
-
|
71 |
-
app_logger.debug(f"current prompt: {type(current_point)}, value:{current_point}, label: {prompt.label}.")
|
72 |
-
new_prompt_data = [current_point['x'], current_point['y']]
|
73 |
new_prompt["label"] = prompt.label.value
|
74 |
elif prompt.type == "rectangle":
|
75 |
-
|
76 |
-
app_logger.debug(f"rectangle:: current_point_ne prompt: {type(current_point_ne)}, value:{current_point_ne}.")
|
77 |
-
current_point_sw = get_latlng_to_pixel_coordinates(ne, sw, prompt.data.sw, new_zoom, prompt.type)
|
78 |
-
app_logger.debug(f"rectangle:: current_point_sw prompt: {type(current_point_sw)}, value:{current_point_sw}.")
|
79 |
-
new_prompt_data = [
|
80 |
-
current_point_ne["x"], current_point_ne["y"],
|
81 |
-
current_point_sw["x"], current_point_sw["y"]
|
82 |
-
]
|
83 |
else:
|
84 |
msg = "Valid prompt type: 'point' or 'rectangle', not '{}'. Check RawRequestInput parsing/validation."
|
85 |
raise TypeError(msg.format(prompt.type))
|
86 |
app_logger.debug(f"new_prompt_data: {type(new_prompt_data)}, value:{new_prompt_data}.")
|
87 |
new_prompt["data"] = new_prompt_data
|
88 |
new_prompt_list.append(new_prompt)
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
99 |
|
100 |
|
101 |
def get_parsed_request_body(event: Dict) -> RawRequestInput:
|
|
|
62 |
ne_latlng = [float(ne.lat), float(ne.lng)]
|
63 |
sw_latlng = [float(sw.lat), float(sw.lng)]
|
64 |
new_zoom = int(request_input.zoom)
|
65 |
+
new_prompt_list = get_parsed_prompt_list(ne, sw, new_zoom, request_input.prompt)
|
66 |
+
|
67 |
+
app_logger.debug(f"bbox => {bbox}.")
|
68 |
+
app_logger.debug(f'request_input-prompt updated => {new_prompt_list}.')
|
69 |
+
|
70 |
+
app_logger.info("unpacking elaborated request...")
|
71 |
+
return {
|
72 |
+
"bbox": [ne_latlng, sw_latlng],
|
73 |
+
"prompt": new_prompt_list,
|
74 |
+
"zoom": new_zoom
|
75 |
+
}
|
76 |
+
|
77 |
+
|
78 |
+
def get_parsed_prompt_list(bbox_ne, bbox_sw, zoom, prompt_list):
|
79 |
new_prompt_list = []
|
80 |
+
for prompt in prompt_list:
|
81 |
app_logger.debug(f"current prompt: {type(prompt)}, value:{prompt}.")
|
82 |
new_prompt = {"type": prompt.type.value}
|
83 |
if prompt.type == "point":
|
84 |
+
new_prompt_data = get_new_prompt_data_point(bbox_ne, bbox_sw, prompt, zoom)
|
|
|
|
|
85 |
new_prompt["label"] = prompt.label.value
|
86 |
elif prompt.type == "rectangle":
|
87 |
+
new_prompt_data = get_new_prompt_data_rectangle(bbox_ne, bbox_sw, prompt, zoom)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
88 |
else:
|
89 |
msg = "Valid prompt type: 'point' or 'rectangle', not '{}'. Check RawRequestInput parsing/validation."
|
90 |
raise TypeError(msg.format(prompt.type))
|
91 |
app_logger.debug(f"new_prompt_data: {type(new_prompt_data)}, value:{new_prompt_data}.")
|
92 |
new_prompt["data"] = new_prompt_data
|
93 |
new_prompt_list.append(new_prompt)
|
94 |
+
return new_prompt_list
|
95 |
+
|
96 |
+
|
97 |
+
def get_new_prompt_data_point(bbox_ne, bbox_sw, prompt, zoom):
|
98 |
+
current_point = get_latlng_to_pixel_coordinates(bbox_ne, bbox_sw, prompt.data, zoom, prompt.type)
|
99 |
+
app_logger.debug(f"current prompt: {type(current_point)}, value:{current_point}, label: {prompt.label}.")
|
100 |
+
return [current_point['x'], current_point['y']]
|
101 |
+
|
102 |
+
|
103 |
+
def get_new_prompt_data_rectangle(bbox_ne, bbox_sw, prompt, zoom):
|
104 |
+
current_point_ne = get_latlng_to_pixel_coordinates(bbox_ne, bbox_sw, prompt.data.ne, zoom, prompt.type)
|
105 |
+
app_logger.debug(
|
106 |
+
f"rectangle:: current_point_ne prompt: {type(current_point_ne)}, value:{current_point_ne}.")
|
107 |
+
current_point_sw = get_latlng_to_pixel_coordinates(bbox_ne, bbox_sw, prompt.data.sw, zoom, prompt.type)
|
108 |
+
app_logger.debug(
|
109 |
+
f"rectangle:: current_point_sw prompt: {type(current_point_sw)}, value:{current_point_sw}.")
|
110 |
+
return [
|
111 |
+
current_point_ne["x"], current_point_ne["y"],
|
112 |
+
current_point_sw["x"], current_point_sw["y"]
|
113 |
+
]
|
114 |
|
115 |
|
116 |
def get_parsed_request_body(event: Dict) -> RawRequestInput:
|