Created
June 21, 2016 21:48
-
-
Save ThomasG77/4f26bebf7a16e3716ed14f1f83946a99 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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