def canvasPressEvent(self, event): """ If pressEvent point is near the layer point, enable dragging. Else show msg box """ if event.button() == Qt.LeftButton: click_point = self.toLayerCoordinates(self.lm_layer, event.pos()) if self.lm_layer.featureCount() == 1: feature_list = self.lm_layer.dataProvider().getFeatures( ) #Returns iterator to a list of one feature self.lm_feature = next( feature_list) #get first and only element in the list self.lm_point = self.lm_feature.geometry().asPoint() dist = QgsPointXY.distance(click_point, self.lm_point) tolerance = (QgsTolerance.toleranceInMapUnits( 15, self.lm_layer, self.canvas().mapSettings(), QgsTolerance.Pixels)) if dist < tolerance: #Clicked on a landmark self.clicked_on_landmark = True self.create_ghost_point() else: #Not clicked on a landmark confirmation_msg = "Do you want to move {} here? \n\n".format( self.lm_layer.name()) reply = QMessageBox.question(self.parent(), 'Movement Confirmation', confirmation_msg, QMessageBox.Yes, QMessageBox.No) if reply == QMessageBox.Yes: self.move_position(click_point)
def canvasReleaseEvent(self, event): searchRadius = (QgsTolerance.toleranceInMapUnits( 5, self.layerfrom, self.canvas().mapRenderer(), QgsTolerance.Pixels)) point = self.toMapCoordinates(event.pos()) rect = QgsRectangle() rect.setXMinimum(point.x() - searchRadius) rect.setXMaximum(point.x() + searchRadius) rect.setYMinimum(point.y() - searchRadius) rect.setYMaximum(point.y() + searchRadius) rq = QgsFeatureRequest().setFilterRect(rect) try: # Only supports the first feature # TODO build picker to select which feature to inspect feature = self.layerfrom.getFeatures(rq).next() self.band.setToGeometry(feature.geometry(), self.layerfrom) fields = self.layerto.pendingFields() newfeature = QgsFeature(fields) if self.layerto.geometryType() == QGis.Point: newfeature.setGeometry(QgsGeometry.fromPoint(point)) else: newfeature.setGeometry(QgsGeometry(feature.geometry())) #Set the default values for indx in xrange(fields.count()): newfeature[indx] = self.layerto.dataProvider().defaultValue( indx) # Assign the old values to the new feature for fieldfrom, fieldto in self.fields.iteritems(): newfeature[fieldto] = feature[fieldfrom] passed, message = self.validation_method(feature=newfeature, layerto=self.layerto) if passed: self.finished.emit(self.layerto, newfeature) else: self.band.reset() self.error.emit(message) except StopIteration: pass
def canvasReleaseEvent(self, event): searchRadius = (QgsTolerance.toleranceInMapUnits( 5, self.layerfrom, self.canvas().mapRenderer(), QgsTolerance.Pixels)) point = self.toMapCoordinates(event.pos()) rect = QgsRectangle() rect.setXMinimum(point.x() - searchRadius) rect.setXMaximum(point.x() + searchRadius) rect.setYMinimum(point.y() - searchRadius) rect.setYMaximum(point.y() + searchRadius) rq = QgsFeatureRequest().setFilterRect(rect) # Look for an existing feature first. If there is one # then we emit that back to qmap. try: feature = self.layerto.getFeatures(rq).next() self.band.setToGeometry(feature.geometry(), self.layerto) self.finished.emit(self.layerto, feature) return except StopIteration: pass try: # Only supports the first feature # TODO build picker to select which feature to inspect feature = self.layerfrom.getFeatures(rq).next() self.band.setToGeometry(feature.geometry(), self.layerfrom) fields = self.layerto.pendingFields() newfeature = QgsFeature(fields) newfeature.setGeometry(QgsGeometry(feature.geometry())) #Set the default values for indx in xrange(fields.count()): newfeature[indx] = self.layerto.dataProvider().defaultValue( indx) # Assign the old values to the new feature for fieldfrom, fieldto in self.fields.iteritems(): newfeature[fieldto] = feature[fieldfrom] self.finished.emit(self.layerto, newfeature) except StopIteration: pass
def canvasReleaseEvent(self, event): searchRadius = (QgsTolerance.toleranceInMapUnits( 5, self.layerfrom, self.canvas().mapRenderer(), QgsTolerance.Pixels)) point = self.toMapCoordinates(event.pos()) rect = QgsRectangle() rect.setXMinimum(point.x() - searchRadius) rect.setXMaximum(point.x() + searchRadius) rect.setYMinimum(point.y() - searchRadius) rect.setYMaximum(point.y() + searchRadius) rq = QgsFeatureRequest().setFilterRect(rect) # Look for an existing feature first. If there is one # then we emit that back to qmap. try: feature = self.layerto.getFeatures(rq).next() self.band.setToGeometry(feature.geometry(), self.layerto) self.finished.emit(self.layerto, feature) return except StopIteration: pass try: # Only supports the first feature # TODO build picker to select which feature to inspect feature = self.layerfrom.getFeatures(rq).next() self.band.setToGeometry(feature.geometry(), self.layerfrom) fields = self.layerto.pendingFields() newfeature = QgsFeature(fields) newfeature.setGeometry(QgsGeometry(feature.geometry())) #Set the default values for indx in xrange(fields.count()): newfeature[indx] = self.layerto.dataProvider().defaultValue( indx ) # Assign the old values to the new feature for fieldfrom, fieldto in self.fields.iteritems(): newfeature[fieldto] = feature[fieldfrom] self.finished.emit(self.layerto, newfeature) except StopIteration: pass
def canvasReleaseEvent(self, event): searchRadius = (QgsTolerance.toleranceInMapUnits( 5, self.layerfrom, self.canvas().mapRenderer(), QgsTolerance.Pixels)) point = self.toMapCoordinates(event.pos()) rect = QgsRectangle() rect.setXMinimum(point.x() - searchRadius) rect.setXMaximum(point.x() + searchRadius) rect.setYMinimum(point.y() - searchRadius) rect.setYMaximum(point.y() + searchRadius) rq = QgsFeatureRequest().setFilterRect(rect) try: # Only supports the first feature # TODO build picker to select which feature to inspect feature = self.layerfrom.getFeatures(rq).next() self.band.setToGeometry(feature.geometry(), self.layerfrom) fields = self.layerto.pendingFields() newfeature = QgsFeature(fields) if self.layerto.geometryType() == QGis.Point: newfeature.setGeometry(QgsGeometry.fromPoint(point)) else: newfeature.setGeometry(QgsGeometry(feature.geometry())) #Set the default values for indx in xrange(fields.count()): newfeature[indx] = self.layerto.dataProvider().defaultValue( indx ) # Assign the old values to the new feature for fieldfrom, fieldto in self.fields.iteritems(): newfeature[fieldto] = feature[fieldfrom] passed, message = self.validation_method(feature=newfeature, layerto=self.layerto) if passed: self.finished.emit(self.layerto, newfeature) else: self.band.reset() self.error.emit(message) except StopIteration: pass
def getFeatures(self, point): searchRadius = (QgsTolerance.toleranceInMapUnits( self.radius, self.layers[0], self.canvas.mapRenderer(), QgsTolerance.Pixels)) point = self.toMapCoordinates(point) rect = QgsRectangle() rect.setXMinimum(point.x() - searchRadius) rect.setXMaximum(point.x() + searchRadius) rect.setYMinimum(point.y() - searchRadius) rect.setYMaximum(point.y() + searchRadius) rq = QgsFeatureRequest().setFilterRect(rect) self.band.reset() for layer in self.layers: rq = QgsFeatureRequest().setFilterRect(rect) for feature in layer.getFeatures(rq): if feature.isValid(): yield feature, layer
def canvasPressEvent(self, event): """ Override of QgsMapTool mouse press event """ if event.button() == Qt.LeftButton and not self.mCtrl: if self.band: self.band.hide() self.band = None self.feature = None logger.info("layer feature count {}".format(self.layer.featureCount())) if not self.layer: return logger.info("Trying to find feature in layer") point = self.toLayerCoordinates(self.layer, event.pos()) search_radius = (QgsTolerance.toleranceInMapUnits(10, self.layer, self.canvas().mapSettings(), QgsTolerance.Pixels)) rect = QgsRectangle() rect.setXMinimum(point.x() - search_radius) rect.setXMaximum(point.x() + search_radius) rect.setYMinimum(point.y() - search_radius) rect.setYMaximum(point.y() + search_radius) rq = QgsFeatureRequest().setFilterRect(rect) f = QgsFeature() self.layer.getFeatures(rq).nextFeature(f) if f.geometry(): self.band = self.create_rubber_band() self.band.setToGeometry(f.geometry(), self.layer) self.band.show() self.startcoord = self.toMapCoordinates(event.pos()) self.feature = f self.clicked_outside_layer = False return else: self.clicked_outside_layer = True
def createFormButtons(self, projectlayers): """ Create buttons for each form that is definded """ # Remove all the old buttons for action in self.actions: self.actionGroup.removeAction(action) self.toolbar.removeAction(action) self.edittool.layers = [] self.movetool.layers = [] for layer in projectlayers: try: qgslayer = QgsMapLayerRegistry.instance().mapLayersByName(layer.name)[0] if qgslayer.type() == QgsMapLayer.RasterLayer: utils.log("We can't support raster layers for data entry") continue layer.QGISLayer = qgslayer except KeyError: utils.log("Layer not found in project") continue if 'capture' in layer.capabilities: text = layer.icontext try: tool = layer.getMaptool(self.iface.mapCanvas()) except NoMapToolConfigured: utils.log("No map tool configured") continue except ErrorInMapTool as error: self.messageBar.pushMessage("Error configuring map tool", error.message, level=QgsMessageBar.WARNING) continue # Hack until I fix it later if isinstance(tool, PointTool): add = functools.partial(self.addNewFeature, qgslayer) tool.geometryComplete.connect(add) else: tool.finished.connect(self.openForm) action = QAction(QIcon(layer.icon), text, self.mainwindow) action.setData(layer) action.setCheckable(True) action.toggled.connect(functools.partial(self.setMapTool, tool)) self.toolbar.insertAction(self.editingmodeaction, action) if not tool.isEditTool(): # Connect the GPS tools strip to the action pressed event. showgpstools = (functools.partial(self.extraaddtoolbar.showToolbar, action, None)) action.toggled.connect(showgpstools) self.actionGroup.addAction(action) self.actions.append(action) if 'edit' in layer.capabilities: # TODO Use snapping options from project radius = (QgsTolerance.toleranceInMapUnits( 10, qgslayer, self.iface.mapCanvas().mapRenderer(), QgsTolerance.Pixels)) self.edittool.addLayer(qgslayer) self.edittool.searchRadius = radius if 'move' in layer.capabilities: self.movetool.addLayer(qgslayer)