23
23
# This will get replaced with a git SHA1 when you do a git archive
24
24
__revision__ = '$Format:%H$'
25
25
26
- import os .path
27
-
28
- from PyQt4 import QtGui
29
26
from PyQt4 .QtCore import *
30
27
31
28
from qgis .core import *
39
36
40
37
from sextante .outputs .OutputVector import OutputVector
41
38
42
- from sextante .algs .ftools import FToolsUtils as utils
43
-
44
39
class SelectByLocation (GeoAlgorithm ):
45
40
46
41
INPUT = "INPUT"
47
42
INTERSECT = "INTERSECT"
48
- METHOD = "METHOD"
49
- USE_SELECTED = "USE_SELECTED"
43
+ METHOD = "METHOD"
50
44
OUTPUT = "OUTPUT"
51
45
52
46
METHODS = ["creating new selection" ,
@@ -61,63 +55,44 @@ class SelectByLocation(GeoAlgorithm):
61
55
def defineCharacteristics (self ):
62
56
self .name = "Select by location"
63
57
self .group = "Vector selection tools"
64
-
65
58
self .addParameter (ParameterVector (self .INPUT , "Layer to select from" , ParameterVector .VECTOR_TYPE_ANY ))
66
59
self .addParameter (ParameterVector (self .INTERSECT , "Additional layer (intersection layer)" , ParameterVector .VECTOR_TYPE_ANY ))
67
- self .addParameter (ParameterSelection (self .METHOD , "Modify current selection by" , self .METHODS , 0 ))
68
- self .addParameter (ParameterBoolean (self .USE_SELECTED , "Use only selected features" , False ))
69
-
60
+ self .addParameter (ParameterSelection (self .METHOD , "Modify current selection by" , self .METHODS , 0 ))
70
61
self .addOutput (OutputVector (self .OUTPUT , "Selection" , True ))
71
62
72
63
def processAlgorithm (self , progress ):
73
64
filename = self .getParameterValue (self .INPUT )
74
65
inputLayer = QGisLayers .getObjectFromUri (filename )
75
66
method = self .getParameterValue (self .METHOD )
76
- selection = self .getParameterValue (self .USE_SELECTED )
77
-
78
67
filename = self .getParameterValue (self .INTERSECT )
79
68
selectLayer = QGisLayers .getObjectFromUri (filename )
80
-
81
69
inputProvider = inputLayer .dataProvider ()
82
70
selectProvider = selectLayer .dataProvider ()
83
-
84
- index = utils . createSpatialIndex ( inputLayer )
85
-
71
+
72
+ index = QgsSpatialIndex ( )
73
+ inputProvider . rewind ()
86
74
inputProvider .select ()
87
- selectProvider .select ()
88
-
89
75
feat = QgsFeature ()
76
+ while inputProvider .nextFeature (feat ):
77
+ index .insertFeature (feat )
78
+
79
+ selectProvider .select ()
90
80
infeat = QgsFeature ()
91
81
geom = QgsGeometry ()
92
- selectedSet = []
93
-
94
- if selection :
95
- features = selectLayer .selectedFeatures ()
96
- total = 100.0 / float (len (features ))
97
- current = 0
98
- for feat in features :
99
- geom = QgsGeometry (feat .geometry ())
100
- intersects = index .intersects (geom .boundingBox ())
101
- for i in intersects :
102
- inputProvider .featureAtId (i , infeat , True )
103
- tmpGeom = QgsGeometry (infeat .geometry ())
104
- if geom .intersects (tmpGeom ):
105
- selectedSet .append (infeat .id ())
106
- current += 1
107
- progress .setPercentage (int (current * total ))
108
- else :
109
- total = 100.0 / float (selectProvider .featureCount ())
110
- current = 0
111
- while selectProvider .nextFeature (feat ):
112
- geom = QgsGeometry (feat .geometry ())
113
- intersects = index .intersects (geom .boundingBox ())
114
- for i in intersects :
115
- inputProvider .featureAtId (i , infeat , True )
116
- tmpGeom = QgsGeometry ( infeat .geometry () )
117
- if geom .intersects (tmpGeom ):
118
- selectedSet .append (infeat .id ())
119
- current += 1
120
- progress .setPercentage (int (current * total ))
82
+ selectedSet = []
83
+ current = 0
84
+ features = QGisLayers .features (selectLayer )
85
+ total = 100.0 / float (len (features ))
86
+ for feat in features :
87
+ geom = QgsGeometry (feat .geometry ())
88
+ intersects = index .intersects (geom .boundingBox ())
89
+ for i in intersects :
90
+ inputProvider .featureAtId (i , infeat , True )
91
+ tmpGeom = QgsGeometry ( infeat .geometry () )
92
+ if geom .intersects (tmpGeom ):
93
+ selectedSet .append (infeat .id ())
94
+ current += 1
95
+ progress .setPercentage (int (current * total ))
121
96
122
97
if method == 1 :
123
98
selectedSet = list (set (inputLayer .selectedFeaturesIds ()).union (selectedSet ))
0 commit comments