Skip to content

Instantly share code, notes, and snippets.

@ThomasG77
Created June 21, 2016 21:48
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save ThomasG77/4f26bebf7a16e3716ed14f1f83946a99 to your computer and use it in GitHub Desktop.
# coding: utf-8
from PyQt4.QtGui import QMessageBox
from qgis.core import QgsRectangle
from qgis.gui import QgsMapToolEmitPoint
from qgis.utils import iface
canvas = iface.mapCanvas()
main_window = iface.mainWindow()
clickTool = QgsMapToolEmitPoint(canvas)
def my_custom_function(features):
print(features)
def handle_mouse_down(point, button):
""""Manage selection when clicking on the canevas."""
layers = canvas.layers()
vl_to_query = [l for l in layers if l.name() != u'my_layer_name'][0]
w = canvas.mapUnitsPerPixel() * 3
rect = QgsRectangle(
point.x() - w,
point.y() - w,
point.x() + w,
point.y() + w
)
l_rect = canvas.mapSettings().mapToLayerCoordinates(vl_to_query, rect)
vl_to_query.select(l_rect, False)
feats = vl_to_query.selectedFeatures()
vl_to_query.removeSelection()
clickTool.canvasClicked.disconnect(handle_mouse_down)
# Do operations using selected features. Here we run a function
my_custom_function(feats)
# We change the default tool to pan tool
# after the function execution on selected features
iface.actionPan().trigger()
clickTool.canvasClicked.connect(handle_mouse_down)
QMessageBox.information(
main_window,
"User dialog info",
u"Thanks to select a geographic feature"
)
canvas.setMapTool(clickTool)
@BharathGundapuneni
Copy link

When am trying to use this code getting an error like "AttributeError: class instance has no attribute 'x' ".
Can you tell me why is this happening
Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment