Hello!
I try to develop some Python modules for construction in landsurveying projects. Therefore these tools must be able to snap to existiing vertices in a map. I found a class,which snaps to existing vertices, settings are taken from qgis, which uses QgsMapToolEmitPoint
class ShowOnMapTool(QgsMapToolEmitPoint):
def __init__(self, iface, PointXY):
QgsMapToolEmitPoint.__init__(self, iface.mapCanvas())
self.iface = iface
self.canvas = iface.mapCanvas()
self.marker = None
self.vertex = None
self.rtnPoint = PointXY
...
def canvasPressEvent(self, event):
'''Capture the coordinate when the mouse button has been released,
format it, and copy it to the clipboard.'''
self.rtnPoint = self.snappoint(event.originalPixelPoint())
self.removeVertexMarker()
if self.marker is None:
self.marker = QgsVertexMarker(self.canvas)
self.marker.setIconSize(18)
self.marker.setPenWidth(2)
self.marker.setIconType(QgsVertexMarker.ICON_CROSS)
self.marker.setCenter(self.rtnPoint)
...
In my form I want to use the snapped coordinates in textboxes. Therefore I added the rtnPoint in the init-procedere and in the canvasPressEvent-Function listed above.
In my own class I did this:
class InputMeasurepoints:
def __init__(self, iface):
self.iface = iface
self.plugin_dir = os.path.dirname(__file__)
locale = QSettings().value('locale/userLocale')[0:2]
locale_path = os.path.join(
self.plugin_dir,
'i18n',
'InputMeasurepoints_{}.qm'.format(locale))
if os.path.exists(locale_path):
self.translator = QTranslator()
self.translator.load(locale_path)
QCoreApplication.installTranslator(self.translator)
self.actions = []
self.menu = self.tr(u'&SurveyingTools')
self.first_start = None
self.canvas = iface.mapCanvas()
self.Point=QgsPointXY()
self.ShowMapTool = ShowOnMapTool(self.iface,self.Point)
self.marker = None
self.vertex = None
...
def onMouseClick(self,button):
self.dlg.ledt_Rechts.setText(format("%.3f" %self.ShowMapTool.rtnPoint.x()))
self.dlg.ledt_Hoch.setText(format("%.3f" %self.ShowMapTool.rtnPoint.y()))
def getPoint(self):
self.canvas.setMapTool(self.ShowMapTool)
self.ShowMapTool.canvasClicked.connect(self.onMouseClick)
def run(self):
if self.first_start == True:
self.first_start = False
self.dlg = InputMeasurepointsDialog()
self.dlg.btnPickXY.clicked.connect(self.getPoint)
Nothing happens, the Programm does not reach the onMouseClick-Function, and the ShowMapTool does not stop. How can I get the rtnPoint of the ShowOnMapTool-Class, what is wrong? I found no example Code in the web, I do not understand how QgsMapToolEmitPoint and the Mouse events work together. Is there any document with simple examples?
Thank you for investing your time in a stupid beginner like me!
Sugarfox