Skip to content

Instantly share code, notes, and snippets.

@ClemRz
Last active May 4, 2018 15:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ClemRz/e08d856ca93502b8fa3f16ad531110a5 to your computer and use it in GitHub Desktop.
Save ClemRz/e08d856ca93502b8fa3f16ad531110a5 to your computer and use it in GitHub Desktop.
This python script can be used in QGIS actions. It allows to run a specific code on the very las feature when clicking on a stack of features.
from PyQt4.QtCore import QSettings
class Selection(QgsMapTool):
def __init__(self, canvas, layer):
QgsMapTool.__init__(self, canvas)
self.canvas = canvas
self.layer = layer
def getFeatureRequest(self):
sr = self.searchRadiusMU(self.canvas)
r = QgsRectangle()
r.setXMinimum([%$clickx%] - sr)
r.setXMaximum([%$clickx%] + sr)
r.setYMinimum([%$clicky%] - sr)
r.setYMaximum([%$clicky%] + sr)
r = self.toLayerCoordinates(self.layer, r)
return QgsFeatureRequest().setFilterRect(r).setFlags(QgsFeatureRequest.ExactIntersect)
def getFeaures(self):
return self.layer.getFeatures(self.getFeatureRequest())
def getLastFeature(self):
feature = QgsFeature()
features = self.layer.getFeatures(self.getFeatureRequest())
for feature in features:
pass
return feature
def getLastFeatureId(self):
return self.getLastFeature().id()
def qPrint(message):
txt = unicode(message)
if txt:
QgsMessageLog.logMessage(txt, tag="pics", level=QgsMessageLog.INFO)
def getLayer():
return qgis.utils.iface.activeLayer()
def getCanvas():
return qgis.utils.iface.mapCanvas()
def getFeature():
feature = QgsFeature()
features = getLayer().getFeatures(QgsFeatureRequest([% $id %]))
features.nextFeature(feature)
return feature
if __name__=='__main__':
selection = Selection(getCanvas(), getLayer())
currentId = getFeature().id()
if selection.getLastFeatureId() == currentId:
qPrint('This is the one at the top: ' + str(currentId))
else:
qPrint('This is NOT the one at the top: ' + str(currentId))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment