Last active
February 9, 2022 12:09
-
-
Save thbaumann/2d2ba6f719c6dac741deddf8f7256b5b to your computer and use it in GitHub Desktop.
basic example how to write getfeatureinfo response to selected features of a vectorlayer in QGIS / pyqgis
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
from qgis.PyQt.QtCore import Qt | |
from qgis.gui import QgsMapTool | |
from qgis.utils import iface | |
class SendPointToolCoordinates(QgsMapTool): | |
""" Enable to return coordinates from clic in a layer. | |
""" | |
def __init__(self, canvas, layer): | |
""" Constructor. | |
""" | |
QgsMapTool.__init__(self, canvas) | |
self.canvas = canvas | |
self.layer = layer | |
self.setCursor(Qt.CrossCursor) | |
def canvasReleaseEvent(self, event): | |
point = self.toLayerCoordinates(self.layer, event.pos()) | |
print(point.x(), point.y()) | |
wmsLayer = iface.activeLayer() | |
# Change with your coordinates | |
ident = self.layer.dataProvider().identify(QgsPointXY(point.x(),point.y()), QgsRaster.IdentifyFormatFeature) | |
# You can look at ident.results() and the features | |
#print(ident.results()[0][0].features()) | |
# To see one feature from the getfeatureinfo behind the scene | |
fields = [f.name() for f in ident.results()[0][0].features()[0].fields()] | |
attributes = ident.results()[0][0].features()[0].attributes() | |
featureinfo_dict=dict(zip(fields, attributes)) | |
print(featureinfo_dict) | |
print(featureinfo_dict['Art']) | |
layers = QgsProject.instance().mapLayersByName('testlayer') | |
layer = layers[0] # We assume there is at least 1 | |
field_idx = layer.fields().indexOf('testfeld') | |
new_value = featureinfo_dict['Art'] | |
#version1: vectorlayer has to be in editing mode and you have to save the changes yourself | |
for feature in layer.selectedFeatures(): | |
feature["testfeld"] = new_value | |
layer.updateFeature(feature) | |
#version2: vectorlayer will be set to edit-mode and changes commited without asking you | |
#with edit(layer): | |
# for feat_id in layer.selectedFeatureIds(): | |
# print(feat_id) | |
# layer.changeAttributeValue(feat_id, field_idx, new_value) | |
layer, canvas = iface.activeLayer(), iface.mapCanvas() | |
send_point_tool_coordinates = SendPointToolCoordinates( | |
canvas, | |
layer | |
) | |
canvas.setMapTool(send_point_tool_coordinates) | |
#use: create a vectorlayer named testlayer with a field called 'testfeld' (type string). | |
#active Layer before running the code: WMS-Layer with enabled getfeatureinfo. example https://geodienste.ch/db/av_0 , layers=Aggregierte_Amtliche_Vermessung | |
#based on | |
#https://github.com/webgeodatavore/pyqgis-samples/blob/master/gui/qgis-sample-QgsMapTool.py | |
#https://gis.stackexchange.com/questions/102119/changing-the-value-of-attribute-using-qgsfeature-in-pyqgis |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment