Skip to content

Commit

Permalink
adds option to use selected features only; addresses #2476
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@14746 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
cfarmer committed Nov 23, 2010
1 parent 9c51929 commit d7be6dd
Showing 1 changed file with 38 additions and 19 deletions.
57 changes: 38 additions & 19 deletions python/plugins/fTools/tools/doSelectByLocation.py
Expand Up @@ -13,15 +13,22 @@ def __init__(self, iface):
# Set up the user interface from Designer.
self.setupUi(self)
self.buttonOk = self.buttonBox_2.button( QDialogButtonBox.Ok )

# populate layer list
self.progressBar.setValue(0)
mapCanvas = self.iface.mapCanvas()
layers = ftools_utils.getLayerNames([QGis.Point, QGis.Line, QGis.Polygon])
self.inPolygon.addItems(layers)
self.inPoint.addItems(layers)
self.updateUI()
self.connect(self.inPoint, SIGNAL("currentIndexChanged(QString)"), self.updateCheck)
self.cmbModify.addItems([self.tr("creating new selection"), self.tr("adding to current selection"), self.tr("removing from current selection")])

def updateCheck(self, text):
vlayer = ftools_utils.getVectorLayerByName(text)
if vlayer.selectedFeatureCount() > 0:
self.chkSelected.setChecked(True)
else:
self.chkSelected.setChecked(False)

def updateUI(self):
self.label_5.setVisible(False)
Expand All @@ -37,8 +44,11 @@ def updateUI(self):
self.label_mod.setText(self.tr("Modify current selection by:"))
self.cmbModify = QComboBox(self)
self.cmbModify.setObjectName("cmbModify")
self.gridLayout.addWidget(self.label_mod,2,0,1,1)
self.gridLayout.addWidget(self.cmbModify,3,0,1,1)
self.chkSelected = QCheckBox(self.tr("Use selected features only"), self)
self.chkSelected.setObjectName("chkSelected")
self.gridLayout.addWidget(self.chkSelected,2,0,1,1)
self.gridLayout.addWidget(self.label_mod,3,0,1,1)
self.gridLayout.addWidget(self.cmbModify,4,0,1,1)
self.resize(381, 100)

def accept(self):
Expand All @@ -50,11 +60,11 @@ def accept(self):
else:
inPoly = self.inPolygon.currentText()
inPts = self.inPoint.currentText()
self.compute(inPoly, inPts, self.cmbModify.currentText())
self.compute(inPoly, inPts, self.cmbModify.currentText(), self.chkSelected.isChecked())
self.progressBar.setValue(0)
self.buttonOk.setEnabled( True )

def compute(self, inPoly, inPts, modify):
def compute(self, inPoly, inPts, modify, selection):
inputLayer = ftools_utils.getVectorLayerByName(inPoly)
selectLayer = ftools_utils.getVectorLayerByName(inPts)
inputProvider = inputLayer.dataProvider()
Expand All @@ -68,20 +78,29 @@ def compute(self, inPoly, inPts, modify):
geom = QgsGeometry()
selectedSet = []
index = ftools_utils.createIndex(inputProvider)
#selectProvider.nextFeature(feat)
#geomLayer = QgsGeometry(feat.geometry())
self.progressBar.setMaximum(selectProvider.featureCount())

while selectProvider.nextFeature(feat):
geom = QgsGeometry(feat.geometry())
intersects = index.intersects(geom.boundingBox())
print len(intersects)
for id in intersects:
inputProvider.featureAtId(int(id), infeat, True)
tmpGeom = QgsGeometry( infeat.geometry() )
if geom.intersects(tmpGeom):
selectedSet.append(infeat.id())
self.progressBar.setValue(self.progressBar.value()+1)
if selection:
features = selectLayer.selectedFeatures()
self.progressBar.setMaximum(len(features))
for feat in features:
geom = QgsGeometry(feat.geometry())
intersects = index.intersects(geom.boundingBox())
for id in intersects:
inputProvider.featureAtId(int(id), infeat, True)
tmpGeom = QgsGeometry(infeat.geometry())
if geom.intersects(tmpGeom):
selectedSet.append(infeat.id())
self.progressBar.setValue(self.progressBar.value()+1)
else:
self.progressBar.setMaximum(selectProvider.featureCount())
while selectProvider.nextFeature(feat):
geom = QgsGeometry(feat.geometry())
intersects = index.intersects(geom.boundingBox())
for id in intersects:
inputProvider.featureAtId(int(id), infeat, True)
tmpGeom = QgsGeometry( infeat.geometry() )
if geom.intersects(tmpGeom):
selectedSet.append(infeat.id())
self.progressBar.setValue(self.progressBar.value()+1)
if modify == self.tr("adding to current selection"):
selectedSet = list(set(inputLayer.selectedFeaturesIds()).union(selectedSet))
elif modify == self.tr("removing from current selection"):
Expand Down

0 comments on commit d7be6dd

Please sign in to comment.