Skip to content

Commit

Permalink
Merge pull request #143 from MIERUNE/fix/geographic-based-scale
Browse files Browse the repository at this point in the history
scale correction for EPSG 3857
  • Loading branch information
Kanahiro authored Aug 29, 2023
2 parents a490426 + 17806f9 commit 87d1a1d
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 3 deletions.
12 changes: 10 additions & 2 deletions ui/main_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


class MainDialog(QDialog):
Expand Down Expand Up @@ -50,6 +50,11 @@ def init_ui(self):
]
)

# 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)

Expand Down Expand Up @@ -138,7 +143,7 @@ def _run(self):
self.ui.mExtentGroupBox.outputExtent().xMaximum(),
self.ui.mExtentGroupBox.outputExtent().yMaximum(),
],
"scale": iface.mapCanvas().scale(),
"scale": get_scale(),
"layers": layers_processed_successfully, # layer_0,2,5..
"assets_path": "assets",
}
Expand Down Expand Up @@ -272,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()))
25 changes: 25 additions & 0 deletions ui/main_dialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,31 @@
<item>
<widget class="QgsExtentGroupBox" name="mExtentGroupBox"/>
</item>
<item>
<layout class="QHBoxLayout" name="export_scale">
<item>
<widget class="QLabel" name="label_scale">
<property name="text">
<string>出力縮尺</string>
</property>
</widget>
</item>
<item alignment="Qt::AlignRight">
<widget class="QLabel" name="label_scale_1">
<property name="text">
<string>1:</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_scale_value">
<property name="text">
<string>1</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
Expand Down
56 changes: 55 additions & 1 deletion utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,16 @@
from typing import Union


from qgis.core import QgsRenderContext, QgsUnitTypes
from qgis.core import (
QgsRenderContext,
QgsUnitTypes,
QgsProject,
QgsRectangle,
QgsPoint,
QgsScaleCalculator,
QgsCoordinateReferenceSystem,
QgsCoordinateTransform,
)
from qgis.utils import iface


Expand Down Expand Up @@ -52,3 +61,48 @@ 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() -> 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":
canvas = iface.mapCanvas()
# 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 iface.mapCanvas().scale()

0 comments on commit 87d1a1d

Please sign in to comment.