Geographic Information Systems Asked by Sebastiaan B on June 27, 2021
Let’s assume you have to perform different field surveys for different projects. For example, you have to log all sightings of birds for different projects.
During data entry of the bird sightings I would like to link the recorded sighting to a single projectnumber. But I would only like to enter this number once and not for each bird sighting. Once I leave the data entry mode all sightings should have been linked to the project choosen. When I perform another field survey I need to enter the project number again once I start recording new sightings. So this default value varies depending on the moment you perform a field survey.
I can define a variable and use a formula to set a default value based on the variable value. But if you forget to change the variable then all sightings are linked to the wrong project. So in this case I would like to be forced to set a default value prior to data entry. Something like a pop-up menu before data entry. Is there a way to achieve this in QGIS?
This will need some Python code to achieve it. You can connect the signal "editingStarted" - which will be emitted every time when the editstate is changing to be started - to a function, which will ask you for a project ID and set it to a project variable. Below you will find an example for that.
This script need to be run only once in your session, for example at the beginning or opening of your project. So one strategy could be, to put this script as an open-Project Python macro (see project->properties->macros):
from qgis.gui import *
from qgis.core import QgsProject, QgsExpressionContextUtils
from PyQt5.QtWidgets import *
class MyDialog(QDialog):
def __init__(self):
QDialog.__init__(self)
self.setWindowTitle("Enter Variable")
layout = QFormLayout(self)
self.v_label = QLabel("Variable", self)
self.v_field = QLineEdit(self)
self.v2_label = QLabel("Variable 2", self)
self.v2_field = QLineEdit(self)
self.ok_btn = QPushButton("OK", self)
self.ok_btn.clicked.connect(self.accept)
self.cancel_btn = QPushButton("Cancel", self)
self.cancel_btn.clicked.connect(self.reject)
btn_layout = QHBoxLayout(self)
btn_layout.addWidget(self.ok_btn)
btn_layout.addWidget(self.cancel_btn)
layout.addRow(self.v_label, self.v_field)
layout.addRow(self.v2_label, self.v2_field)
layout.addRow(btn_layout)
self.setLayout(layout)
def setNewVariable():
dialog = MyDialog()
if dialog.exec_() == QDialog.Accepted:
var = dialog.v_field.text()
var2 = dialog.v2_field.text()
project = QgsProject.instance()
QgsExpressionContextUtils.setProjectVariable(project,'myvar',var)
QgsExpressionContextUtils.setProjectVariable(project,'myvar2',var2)
def openProject():
layer = QgsProject.instance().mapLayersByName('contourlines_l')[0]
# The Signal 'editingStarted' is connected to the function 'setNewVariable'
layer.editingStarted.connect(setNewVariable)
Correct answer by eurojam on June 27, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP