Selecting and Identifying Single Feature using QGIS Plugin?

by user26286   Last Updated July 12, 2019 20:22 PM

given is a layer with some nodes on it and every node has some attributes(name, id, etc...). For showing up the informations of a single node I can use the "Identify Feature Tool". Choose the tool, click the node and see the informations. I am trying to develop a Plugin with Python, which is doing almost the same: Start the Plugin, click on a node and get the informations about this node(output in the python console is enough for the first steps). Okay, so this is the status: I was following this tutorial: http://www.qgisworkshop.org/html/workshop/plugins_tutorial.html which works not for QGIS 2.0. The 'MouseClickEvent' is working, I get the X,Y-coordinates. But I am not able to select and to get the informations of a node. That is my selectFeature function, which is called by result = QObject.connect(self.clickTool, SIGNAL("canvasClicked(const QgsPoint &, Qt::MouseButton)"), self.selectFeature):

def selectFeature(self, point, button):
    #pntGeom = QgsGeometry.fromPoint(point)
    #pntBuff = pntGeom.buffer( (self.canvas.mapUnitsPerPixel() * 2),0) 
    #rect = pntBuff.boundingBox()

    layer = self.canvas.currentLayer()
    selected_features = layer.selectedFeatures()
    for i in selected_features:
        attrs = i.attributeMap()
        for (k,attr) in attrs.iteritems():
            print "%d: %s" % (k, attr.toString())

This let me see nothing. I was reading and trying out a lot of things and at the moment in feel a bit lost. I lost the overview.

Do I need these geometry functions to capture the node by clicking the mouse button?

What functions do I need to get the attributes of a single selected feature?



Answers 1


You aren't selecting anything in your code. Once you have the point click coordinates, you need to select features using:

QgsVectorLayer.select (QgsRectangle, False)

In other words, create a QgsRectangle around the click point using whatever search radius you want and then select the feature(s):

# use 5 map units for selecting
radius = 5
rect = QgsRectangle(point.x() - radius,
                    point.y() - radius,
                    point.x() + radius,
                    point.y() + radius)
layer.select(rect, False)
# process selected features
...
gsherman
gsherman
February 18, 2014 05:28 AM

Related Questions





Testing for vector joins in PyQGIS 3

Updated March 26, 2019 04:22 AM