From 9c4be869f5f8415665e8944476b31123a7ef7b55 Mon Sep 17 00:00:00 2001 From: bordoray Date: Tue, 29 Aug 2023 12:52:01 +0900 Subject: [PATCH 1/6] implement scale correction --- ui/main_dialog.py | 4 ++-- utils.py | 54 ++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 55 insertions(+), 3 deletions(-) diff --git a/ui/main_dialog.py b/ui/main_dialog.py index 7e37fb8..1a2a433 100644 --- a/ui/main_dialog.py +++ b/ui/main_dialog.py @@ -22,7 +22,7 @@ from translator.vector.label import generate_label_vector from ui.progress_dialog import ProgressDialog from translator.thread import ProcessingThread -from utils import write_json, get_tempdir +from utils import write_json, get_tempdir, get_scale_from class MainDialog(QDialog): @@ -138,7 +138,7 @@ def _run(self): self.ui.mExtentGroupBox.outputExtent().xMaximum(), self.ui.mExtentGroupBox.outputExtent().yMaximum(), ], - "scale": iface.mapCanvas().scale(), + "scale": get_scale_from(iface.mapCanvas()), "layers": layers_processed_successfully, # layer_0,2,5.. "assets_path": "assets", } diff --git a/utils.py b/utils.py index 9da5903..3c3084b 100644 --- a/utils.py +++ b/utils.py @@ -3,7 +3,17 @@ from typing import Union -from qgis.core import QgsRenderContext, QgsUnitTypes +from qgis.core import ( + QgsRenderContext, + QgsUnitTypes, + QgsProject, + QgsRectangle, + QgsPoint, + QgsScaleCalculator, + QgsCoordinateReferenceSystem, + QgsCoordinateTransform, +) +from qgis.gui import QgsMapCanvas from qgis.utils import iface @@ -52,3 +62,45 @@ def get_tempdir(output_dir: str) -> str: os.mkdir(os.path.join(output_dir, temp_dir_path)) return os.path.join(output_dir, temp_dir_path) + + +def get_scale_from(canvas: QgsMapCanvas) -> float: + if QgsProject.instance().crs().authid() == "EPSG:3857": + """correct scale value according to scale factor in case of web mercator""" + + # get map canvas center coordinates in geographic + transform = QgsCoordinateTransform( + canvas.mapSettings().destinationCrs(), + QgsCoordinateReferenceSystem("EPSG:4326"), + QgsProject.instance(), + ) + center_geographic = transform.transform(canvas.center()) + center_point = QgsPoint(center_geographic.x(), center_geographic.y()) + + # calculate scale_factor from center_point + # https://en.wikipedia.org/wiki/Mercator_projection#Scale_factor + scale_factor_x = ( + QgsProject.instance().crs().factors(center_point).parallelScale() + ) + scale_factor_y = ( + QgsProject.instance().crs().factors(center_point).meridionalScale() + ) + + # determine extension corrected with scale factor + extent = canvas.extent() + delta_x = (extent.width() * scale_factor_x) - extent.width() + delta_y = (extent.height() * scale_factor_y) - extent.height() + corrected_extent = QgsRectangle( + extent.xMinimum() - delta_x / 2, + extent.yMinimum() - delta_y / 2, + extent.xMaximum() + delta_x / 2, + extent.yMaximum() + delta_y / 2, + ) + + # calculate scale based on corrected map extent + scale_calculator = QgsScaleCalculator( + canvas.mapSettings().outputDpi(), canvas.mapUnits() + ) + return scale_calculator.calculate(corrected_extent, canvas.size().width()) + else: + return canvas.scale() From 1c5236c05f0ac4c13cfda1d3f7761f6de3a27f3f Mon Sep 17 00:00:00 2001 From: bordoray Date: Tue, 29 Aug 2023 14:24:44 +0900 Subject: [PATCH 2/6] fix variables --- ui/main_dialog.py | 4 ++-- utils.py | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/ui/main_dialog.py b/ui/main_dialog.py index 1a2a433..e57e63f 100644 --- a/ui/main_dialog.py +++ b/ui/main_dialog.py @@ -22,7 +22,7 @@ from translator.vector.label import generate_label_vector from ui.progress_dialog import ProgressDialog from translator.thread import ProcessingThread -from utils import write_json, get_tempdir, get_scale_from +from utils import write_json, get_tempdir, get_scale class MainDialog(QDialog): @@ -138,7 +138,7 @@ def _run(self): self.ui.mExtentGroupBox.outputExtent().xMaximum(), self.ui.mExtentGroupBox.outputExtent().yMaximum(), ], - "scale": get_scale_from(iface.mapCanvas()), + "scale": get_scale(), "layers": layers_processed_successfully, # layer_0,2,5.. "assets_path": "assets", } diff --git a/utils.py b/utils.py index 3c3084b..10d65b6 100644 --- a/utils.py +++ b/utils.py @@ -64,10 +64,10 @@ def get_tempdir(output_dir: str) -> str: return os.path.join(output_dir, temp_dir_path) -def get_scale_from(canvas: QgsMapCanvas) -> float: +def get_scale() -> float: if QgsProject.instance().crs().authid() == "EPSG:3857": """correct scale value according to scale factor in case of web mercator""" - + canvas = iface.mapCanvas() # get map canvas center coordinates in geographic transform = QgsCoordinateTransform( canvas.mapSettings().destinationCrs(), @@ -103,4 +103,4 @@ def get_scale_from(canvas: QgsMapCanvas) -> float: ) return scale_calculator.calculate(corrected_extent, canvas.size().width()) else: - return canvas.scale() + return iface.mapCanvas().scale() From 6de8fe96f48d023d21dfb055f37ce420511ad72d Mon Sep 17 00:00:00 2001 From: bordoray Date: Tue, 29 Aug 2023 14:26:03 +0900 Subject: [PATCH 3/6] Lint --- utils.py | 1 - 1 file changed, 1 deletion(-) diff --git a/utils.py b/utils.py index 10d65b6..380c2e9 100644 --- a/utils.py +++ b/utils.py @@ -13,7 +13,6 @@ QgsCoordinateReferenceSystem, QgsCoordinateTransform, ) -from qgis.gui import QgsMapCanvas from qgis.utils import iface From c2fb6d4cb42de93492302aa7b06232cbbd5fd94a Mon Sep 17 00:00:00 2001 From: bordoray Date: Tue, 29 Aug 2023 14:49:45 +0900 Subject: [PATCH 4/6] Doctsring --- utils.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/utils.py b/utils.py index 380c2e9..ee9f7aa 100644 --- a/utils.py +++ b/utils.py @@ -64,8 +64,11 @@ def get_tempdir(output_dir: str) -> str: def get_scale() -> float: + """get scale from map canvas. + For web mercator projection (EPSG:3857) case, + calculate scale with map extent correction according to scale factor""" + if QgsProject.instance().crs().authid() == "EPSG:3857": - """correct scale value according to scale factor in case of web mercator""" canvas = iface.mapCanvas() # get map canvas center coordinates in geographic transform = QgsCoordinateTransform( From 6c94595fd4ad3f44955274fb4d2c8de1bcc7e528 Mon Sep 17 00:00:00 2001 From: bordoray Date: Tue, 29 Aug 2023 15:33:50 +0900 Subject: [PATCH 5/6] [WIP] add scale to export in UI not work when updated --- ui/main_dialog.py | 2 ++ ui/main_dialog.ui | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/ui/main_dialog.py b/ui/main_dialog.py index e57e63f..2664e96 100644 --- a/ui/main_dialog.py +++ b/ui/main_dialog.py @@ -50,6 +50,8 @@ def init_ui(self): ] ) + self.ui.label_scale_value.setText(str(get_scale())) + # close dialog when project cleared to avoid crash: Issue #55 QgsProject.instance().cleared.connect(self.close) diff --git a/ui/main_dialog.ui b/ui/main_dialog.ui index 411453b..51e7274 100644 --- a/ui/main_dialog.ui +++ b/ui/main_dialog.ui @@ -46,6 +46,31 @@ + + + + + + 出力縮尺 + + + + + + + 1: + + + + + + + 1 + + + + + From 17806f982bf00dc2e21a749a315fe1790f123775 Mon Sep 17 00:00:00 2001 From: bordoray Date: Tue, 29 Aug 2023 15:49:47 +0900 Subject: [PATCH 6/6] update ui scale on the fly --- ui/main_dialog.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/ui/main_dialog.py b/ui/main_dialog.py index 2664e96..ac8fb3a 100644 --- a/ui/main_dialog.py +++ b/ui/main_dialog.py @@ -50,7 +50,10 @@ def init_ui(self): ] ) - self.ui.label_scale_value.setText(str(get_scale())) + # calculate export scale and show to ui + self._update_ui_scale() + # update export scale shown in ui when change map extent + iface.mapCanvas().extentsChanged.connect(self._update_ui_scale) # close dialog when project cleared to avoid crash: Issue #55 QgsProject.instance().cleared.connect(self.close) @@ -274,3 +277,6 @@ def _process_node_recursive(self, node, parent_node): if child_type == "group": self._process_node_recursive(child, item) + + def _update_ui_scale(self): + self.ui.label_scale_value.setText(str(get_scale()))