def canvasReleaseEvent(self, event):
     x = event.pos().x()
     y = event.pos().y()
     
     # TODO layer automatisch speichern damit das polygon eine id von der db kriegt
     
     # Ugly hack to identify the clicked element. For some reasons if InfoPointTool inherits from QgsMapToolIdentify,
     # canvasRealeaseEvent isn't called.
     tool = QgsMapToolIdentify(self.canvas)
     results = tool.identify(x,y, tool.ActiveLayer, tool.AllLayers)
     if not results:
         return
     
     guiName = self.canvas.currentLayer().name()
     dbName = self.canvas.currentLayer().dataProvider().dataSourceUri().split(' ')[0].split('=')[1].replace("'", '')
     
     result = results[0]
     
     feat = result.mFeature
     attrs = feat.attributes()
     fields = feat.fields().toList()
     fieldId = None
     
     for i, attr in enumerate(attrs):
         name = fields[i].name()
         if name == 'id' and attr is not None:
             fieldId = str(attr)
             break
     
     if fieldId is None:
         QMessageBox.warning(None, "Ungespeicherte Aenderungen", "Sie muessen das Polygon zuerst speichern")
         return
     
     DynamicGuiLoader(dbName, guiName, fieldId)
Example #2
0
    def canvasReleaseEvent(self, mouseEvent):
        theLayer = self.mCanvas.currentLayer()
        if theLayer is None:
            return
        if theLayer.type() != QgsMapLayer.VectorLayer:
            return
        if theLayer.featureCount() == 0:
            return

        qgsMapToolIndentify = QgsMapToolIdentify(self.mCanvas)
        resultList = qgsMapToolIndentify.identify(mouseEvent.x(),
                                                  mouseEvent.y(), -1)
        if resultList == []:  # no feature selected, pan the canvas
            if self.mDragging == True:
                self.mCanvas.panActionEnd(mouseEvent.pos())
                self.mDragging = False
            else:  # add pan to mouse cursor
                # transform the mouse pos to map coordinates
                center = self.mCanvas.getCoordinateTransform().toMapPoint(
                    mouseEvent.x(), mouseEvent.y())
                self.mCanvas.setCenter(center)
                self.mCanvas.refresh()
        else:  # select the features
            theFeature = (resultList[0]).mFeature
            if RdbLinkObject.isMyFeature(theFeature) == True:
                nodeId = theFeature.attribute('start_node_id')
                self.mNaviDlg.addNode(nodeId)
            return
Example #3
0
class ClickTool(QgsMapToolIdentify):
    def __init__(self, canvas, dlg):
        global features
        QgsMapToolIdentify.__init__(self, canvas)
        self.canvas = canvas
        self.dlg = dlg
        self.select_id = None
        self.tool = QgsMapToolIdentify(self.canvas)

    # 滑鼠按下時動作
    def canvasPressEvent(self, event):
        global point, features
        layer = self.dlg.mMapLayerComboBox.currentLayer()
        # point類型為QgsPointXY
        point = self.toMapCoordinates(self.canvas.mouseLastXY())
        # QgsPointXY轉list
        point = list(point)
        #print("\n---\n")
        #print("click (x,y): ", point)

        # 挑最上層layer選
        features = self.tool.identify(event.x(), event.y(), [layer],
                                      self.tool.TopDownStopAtFirst)
        #print(event.x(), event.y())


        if len(features) > 0:
            # here you get the selected feature
            feature = features[0].mFeature
            #print(list(feature.geometry().asMultiPolyline()))
            self.dlg.label_coor.setText("[{:<10.1f}, {:>10.1f}]".format(point[0], point[1]))
            #print("select ID: ", self.select_id)
            #print("click x,y: ", point[0], point[1])
            QMessageBox.information(self.dlg, "提示","您點擊了座標:[{:.1f} , {:.1f}]\n該圖徵ID:{:d}"
                                    .format(point[0], point[1], feature.id()))
 def canvasReleaseEvent(self, mouseEvent): 
     theLayer = self.mCanvas.currentLayer()
     if theLayer is None:
         return
     if theLayer.type() != QgsMapLayer.VectorLayer:
         return
     if theLayer.featureCount() == 0:
         return
       
     qgsMapTollIndentify = QgsMapToolIdentify(self.mCanvas)
     resultList = qgsMapTollIndentify.identify(mouseEvent.x(), mouseEvent.y(), -1)
     if resultList == []: # no feature selected, pan the canvas
         if self.mDragging == True:
             self.mCanvas.panActionEnd(mouseEvent.pos())
             self.mDragging = False
         else: # add pan to mouse cursor
             # transform the mouse pos to map coordinates
             center = self.mCanvas.getCoordinateTransform().toMapPoint(mouseEvent.x(), mouseEvent.y())
             self.mCanvas.setCenter(center)
             self.mCanvas.refresh()
     else: # select the features
         self.removeAllSelection()
         featureIdList = []
         for oneResult in resultList:
             featureIdList.append(oneResult.mFeature.id())
         theLayer.select(featureIdList)
         dlg = self.getAppreciateDlg(theLayer, featureIdList)
         dlg.show()
         result = dlg.exec_()
         if result:
             pass
         else:
             pass
         return
Example #5
0
class PointTool(QgsMapTool):
    trigger = pyqtSignal(QgsFeature)

    def __init__(self, canvas):
        QgsMapTool.__init__(self, canvas)
        self.canvas = canvas
        self.identifier = QgsMapToolIdentify(canvas)

    def canvasPressEvent(self, e):
        x = e.pos().x()
        y = e.pos().y()

        point = self.canvas.getCoordinateTransform().toMapCoordinates(x, y)
        indentifiedFeatures = self.identifier.identify(
            e.x(), e.y(), self.identifier.TopDownAll)
        if len(indentifiedFeatures) > 0:
            self.trigger.emit(indentifiedFeatures[0].mFeature)