Skip to content

Commit

Permalink
review Select by location tool
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbruy committed Oct 4, 2012
1 parent 9f13601 commit 13d660f
Showing 1 changed file with 51 additions and 35 deletions.
86 changes: 51 additions & 35 deletions python/plugins/sextante/ftools/SelectByLocation.py
@@ -1,15 +1,20 @@
from sextante.core.GeoAlgorithm import GeoAlgorithm
import os.path

from PyQt4 import QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *

from qgis.core import *
from sextante.parameters.ParameterVector import ParameterVector

from sextante.core.GeoAlgorithm import GeoAlgorithm
from sextante.core.QGisLayers import QGisLayers
from sextante.outputs.OutputVector import OutputVector

from sextante.parameters.ParameterSelection import ParameterSelection
from sextante.parameters.ParameterVector import ParameterVector
from sextante.parameters.ParameterBoolean import ParameterBoolean
from sextante.ftools import ftools_utils

from sextante.outputs.OutputVector import OutputVector

from sextante.ftools import FToolsUtils as utils

class SelectByLocation(GeoAlgorithm):

Expand All @@ -19,67 +24,78 @@ class SelectByLocation(GeoAlgorithm):
USE_SELECTED = "USE_SELECTED"
OUTPUT = "OUTPUT"

METHODS = ["creating new selection",
"adding to current selection",
"removing from current selection"]

def getIcon(self):
return QtGui.QIcon(os.path.dirname(__file__) + "/icons/select_location.png")

def defineCharacteristics(self):
self.name = "Select by location"
self.group = "Research tools"

self.addParameter(ParameterVector(self.INPUT, "Layer to select from", ParameterVector.VECTOR_TYPE_ANY))
self.addParameter(ParameterVector(self.INTERSECT, "Additional layer (intersection layer)", ParameterVector.VECTOR_TYPE_ANY))
self.addParameter(ParameterSelection(self.METHOD, "Modify current selection by", self.METHODS, 0))
self.addParameter(ParameterBoolean(self.USE_SELECTED, "Use only selected features", False))

self.addOutput(OutputVector(self.OUTPUT, "Selection", True))

def processAlgorithm(self, progress):
filename = self.getParameterValue(self.INPUT)
inputLayer = QGisLayers.getObjectFromUri(filename)
method = self.getParameterValue(self.METHOD)
selection = self.getParameterValue(self.USE_SELECTED)
filename = self.getParameterValue(SelectByLocation.INPUT)
inputLayer = QGisLayers.getObjectFromUri(filename)
filename = self.getParameterValue(SelectByLocation.INTERSECT)

filename = self.getParameterValue(self.INTERSECT)
selectLayer = QGisLayers.getObjectFromUri(filename)

inputProvider = inputLayer.dataProvider()
allAttrs = inputProvider.attributeIndexes()
inputProvider.select(allAttrs, QgsRectangle())
selectProvider = selectLayer.dataProvider()
allAttrs = selectProvider.attributeIndexes()
selectProvider.select(allAttrs, QgsRectangle())

index = utils.createSpatialIndex(inputProvider)

inputProvider.select()
selectProvider.select()

feat = QgsFeature()
infeat = QgsFeature()
geom = QgsGeometry()
selectedSet = []
index = ftools_utils.createIndex(inputProvider)

if selection:
features = selectLayer.selectedFeatures()
featurescount = len(features)
i = 0
total = 100.0 / float(len(features))
current = 0
for feat in features:
geom = QgsGeometry(feat.geometry())
intersects = index.intersects(geom.boundingBox())
for id in intersects:
inputProvider.featureAtId(int(id), infeat, True)
for i in intersects:
inputProvider.featureAtId(i, infeat, True)
tmpGeom = QgsGeometry(infeat.geometry())
if geom.intersects(tmpGeom):
selectedSet.append(infeat.id())
i += 1
progress.setPercentage(i/featurescount * 100)
current += 1
progress.setPercentage(int(current * total))
else:
featurescount = selectProvider.featureCount()
total = 100.0 / float(selectProvider.featureCount())
current = 0
while selectProvider.nextFeature(feat):
geom = QgsGeometry(feat.geometry())
intersects = index.intersects(geom.boundingBox())
i = 0
for iid in intersects:
inputProvider.featureAtId(int(iid), infeat, True)
for i in intersects:
inputProvider.featureAtId(i, infeat, True)
tmpGeom = QgsGeometry( infeat.geometry() )
if geom.intersects(tmpGeom):
selectedSet.append(infeat.id())
i += 1
progress.setPercentage(i/featurescount * 100)
current += 1
progress.setPercentage(int(current * total))

if method == 1:
selectedSet = list(set(inputLayer.selectedFeaturesIds()).union(selectedSet))
elif method == 2:
selectedSet = list(set(inputLayer.selectedFeaturesIds()).difference(selectedSet))

inputLayer.setSelectedFeatures(selectedSet)
self.setOutputValue(self.OUTPUT, filename)


def defineCharacteristics(self):
self.name = "Select by location"
self.group = "Research tools"
self.addParameter(ParameterVector(SelectByLocation.INPUT, "Layer to select from", ParameterVector.VECTOR_TYPE_ANY))
self.addParameter(ParameterVector(SelectByLocation.INTERSECT, "Additional layer (intersection layer)", ParameterVector.VECTOR_TYPE_ANY))
self.addParameter(ParameterSelection(SelectByLocation.METHOD, "Modify current selection by", ["creating new selection","adding to current selection", "removing from current selection"]))
self.addParameter(ParameterBoolean(SelectByLocation.USE_SELECTED, "Use only selected features", False))
self.addOutput(OutputVector(SelectByLocation.OUTPUT, "Selection", True))

0 comments on commit 13d660f

Please sign in to comment.