-
Notifications
You must be signed in to change notification settings - Fork 9
/
qgisToolbox.py
executable file
·98 lines (87 loc) · 3.72 KB
/
qgisToolbox.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# -*- coding: utf-8 -*-
"""
Author: Robert Moerman
Contact: [email protected]
Company: AfriSpatial
This is a collection of custom qgis tools.
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
"""
from builtins import object
from qgis.core import QgsGeometry
from qgis.gui import QgsMapToolEmitPoint
class FeatureSelector(object):
""" This tool enables the selection of a single feature or
multiple features from a vector layer, returning the feature's id or
features' ids after each selection via the captured method in the
invoking class
"""
def __init__(self, iface, layer, capturing=True, parent=None):
# initialize instance valiables
self.parent = parent # assume parent has a captured method
self.iface = iface
self.layer = layer
self.capturing = capturing
self.selected = []
# remember current tool
self.parent_tool = self.iface.mapCanvas().mapTool()
# create selection tool
self.select_tool = QgsMapToolEmitPoint(self.iface.mapCanvas())
self.select_tool.canvasClicked.connect(self.capture)
# enable capturing if allowed
if capturing:
self.iface.mapCanvas().setMapTool(self.select_tool)
# clear selection
self.layer.removeSelection()
def enable_capturing(self):
""" Enable feature selection
"""
self.capturing = True
self.iface.mapCanvas().setMapTool(self.select_tool)
def disable_capturing(self):
""" Disable feature selection
"""
self.capturing = False
self.iface.mapCanvas().setMapTool(self.parent_tool)
def clear_selection(self):
""" Clear feature selection
"""
self.selected = []
self.layer.removeSelection()
def append_selection(self, id):
""" Append a feature to the list of currently selected features
"""
# toggle selection
self.selected.append(id)
# notify parent of changed selection
self.parent.captured(self.selected)
def capture(self, point, button):
""" Capture id of feature selected by the selector tool
"""
# check that capturing has been enabled
if self.capturing:
point_geometry = QgsGeometry.fromPointXY(point)
point_buffer = point_geometry.buffer(
(self.iface.mapCanvas().mapUnitsPerPixel() * 4), 0)
point_rectangle = point_buffer.boundingBox()
self.layer.invertSelectionInRectangle(point_rectangle)
if bool(self.layer.selectedFeatureIds()):
for id in self.layer.selectedFeatureIds():
if id not in self.selected:
self.selected.append(id)
selected = self.selected
for id in selected:
if id not in self.layer.selectedFeatureIds():
self.selected.remove(id)
self.parent.captured(self.selected)
# self.layer.select([], point_rectangle, True, True)
# feat = QgsFeature()
# while self.layer.nextFeature(feat):
# self.append_selection(feat.id())
# break