Skip to content

Commit

Permalink
hotfix: Change the approach to watch mouse movements.
Browse files Browse the repository at this point in the history
  • Loading branch information
ciscorn committed Jul 20, 2024
1 parent 2371167 commit 4914474
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 34 deletions.
2 changes: 1 addition & 1 deletion japanese_grids/metadata.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[general]
name=Japanese Grid Mesh
version=1.4.0
version=1.4.1
qgisMinimumVersion=3.6
description=Create common grid squares used in Japan. 日本で使われている「標準地域メッシュ」および「国土基本図図郭」のポリゴンを作成できるほか、国勢調査や経済センサスなどの「地域メッシュ統計」のCSVファイルを読み込むこともできます。プロセッシングツールボックスから利用できます。また、地図キャンバス上のマウスカーソル位置の地域メッシュコードをリアルタイムに表示するパネルも搭載しています。
author=MIERUNE Inc.
Expand Down
50 changes: 17 additions & 33 deletions japanese_grids/panel.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Coordinate Panel"""
"""Panel to show the grid square code of the current mouse position."""

# Copyright (C) 2023 MIERUNE Inc.
#
Expand All @@ -17,10 +17,15 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

import re
from typing import Callable

from qgis.core import QgsCoordinateReferenceSystem, QgsCoordinateTransform, QgsProject
from qgis.gui import QgisInterface, QgsMapMouseEvent, QgsMapTool
from qgis._gui import QgsMapCanvas
from qgis.core import (
QgsCoordinateReferenceSystem,
QgsCoordinateTransform,
QgsPointXY,
QgsProject,
)
from qgis.gui import QgisInterface
from qgis.PyQt.QtCore import Qt, QTimer
from qgis.PyQt.QtWidgets import (
QDockWidget,
Expand All @@ -34,37 +39,20 @@
from japanese_grids.algorithms.utils.gridsquare_to_box import lnglat_to_grid_square_code


class MapMouseMoveTool(QgsMapTool):
def __init__(self, canvas, callback: Callable[[float, float], None]):
super().__init__(canvas)
self._canvas = canvas
self._callback = callback
self._pressed = False

def canvasPressEvent(self, event: QgsMapMouseEvent):
self._pressed = True

def canvasReleaseEvent(self, event: QgsMapMouseEvent):
self._pressed = False

def canvasMoveEvent(self, event: QgsMapMouseEvent):
if not self._pressed:
point = self.toMapCoordinates(event.pos())
self._callback(point.x(), point.y())


class CoordinatePanel:
def __init__(self, iface: QgisInterface):
self._iface = iface
self._setup()
self._current_coord = None
self._dest_crs = QgsCoordinateReferenceSystem(6668) # JGD 2011

def _handle_mousemove(self, x: float, y: float):
def _handle_mousemove(self, xy: QgsPointXY):
canvas = self._iface.mapCanvas()
source_crs = canvas.mapSettings().destinationCrs()
dest_crs = QgsCoordinateReferenceSystem(4326) # WGS 84
transform = QgsCoordinateTransform(source_crs, dest_crs, QgsProject.instance())
geog_point = transform.transform(x, y)
transform = QgsCoordinateTransform(
source_crs, self._dest_crs, QgsProject.instance()
)
geog_point = transform.transform(xy)
self._current_coord = geog_point

(geog_lng, geog_lat) = (self._current_coord.x(), self._current_coord.y())
Expand Down Expand Up @@ -102,13 +90,9 @@ def _setup(self):

self._iface.addDockWidget(Qt.RightDockWidgetArea, self._dock_widget)

canvas = self._iface.mapCanvas()
self._maptool = MapMouseMoveTool(
self._iface.mapCanvas(), self._handle_mousemove
)
canvas.setMapTool(self._maptool)
canvas: QgsMapCanvas = self._iface.mapCanvas()
canvas.xyCoordinates.connect(self._handle_mousemove)

def teardown(self):
self._iface.removeDockWidget(self._dock_widget)
del self._dock_widget
del self._maptool

0 comments on commit 4914474

Please sign in to comment.