Wednesday, 30 August 2017

python 2.7 - Getting attribute value from vector layer after mouse click in PyQGIS?


I am trying to write a piece of PyQGIS code that runs after a mouse click to collect the Map Coordinate position, and the value of a polygon vector layer within the project under the cursor position.



The vector layer is called 'Parish Boundaries'. The attribute I would like the value of is called 'name'. The first bit in the canvasReleaseEvent makes sure that the correct layer is selected, regardless of how its named.


I then have got very confused as to how to get the attribute value.


So far, I have the following:


class FaultTool(QgsMapTool):

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

def canvasReleaseEvent(self, event):


parish_layer = None
layers = self.canvas.layers()
for each_layer in layers:
if each_layer.source() == "C:/MapData/QGIS/Base Maps/Other Layers/Parish Boundaries.shp":
parish_layer = each_layer.name()


x = event.pos().x()
y = event.pos().y()

parishName = # This is the bit I can't work out!
# something like....event.pos().attributes()[0]...?

point = self.canvas.getCoordinateTransform().toMapCoordinates(x, y)


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...