|
| 1 | +import json |
| 2 | + |
1 | 3 | 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 |
2 | 6 | from qgis.PyQt.QtWidgets import QDialog, QMessageBox
|
3 | 7 | from qgis.utils import iface
|
4 | 8 |
|
5 | 9 | from arho_feature_template.core.update_plan import LandUsePlan, update_selected_plan
|
6 | 10 | from arho_feature_template.gui.load_plan_dialog import LoadPlanDialog
|
| 11 | +from arho_feature_template.gui.serialize_plan import SerializePlan |
7 | 12 | from arho_feature_template.utils.db_utils import get_existing_database_connection_names
|
8 |
| -from arho_feature_template.utils.misc_utils import get_layer_by_name, handle_unsaved_changes |
| 13 | +from arho_feature_template.utils.misc_utils import get_active_plan_id, get_settings, handle_unsaved_changes |
9 | 14 |
|
10 | 15 |
|
11 | 16 | class PlanManager:
|
12 | 17 | def __init__(self):
|
13 |
| - self.kaava_layer = get_layer_by_name("Kaava") |
| 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 |
14 | 38 |
|
15 | 39 | def add_new_plan(self):
|
16 | 40 | """Initiate the process to add a new plan to the Kaava layer."""
|
@@ -98,3 +122,87 @@ def commit_all_editable_layers(self):
|
98 | 122 | for layer in QgsProject.instance().mapLayers().values():
|
99 | 123 | if isinstance(layer, QgsVectorLayer) and layer.isEditable():
|
100 | 124 | layer.commitChanges()
|
| 125 | + |
| 126 | + def get_plan_json(self): |
| 127 | + """Serializes plan and plan outline to JSON""" |
| 128 | + dialog = SerializePlan() |
| 129 | + 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() |
| 132 | + |
| 133 | + plan_id = get_active_plan_id() |
| 134 | + |
| 135 | + if not plan_id: |
| 136 | + QMessageBox.critical(None, "Virhe", "Ei aktiivista kaavaa. Luo tai avaa kaava.") |
| 137 | + return |
| 138 | + |
| 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") |
| 144 | + |
| 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}") |
| 155 | + return |
| 156 | + |
| 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 |
| 171 | + |
| 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 |
| 176 | + |
| 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}") |
| 206 | + |
| 207 | + # Clean up the reply |
| 208 | + reply.deleteLater() |
0 commit comments