Skip to content

Commit 45ded4c

Browse files
author
cfarmer
committedNov 23, 2010
adds option to use selected features only; addresses #2476
git-svn-id: http://svn.osgeo.org/qgis/trunk@14746 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent fac2a3c commit 45ded4c

File tree

1 file changed

+38
-19
lines changed

1 file changed

+38
-19
lines changed
 

‎python/plugins/fTools/tools/doSelectByLocation.py

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,22 @@ def __init__(self, iface):
1313
# Set up the user interface from Designer.
1414
self.setupUi(self)
1515
self.buttonOk = self.buttonBox_2.button( QDialogButtonBox.Ok )
16-
1716
# populate layer list
1817
self.progressBar.setValue(0)
1918
mapCanvas = self.iface.mapCanvas()
2019
layers = ftools_utils.getLayerNames([QGis.Point, QGis.Line, QGis.Polygon])
2120
self.inPolygon.addItems(layers)
2221
self.inPoint.addItems(layers)
2322
self.updateUI()
23+
self.connect(self.inPoint, SIGNAL("currentIndexChanged(QString)"), self.updateCheck)
2424
self.cmbModify.addItems([self.tr("creating new selection"), self.tr("adding to current selection"), self.tr("removing from current selection")])
25+
26+
def updateCheck(self, text):
27+
vlayer = ftools_utils.getVectorLayerByName(text)
28+
if vlayer.selectedFeatureCount() > 0:
29+
self.chkSelected.setChecked(True)
30+
else:
31+
self.chkSelected.setChecked(False)
2532

2633
def updateUI(self):
2734
self.label_5.setVisible(False)
@@ -37,8 +44,11 @@ def updateUI(self):
3744
self.label_mod.setText(self.tr("Modify current selection by:"))
3845
self.cmbModify = QComboBox(self)
3946
self.cmbModify.setObjectName("cmbModify")
40-
self.gridLayout.addWidget(self.label_mod,2,0,1,1)
41-
self.gridLayout.addWidget(self.cmbModify,3,0,1,1)
47+
self.chkSelected = QCheckBox(self.tr("Use selected features only"), self)
48+
self.chkSelected.setObjectName("chkSelected")
49+
self.gridLayout.addWidget(self.chkSelected,2,0,1,1)
50+
self.gridLayout.addWidget(self.label_mod,3,0,1,1)
51+
self.gridLayout.addWidget(self.cmbModify,4,0,1,1)
4252
self.resize(381, 100)
4353

4454
def accept(self):
@@ -50,11 +60,11 @@ def accept(self):
5060
else:
5161
inPoly = self.inPolygon.currentText()
5262
inPts = self.inPoint.currentText()
53-
self.compute(inPoly, inPts, self.cmbModify.currentText())
63+
self.compute(inPoly, inPts, self.cmbModify.currentText(), self.chkSelected.isChecked())
5464
self.progressBar.setValue(0)
5565
self.buttonOk.setEnabled( True )
5666

57-
def compute(self, inPoly, inPts, modify):
67+
def compute(self, inPoly, inPts, modify, selection):
5868
inputLayer = ftools_utils.getVectorLayerByName(inPoly)
5969
selectLayer = ftools_utils.getVectorLayerByName(inPts)
6070
inputProvider = inputLayer.dataProvider()
@@ -68,20 +78,29 @@ def compute(self, inPoly, inPts, modify):
6878
geom = QgsGeometry()
6979
selectedSet = []
7080
index = ftools_utils.createIndex(inputProvider)
71-
#selectProvider.nextFeature(feat)
72-
#geomLayer = QgsGeometry(feat.geometry())
73-
self.progressBar.setMaximum(selectProvider.featureCount())
74-
75-
while selectProvider.nextFeature(feat):
76-
geom = QgsGeometry(feat.geometry())
77-
intersects = index.intersects(geom.boundingBox())
78-
print len(intersects)
79-
for id in intersects:
80-
inputProvider.featureAtId(int(id), infeat, True)
81-
tmpGeom = QgsGeometry( infeat.geometry() )
82-
if geom.intersects(tmpGeom):
83-
selectedSet.append(infeat.id())
84-
self.progressBar.setValue(self.progressBar.value()+1)
81+
if selection:
82+
features = selectLayer.selectedFeatures()
83+
self.progressBar.setMaximum(len(features))
84+
for feat in features:
85+
geom = QgsGeometry(feat.geometry())
86+
intersects = index.intersects(geom.boundingBox())
87+
for id in intersects:
88+
inputProvider.featureAtId(int(id), infeat, True)
89+
tmpGeom = QgsGeometry(infeat.geometry())
90+
if geom.intersects(tmpGeom):
91+
selectedSet.append(infeat.id())
92+
self.progressBar.setValue(self.progressBar.value()+1)
93+
else:
94+
self.progressBar.setMaximum(selectProvider.featureCount())
95+
while selectProvider.nextFeature(feat):
96+
geom = QgsGeometry(feat.geometry())
97+
intersects = index.intersects(geom.boundingBox())
98+
for id in intersects:
99+
inputProvider.featureAtId(int(id), infeat, True)
100+
tmpGeom = QgsGeometry( infeat.geometry() )
101+
if geom.intersects(tmpGeom):
102+
selectedSet.append(infeat.id())
103+
self.progressBar.setValue(self.progressBar.value()+1)
85104
if modify == self.tr("adding to current selection"):
86105
selectedSet = list(set(inputLayer.selectedFeaturesIds()).union(selectedSet))
87106
elif modify == self.tr("removing from current selection"):

0 commit comments

Comments
 (0)