Intent is to have two variables (QgsVectorLayer and feature.id() within said layer) as input for the Identify feature map tool in Qgis (2.6). Wondering if a feature can be selected programmatically (via PyQGIS) to be highlighted in mapCanvas()?
I have reached 'writers' block after the following:
layerID,featureID = [i for i in layerAndFeatureIDs.split("_")]
layerRef = ftools_utils.getMapLayerByName(unicode(layerID))
infoTool = QgsMapToolIdentifyFeature(self.iface.mapCanvas(),layerRef)
...?
Runs without error - but hopeful to find out if calling a specific feature within a designated layer is possible. Either by way of a feature.id(), or even the centroid of said feature as a QgsPoint reference.
This command might be relevant. It activates the tool in question, but is it possible after triggering to subsequently highlight or info-select programmatically?
self.iface.actionIdentify().trigger()
I wondered if an alternative method would be to source the feature in questions centroid. Yet upon deriving such - and (futily?) implacing that location into a simulated click event on mapCanvas(); no results were had. Although, a print of 'working' did return...
for feature in layerRef.getFeatures():
if feature.id() != int(featureID):
pass
elif feature.id() == int(featureID):
x,y = feature.geometry().centroid().asPoint()[0],feature.geometry().centroid().asPoint()[1]
self.iface.setActiveLayer(layerRef)
self.iface.actionIdentify().trigger()
virtualMouseClick = QtGui.QMouseEvent(QtCore.QEvent.MouseButtonPress,QtCore.QPoint(x,y), \
QtCore.Qt.LeftButton,QtCore.Qt.LeftButton,QtCore.Qt.NoModifier)
try:
self.iface.mapCanvas().mousePressEvent(virtualMouseClick)
print 'working'
except:
print 'not working'
After @sigeal's advice, I have given a go at implementing some things - but run into a bit of a problem. I am supposing that the sub-classed tool needs its x,y coordinates as pixel rather than map coordinate origin. Yet use of QgsMapToPixel returns '-inf' (negative infinity) values.
What might be the syntax error I am committing, and am I off target in actions on @sigeal's advice?
#-------From main class----------------------
...
for feature in layerRef.getFeatures():
if feature.id() != int(featureID):
pass
elif feature.id() == int(featureID):
point = feature.geometry().centroid().asPoint()
x,y = point[0],point[1]
deviceX,deviceY = QgsMapToPixel().transform(float(x),float(y))[0],QgsMapToPixel().transform(float(x),float(y))[1]
print deviceX,deviceY
self.iface.setActiveLayer(layerRef)
self.iface.actionIdentify().trigger()
try:
featureIdentifyTool(self.iface,layerRef,deviceX,deviceY)
print 'working'
except:
print 'not working'
...
class featureIdentifyTool(QgsMapToolIdentifyFeature):
geomIdentified = pyqtSignal(object, object)
def __init__(self, iface, layer, x, y):
QgsMapToolIdentify.__init__(self, iface.mapCanvas())
self.canvasReleaseEvent(layer,x,y)
def canvasReleaseEvent(self,layer,x,y):
results = self.identify(x, y, self.ActiveLayer, [layer], self.VectorLayer)
print results
if len(results) > 0:
self.geomIdentified.emit(results[0].mLayer, results[0].mFeature)