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
def canvasPressEvent(self, e): ''' override, remember feature on click and move marker (create one if not drawn yet) ''' if self._picked_feature is None: features = QgsMapToolIdentify(self.canvas).identify( e.pos().x(), e.pos().y(), self._layers, QgsMapToolIdentify.TopDownStopAtFirst) if len(features) == 0: return feature = features[0].mFeature self._picked_feature = feature.id() # there is a feature -> drag it self._dragging = True self.canvas.setCursor(self.drag_cursor) # not marked yet -> mark position if not self._marker: color = QColor(0, 0, 255) color.setAlpha(100) self._marker = QgsVertexMarker(self.canvas) self._marker.setColor(color) self._marker.setIconSize(10) self._marker.setIconType(QgsVertexMarker.ICON_CIRCLE) self._marker.setPenWidth(10) point = self.toMapCoordinates(e.pos()) self._marker.setCenter(point)
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): """ Sobrescrevendo método da QgsMapTool herdada para que execute as funcionalidades desejadas. Usamos a QgsMapToolIdentify para identificar feições de camadas no mapa. Para mais Informações consulte a documentação """ if not self.municipios_layer.isValid(): return # pega a camada de municipios layer = self.municipios_layer # Aqui estamos definindo como a identificação deve ser feita inicio_identificacao = QgsMapToolIdentify.TopDownStopAtFirst # usando a ferramenta para ver se o local onde clicamos (x,y do evento de clique) # pertence a uma feição da camada feicoes_clicadas = QgsMapToolIdentify(self.canvas).identify( event.x(), event.y(), [layer], inicio_identificacao ) # se existir, seleciona só a primeira feição retornada na identificação if len(feicoes_clicadas) > 0: municipio_selecionado = feicoes_clicadas[0].mFeature self.municipios_layer.select(municipio_selecionado.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
def run(self): """Run method that performs all the real work""" # Create the dialog with elements (after translation) and keep reference # Only create GUI ONCE in callback, so that it will only load when the plugin is started if self.first_start == True: self.first_start = False self.finish = False self.dlg = TestPluginDialog() self.maptool = QgsMapToolIdentify(self.canvas) self.clicktool = ClickTool(self.canvas, self.dlg) self.mousetool = MouseTool(self.canvas, self.dlg) # 圖層分類 (MultiLineString, MultiPolygon) layer_group = [[],[]] # 不要用self.canvas.layers() 沒打勾的layer無法獲取 layers = list(QgsProject().instance().mapLayers().values()) # dict轉list for l in layers: # QgsVectorLayer if l.wkbType() == QgsWkbTypes.MultiLineString and l.type() == QgsMapLayer.VectorLayer: layer_group[0].append(l) if l.wkbType() == QgsWkbTypes.MultiPolygon and l.type() == QgsMapLayer.VectorLayer: layer_group[1].append(l) # 控制UI連動function self.dlg.btn_start.clicked.connect(self.start_calc) self.dlg.btn_clicktool.clicked.connect(self.show_clicktool) self.dlg.btn_mousetool.clicked.connect(self.show_mousetool) self.dlg.btn_clear.clicked.connect(self.reset) self.dlg.btn_save.clicked.connect(self.save_to_shp) self.dlg.mMapLayerComboBox.layerChanged.connect(self.set_layer) self.dlg.mMapLayerComboBox_2.layerChanged.connect(self.set_layer_2) self.mousetool.featureIdentified.connect(self.identify_select) # 圖層下拉框 設定過濾規則 # 河道 self.dlg.mMapLayerComboBox.setFilters(QgsMapLayerProxyModel.VectorLayer) self.dlg.mMapLayerComboBox.setExceptedLayerList(layer_group[1]) # 只要河道不要集水區 # 集水區 self.dlg.mMapLayerComboBox_2.setFilters(QgsMapLayerProxyModel.VectorLayer) self.dlg.mMapLayerComboBox_2.setExceptedLayerList(layer_group[0]) # 只要集水區不要河道 self.update_ = Update(self.dlg) self.layer = self.dlg.mMapLayerComboBox.currentLayer() self.layer_2 = self.dlg.mMapLayerComboBox_2.currentLayer() # 進度條 self.dlg.progressBar.setMinimum(0) self.dlg.progressBar.setFormat("已計算: %d %%" % (self.dlg.progressBar.value())) # show the dialog self.dlg.show() # Run the dialog event loop result = self.dlg.exec_() # See if OK was pressed if result: self.reset() self.unset_maptool() self.dlg.label_coor.setText("請使用選取工具點擊圖上任一圖徵...")
def canvasReleaseEvent(self, mouseEvent): ''' override, emit first feature found on mouse release ''' if not self._layers: return features = QgsMapToolIdentify(self.canvas).identify( mouseEvent.x(), mouseEvent.y(), self._layers, QgsMapToolIdentify.TopDownStopAtFirst) if len(features) > 0: self.feature_picked.emit(features[0].mFeature.id())
def action_identify_triggered(self): self.maptool = QgsMapToolIdentify(self.mapCanvas) self.mapCanvas.setMapTool(self.maptool)
def __init__(self, canvas): QgsMapTool.__init__(self, canvas) self.canvas = canvas self.identifier = QgsMapToolIdentify(canvas)