-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinfra3d_map_tool.py
93 lines (77 loc) · 3.79 KB
/
infra3d_map_tool.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# -*- coding: utf-8 -*-
"""
/***************************************************************************
Infra3dMapTool
A QGIS plugin
This plugin is an integration of the Infra3D application with QGIS
Generated by Plugin Builder: http://g-sherman.github.io/Qgis-Plugin-Builder/
-------------------
begin : 2022-09-28
git sha : $Format:%H$
copyright : (C) 2022 by Sourcepole AG
email : [email protected]
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
"""
from qgis.core import QgsProject, QgsSnappingUtils, QgsPointLocator, QgsPointXY, Qgis
from qgis.gui import QgsMapToolEmitPoint, QgisInterface, QgsMapMouseEvent, QgsSnapIndicator
from typing import Callable
from .marker_map_item import MarkerMapItem
from .infra3d_client import Infra3dClient
class Infra3dMapTool(QgsMapToolEmitPoint):
def __init__(self, iface: QgisInterface, infra3d_client: Infra3dClient, infra3d_marker: MarkerMapItem, callback_start_infra3d: Callable ):
super(Infra3dMapTool, self).__init__(iface.mapCanvas())
self.iface = iface
self.map_canvas = self.iface.mapCanvas()
self.snapper = QgsSnapIndicator(self.map_canvas)
self.snapper.setVisible(True)
self.locator = None
self.infra3d_client = infra3d_client
self.canvasClicked.connect(self.set_infra3d_position)
self.infra3d_marker = infra3d_marker
self.callback_start_infra3d = callback_start_infra3d
def initLocator(self):
layer = QgsProject.instance().mapLayersByName("infra3DRoad")
if len(layer) > 0:
self.locator = QgsSnappingUtils(self.map_canvas).locatorForLayer(layer[0])
def canvasMoveEvent(self, mouseEvent: QgsMapMouseEvent):
"""Custom snapper for Infra3DRoad layer
Args:
mouseEvent (QgsMapMouseEvent): Mouse event
"""
if not self.locator:
self.initLocator()
if self.locator:
self.snapper.setMatch(self.locator.nearestEdge(mouseEvent.mapPoint(), 100))
def set_infra3d_position(self, point: QgsPointXY):
"""Call the remote function `lookAt2DPosition` and set the position
in the Infra3D application to the selected point in QGIS.
"""
self.callback_start_infra3d()
if self.infra3d_marker.isVisible() is False:
self.infra3d_marker.show()
if not self.locator:
self.infra3d_client.lookAt2DPosition(point.x(), point.y())
self.infra3d_marker.setMapPosition(point)
# Get point from snapper
elif self.snapper.match().isValid():
point = self.snapper.match().point()
self.infra3d_client.lookAt2DPosition(point.x(), point.y())
self.infra3d_marker.setMapPosition(point)
else:
self.iface.messageBar().pushMessage(
"Infra3D",
self.tr(
"No image found for the selected position."
),
Qgis.MessageLevel.Critical, # type: ignore
5
)
self.snapper.setMatch(QgsPointLocator.Match())