-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImajnetMapTool.py
More file actions
202 lines (179 loc) · 11.8 KB
/
ImajnetMapTool.py
File metadata and controls
202 lines (179 loc) · 11.8 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
"""
/***************************************************************************
* *
* Map tool used to send events to the imajnet sdk *
* *
***************************************************************************/
"""
import os
from PyQt5 import QtGui, QtWidgets, uic
from PyQt5.QtCore import pyqtSignal, QUrl, QObject, QVariant, QSize, Qt, QPoint,QPointF
from PyQt5.Qt import QFileInfo, pyqtSlot, QStringListModel, QHBoxLayout,\
QPushButton, QCursor
from PyQt5.QtWebKit import QWebElement, QWebSettings
# from PyQt5.QtGui import QVBoxLayout, QShortcut, QKeySequence
from numpy import double
# from PySide import QtGui, QtCore, QtWebKit
from PyQt5.QtWidgets import QApplication, QSplitter, QVBoxLayout, QWidget
from qgis import *
from qgis.core import *
from qgis.gui import *
from qgis.utils import iface
from .ImajnetUtils import ImajnetUtils
from .ImajnetLog import ImajnetLog
from qgis.gui import QgsVertexMarker, QgsRubberBand
from .MarkerManager import MarkerManager
from .PyImajnet import PyImajnet
class ImajnetMapTool(QgsMapToolIdentify,QgsMapTool):
_imajnetPluginWindow= None
_clickMode = False
dragging=False
def __init__(self, imajnetPlugin):
QgsMapTool.__init__(self, imajnetPlugin.iface.mapCanvas())
self._imajnetPluginWindow = imajnetPlugin.dockwidget;
self.setCursor((Qt.CrossCursor))
self._imajnetPlugin=imajnetPlugin
self.dragging=False
self.currentMarkerLayer=None
self.currentMarkerId=None
def setClickMode(self, clickMode):
self._clickMode=clickMode
if self._clickMode:
self._imajnetPluginWindow.m_view.page().currentFrame().evaluateJavaScript("ImajnetPlugin.onImajnetControlPressed(jQuery(this), 'clickMode');")
else:
self._imajnetPluginWindow.m_view.page().currentFrame().evaluateJavaScript("ImajnetPlugin.onImajnetControlPressed(jQuery(this), 'closestImage');")
def canvasPressEvent(self, event):
if (event.button() == Qt.LeftButton) :
self.canvas().setCursor( (Qt.ClosedHandCursor))
def getMarkerByCanvasCoordinates(self, point, mapPoint):
for layerName,layer in self._imajnetPluginWindow._PyImajnet._markerManager._layers.items():
for fid,marker in layer.items():
if (marker.sceneBoundingRect().contains(point)):
if isinstance(marker,QgsRubberBand):
geom = marker.asGeometry()
if geom.type() == 1 : # for line markers
if geom.buffer(1,1).contains(mapPoint):
return marker
else:
if geom.contains(mapPoint):
return marker
else:
return marker
return None
def canvasMoveEvent(self, event):
if (event.buttons() == Qt.LeftButton) :
self.dragging=True
self.canvas().panAction(event)
else:
x= event.pos().x()
y= event.pos().y()
p = QPointF(x,y)
mapPoint = self.canvas().getCoordinateTransform().toMapCoordinates(x, y)
#itemAt crashes on windows
#marker = self._imajnetPlugin.iface.mapCanvas().itemAt(p);
#if marker is not None and (isinstance(marker,QgsVertexMarker) or isinstance(marker,QgsRubberBand) ):
# layerName = marker.data(MarkerManager.IMAJNET_LAYER_KEY)
# markerId = marker.data(MarkerManager.IMAJNET_MARKER_ID_KEY)
# if layerName :
# ImajnetLog.debug("hover layer item {}.{}".format(layerName,markerId))
# if self.currentMarkerLayer != layerName and self.currentMarkerId!=markerId:
# self._imajnetPluginWindow.m_view.page().currentFrame().evaluateJavaScript("onMarkerHoverInOnMap('{}',{});".format(layerName,markerId));
#
# if self.currentMarkerLayer is not None and self.currentMarkerLayer != layerName and self.currentMarkerId!=markerId:
# self._imajnetPluginWindow.m_view.page().currentFrame().evaluateJavaScript("onMarkerHoverOutOnMap('{}',{});".format(self.currentMarkerLayer,self.currentMarkerId))
# self.currentMarkerLayer=layerName
# self.currentMarkerId=markerId
#
#else:
# if self.currentMarkerLayer is not None:
# self._imajnetPluginWindow.m_view.page().currentFrame().evaluateJavaScript("onMarkerHoverOutOnMap('{}',{});".format(self.currentMarkerLayer,self.currentMarkerId))
# self.currentMarkerLayer=None
# self.currentMarkerId=None
marker = self.getMarkerByCanvasCoordinates(p,mapPoint)
if marker is not None:
layerName = marker.data(MarkerManager.IMAJNET_LAYER_KEY)
markerId = marker.data(MarkerManager.IMAJNET_MARKER_ID_KEY)
ImajnetLog.debug("hover layer item {}.{}".format(layerName,markerId))
if self.currentMarkerLayer != layerName or self.currentMarkerId!=markerId:
self._imajnetPluginWindow.m_view.page().currentFrame().evaluateJavaScript("onMarkerHoverInOnMap('{}',{});".format(layerName,markerId));
if self.currentMarkerLayer and (self.currentMarkerLayer != layerName or self.currentMarkerId!=markerId):
self._imajnetPluginWindow.m_view.page().currentFrame().evaluateJavaScript("onMarkerHoverOutOnMap('{}',{});".format(self.currentMarkerLayer,self.currentMarkerId))
self.currentMarkerLayer=layerName
self.currentMarkerId=markerId
else:
if self.currentMarkerLayer is not None:
self._imajnetPluginWindow.m_view.page().currentFrame().evaluateJavaScript("onMarkerHoverOutOnMap('{}',{});".format(self.currentMarkerLayer,self.currentMarkerId))
self.currentMarkerLayer=None
self.currentMarkerId=None
def canvasReleaseEvent(self, event):
if (event.button() == Qt.LeftButton) :
if self.dragging == True :
self.dragging=False
self.canvas().panActionEnd(event.pos())
else:
#handle marker/feature clicks
x= event.pos().x()
y= event.pos().y()
p = QPointF(x,y)
mapPoint = self.canvas().getCoordinateTransform().toMapCoordinates(x, y)
#itemAt crashes on windows
#p = QPoint( event.pos() )
#marker = self._imajnetPlugin.iface.mapCanvas().itemAt(p);
#makrerClick=False;
#if marker is not None :
# if (isinstance(marker,QgsVertexMarker) or isinstance(marker,QgsRubberBand) ):
# layerName = marker.data(MarkerManager.IMAJNET_LAYER_KEY)
# markerId = marker.data(MarkerManager.IMAJNET_MARKER_ID_KEY)
# if layerName :
# ImajnetLog.debug("click layer item {}.{}".format(layerName,markerId))
# if self.currentMarkerLayer is not None:
# self._imajnetPluginWindow.m_view.page().currentFrame().evaluateJavaScript("onMarkerHoverOutOnMap('{}',{});".format(self.currentMarkerLayer,self.currentMarkerId))
# self.currentMarkerLayer=None
# self.currentMarkerId=None
# makrerClick = self._imajnetPluginWindow.m_view.page().currentFrame().evaluateJavaScript("onMarkerClickOnMap('{}',{});".format(layerName,markerId));
# else: #possible map features
# #ImajnetLog.error("clicked type: {}".format(type(marker).__name__))
# #ImajnetLog.error("is QgsMapCanvasItem: {}".format(isinstance(marker,QgsMapCanvasItem)))
# identifyResult = self.identify(event.pos().x(),event.pos().y(),self._imajnetPluginWindow._PyImajnet.qgsPointProjectedLayers,QgsMapToolIdentify.TopDownAll)
# if len(identifyResult) > 0:
# result = identifyResult[0]
# layer=result.mLayer
# featureGeom = result.mFeature.geometry()
# #no need to get the center, we only query point layers
# jsGeom = ImajnetUtils.convertQgisGeometryToImajnetGeometry(featureGeom,layer.crs(), PyImajnet.geometryTypes[layer.geometryType()])
# makrerClick = self._imajnetPluginWindow.m_view.page().currentFrame().evaluateJavaScript("onFeatureClickOnMap('{}');".format(jsGeom));
marker = self.getMarkerByCanvasCoordinates(p,mapPoint)
makrerClick=False;
if marker is not None:
layerName = marker.data(MarkerManager.IMAJNET_LAYER_KEY)
markerId = marker.data(MarkerManager.IMAJNET_MARKER_ID_KEY)
ImajnetLog.debug("click layer item {}.{}".format(layerName,markerId))
if self.currentMarkerLayer is not None:
self._imajnetPluginWindow.m_view.page().currentFrame().evaluateJavaScript("onMarkerHoverOutOnMap('{}',{});".format(self.currentMarkerLayer,self.currentMarkerId))
self.currentMarkerLayer=None
self.currentMarkerId=None
makrerClick = self._imajnetPluginWindow.m_view.page().currentFrame().evaluateJavaScript("onMarkerClickOnMap('{}',{});".format(layerName,markerId));
else: #possible map features
#ImajnetLog.error("clicked type: {}".format(type(marker).__name__))
#ImajnetLog.error("is QgsMapCanvasItem: {}".format(isinstance(marker,QgsMapCanvasItem)))
if self._imajnetPluginWindow._PyImajnet.qgsPointProjectedLayers:
identifyResult = self.identify(event.pos().x(),event.pos().y(),self._imajnetPluginWindow._PyImajnet.qgsPointProjectedLayers,QgsMapToolIdentify.TopDownStopAtFirst)
if len(identifyResult) > 0:
result = identifyResult[0]
layer=result.mLayer
featureGeom = result.mFeature.geometry()
#no need to get the center, we only query point layers
geomType = layer.geometryType()
jsGeom = ImajnetUtils.convertQgisGeometryToImajnetGeometry(featureGeom,layer.crs(), PyImajnet.geometryTypes[geomType])
makrerClick = self._imajnetPluginWindow.m_view.page().currentFrame().evaluateJavaScript("onFeatureClickOnMap('{}');".format(jsGeom));
if not makrerClick:
point = self.canvas().getCoordinateTransform().toMapCoordinates(x, y)
imajnetCoord= ImajnetUtils.transformQgisMapCoordToImajnetCoord(self.canvas(),(point))
self._imajnetPluginWindow.m_view.page().currentFrame().evaluateJavaScript("ImajnetMap.mapClickHandler({})".format(imajnetCoord))
self.canvas().setCursor((Qt.OpenHandCursor))
#def activate(self):
# super(QgsMapTool, self).activate()
def deactivate(self):
self._imajnetPlugin.uncheckImajnetActions()
#super(QgsMapTool, self).deactivate()
#self.emit(SIGNAL("deactivated()"))