|
| 1 | +from __future__ import annotations |
| 2 | + |
1 | 3 | import json
|
2 | 4 |
|
3 | 5 | from qgis.core import QgsExpressionContextUtils, QgsProject, QgsVectorLayer
|
4 |
| -from qgis.PyQt.QtCore import QByteArray, QUrl |
5 |
| -from qgis.PyQt.QtNetwork import QNetworkAccessManager, QNetworkProxy, QNetworkReply, QNetworkRequest |
6 | 6 | from qgis.PyQt.QtWidgets import QDialog, QMessageBox
|
7 | 7 | from qgis.utils import iface
|
8 | 8 |
|
| 9 | +from arho_feature_template.core.lambda_service import LambdaService |
9 | 10 | from arho_feature_template.core.update_plan import LandUsePlan, update_selected_plan
|
10 | 11 | from arho_feature_template.gui.load_plan_dialog import LoadPlanDialog
|
11 | 12 | from arho_feature_template.gui.serialize_plan import SerializePlan
|
12 | 13 | from arho_feature_template.utils.db_utils import get_existing_database_connection_names
|
13 |
| -from arho_feature_template.utils.misc_utils import get_active_plan_id, get_settings, handle_unsaved_changes |
| 14 | +from arho_feature_template.utils.misc_utils import get_active_plan_id, get_layer_by_name, handle_unsaved_changes |
14 | 15 |
|
15 | 16 |
|
16 | 17 | class PlanManager:
|
17 | 18 | def __init__(self):
|
18 |
| - # Init network manager |
19 |
| - self.network_manager = QNetworkAccessManager() |
20 |
| - proxy_host, proxy_port, self.lambda_url = get_settings() |
21 |
| - |
22 |
| - # SOCKS5 Proxy |
23 |
| - proxy = QNetworkProxy() |
24 |
| - proxy.setType(QNetworkProxy.Socks5Proxy) |
25 |
| - proxy.setHostName(proxy_host) |
26 |
| - proxy.setPort(int(proxy_port)) |
27 |
| - self.network_manager.setProxy(proxy) |
28 |
| - |
29 |
| - self.kaava_layer = self.get_layer_by_name("Kaava") |
30 |
| - |
31 |
| - def get_layer_by_name(self, layer_name): |
32 |
| - """Retrieve a layer by name from the project.""" |
33 |
| - layers = QgsProject.instance().mapLayersByName(layer_name) |
34 |
| - if layers: |
35 |
| - return layers[0] |
36 |
| - iface.messageBar().pushMessage("Error", f"Layer '{layer_name}' not found", level=3) |
37 |
| - return None |
| 19 | + self.lambda_service = LambdaService() |
| 20 | + self.lambda_service.jsons_received.connect(self.save_plan_jsons) |
| 21 | + self.json_plan_path = None |
| 22 | + self.json_plan_outline_path = None |
| 23 | + self.kaava_layer = get_layer_by_name("Kaava") |
38 | 24 |
|
39 | 25 | def add_new_plan(self):
|
40 | 26 | """Initiate the process to add a new plan to the Kaava layer."""
|
@@ -127,82 +113,32 @@ def get_plan_json(self):
|
127 | 113 | """Serializes plan and plan outline to JSON"""
|
128 | 114 | dialog = SerializePlan()
|
129 | 115 | if dialog.exec_() == QDialog.Accepted:
|
130 |
| - json_plan_path = dialog.plan_path_edit.text() |
131 |
| - json_plan_outline_path = dialog.plan_outline_path_edit.text() |
| 116 | + self.json_plan_path = dialog.plan_path_edit.text() |
| 117 | + self.json_plan_outline_path = dialog.plan_outline_path_edit.text() |
132 | 118 |
|
133 | 119 | plan_id = get_active_plan_id()
|
134 |
| - |
135 | 120 | if not plan_id:
|
136 |
| - QMessageBox.critical(None, "Virhe", "Ei aktiivista kaavaa. Luo tai avaa kaava.") |
| 121 | + QMessageBox.critical(None, "Virhe", "Ei aktiivista kaavaa.") |
137 | 122 | return
|
138 | 123 |
|
139 |
| - payload = {"action": "get_plans", "plan_uuid": plan_id} |
140 |
| - payload_bytes = QByteArray(json.dumps(payload).encode("utf-8")) |
141 |
| - |
142 |
| - request = QNetworkRequest(QUrl(self.lambda_url)) |
143 |
| - request.setHeader(QNetworkRequest.ContentTypeHeader, "application/json") |
| 124 | + self.lambda_service.send_request("get_plans", plan_id) |
144 | 125 |
|
145 |
| - reply = self.network_manager.post(request, payload_bytes) |
146 |
| - |
147 |
| - # Connect the finished signal to a lambda to pass extra arguments |
148 |
| - reply.finished.connect(lambda: self._handle_response(reply, json_plan_path, json_plan_outline_path)) |
149 |
| - |
150 |
| - def _handle_response(self, reply: QNetworkReply, json_plan_path: str, json_plan_outline_path: str): |
151 |
| - """Handle the API response and process the plan and outline data.""" |
152 |
| - if reply.error() != QNetworkReply.NoError: |
153 |
| - error_string = reply.errorString() |
154 |
| - QMessageBox.critical(None, "Virhe API kutsussa", f"Virhe: {error_string}") |
| 126 | + def save_plan_jsons(self, plan_json, outline_json): |
| 127 | + """This slot saves the plan and outline JSONs to files.""" |
| 128 | + if plan_json is None or outline_json is None: |
| 129 | + QMessageBox.critical(None, "Virhe", "Kaava tai sen ulkoraja ei löytynyt.") |
155 | 130 | return
|
156 | 131 |
|
157 |
| - try: |
158 |
| - response_data = reply.readAll().data().decode("utf-8") |
159 |
| - response_json = json.loads(response_data) |
160 |
| - |
161 |
| - details = response_json.get("details") |
162 |
| - if not details: |
163 |
| - QMessageBox.warning(None, "Varoitus", "Vastauksesta puuttuu 'details' kenttä.") |
164 |
| - return |
165 |
| - |
166 |
| - # Extract plan and write it to file |
167 |
| - plan_id = get_active_plan_id() |
168 |
| - if not plan_id: |
169 |
| - QMessageBox.critical(None, "Virhe", "Ei aktiivista kaavaa.") |
170 |
| - return |
| 132 | + # Retrieve paths |
| 133 | + if self.json_plan_path is None or self.json_plan_outline_path is None: |
| 134 | + QMessageBox.critical(None, "Virhe", "Tiedostopolut eivät ole saatavilla.") |
| 135 | + return |
171 | 136 |
|
172 |
| - plan_json = details.get(plan_id) |
173 |
| - if not plan_json: |
174 |
| - QMessageBox.warning(None, "Varoitus", f"Aktiiviselle kaavalle (id: {plan_id}) ei löydy tietoja.") |
175 |
| - return |
| 137 | + # Save the JSONs |
| 138 | + with open(self.json_plan_path, "w", encoding="utf-8") as full_file: |
| 139 | + json.dump(plan_json, full_file, ensure_ascii=False, indent=2) |
176 | 140 |
|
177 |
| - with open(json_plan_path, "w", encoding="utf-8") as full_file: |
178 |
| - json.dump(plan_json, full_file, ensure_ascii=False, indent=2) |
179 |
| - |
180 |
| - # Process the geographicalArea for the outline |
181 |
| - geographical_area = plan_json.get("geographicalArea") |
182 |
| - if geographical_area: |
183 |
| - try: |
184 |
| - # Build the structured outline JSON and write to file |
185 |
| - outline_json = { |
186 |
| - "type": "Feature", |
187 |
| - "properties": {"name": "Example Polygon"}, |
188 |
| - "srid": geographical_area.get("srid"), |
189 |
| - "geometry": geographical_area.get("geometry"), |
190 |
| - } |
191 |
| - |
192 |
| - with open(json_plan_outline_path, "w", encoding="utf-8") as outline_file: |
193 |
| - json.dump(outline_json, outline_file, ensure_ascii=False, indent=2) |
194 |
| - |
195 |
| - QMessageBox.information(None, "Success", "Kaava ja sen ulkorja tallennettu onnistuneesti.") |
196 |
| - except KeyError as e: |
197 |
| - QMessageBox.critical( |
198 |
| - None, |
199 |
| - "Virhe", |
200 |
| - f"'geographicalArea' ei sisällä vaadittuja tietoja: {e}", |
201 |
| - ) |
202 |
| - else: |
203 |
| - QMessageBox.warning(None, "Varoitus", "Kenttä 'geographicalArea' puuttuu kaavan tiedoista.") |
204 |
| - except json.JSONDecodeError as e: |
205 |
| - QMessageBox.critical(None, "Virhe", f"Vastaus JSON:in purkaminen epäonnistui: {e}") |
| 141 | + with open(self.json_plan_outline_path, "w", encoding="utf-8") as outline_file: |
| 142 | + json.dump(outline_json, outline_file, ensure_ascii=False, indent=2) |
206 | 143 |
|
207 |
| - # Clean up the reply |
208 |
| - reply.deleteLater() |
| 144 | + QMessageBox.information(None, "Tallennus onnistui", "Kaava ja sen ulkoraja tallennettu onnistuneesti.") |
0 commit comments