29
29
from qgis .core import *
30
30
from processing .core .GeoAlgorithm import GeoAlgorithm
31
31
from processing .parameters .ParameterVector import ParameterVector
32
+ from processing .parameters .ParameterBoolean import ParameterBoolean
32
33
from processing .outputs .OutputVector import OutputVector
33
34
from processing .tools import dataobjects , vector
34
35
@@ -37,7 +38,15 @@ class ExtractByLocation(GeoAlgorithm):
37
38
38
39
INPUT = 'INPUT'
39
40
INTERSECT = 'INTERSECT'
41
+ TOUCHES = 'TOUCHES'
42
+ OVERLAPS = 'OVERLAPS'
43
+ WITHIN = 'WITHIN'
40
44
OUTPUT = 'OUTPUT'
45
+
46
+ METHODS = ['creating new selection' , 'adding to current selection' ,
47
+ 'removing from current selection' ]
48
+ opFlags = 0
49
+ operators = {'TOUCHES' :1 ,'OVERLAPS' :2 ,'WITHIN' :4 }
41
50
42
51
def defineCharacteristics (self ):
43
52
self .name = 'Extract by location'
@@ -47,6 +56,15 @@ def defineCharacteristics(self):
47
56
self .addParameter (ParameterVector (self .INTERSECT ,
48
57
'Additional layer (intersection layer)' ,
49
58
[ParameterVector .VECTOR_TYPE_ANY ]))
59
+ self .addParameter (ParameterBoolean (self .TOUCHES ,
60
+ 'Include input features that touch the selection features' ,
61
+ [True ]))
62
+ self .addParameter (ParameterBoolean (self .OVERLAPS ,
63
+ 'Include input features that overlap/cross the selection features' ,
64
+ [True ]))
65
+ self .addParameter (ParameterBoolean (self .WITHIN ,
66
+ 'Include input features completely within the selection features' ,
67
+ [True ]))
50
68
self .addOutput (OutputVector (self .OUTPUT , 'Selection' ))
51
69
52
70
def processAlgorithm (self , progress ):
@@ -55,6 +73,39 @@ def processAlgorithm(self, progress):
55
73
filename = self .getParameterValue (self .INTERSECT )
56
74
selectLayer = dataobjects .getObjectFromUri (filename )
57
75
index = vector .spatialindex (layer )
76
+
77
+ def _points_op (geomA ,geomB ):
78
+ return geomA .intersects (geomB )
79
+
80
+ def _poly_lines_op (geomA ,geomB ):
81
+ if geomA .disjoint (geomB ):
82
+ return False
83
+ intersects = False
84
+ if self .opFlags & self .operators ['TOUCHES' ]:
85
+ intersects |= geomA .touches (geomB )
86
+ if not intersects and (self .opFlags & self .operators ['OVERLAPS' ]):
87
+ if geomB .type () == QGis .Line or geomA .type () == QGis .Line :
88
+ intersects |= geomA .crosses (geomB )
89
+ intersects |= geomA .overlaps (geomB )
90
+ if not intersects and (self .opFlags & self .operators ['WITHIN' ]):
91
+ intersects |= geomA .contains (geomB )
92
+ return intersects
93
+
94
+ def _sp_operator ():
95
+ if layer .geometryType () == QGis .Point :
96
+ return _points_op
97
+ else :
98
+ return _poly_lines_op
99
+
100
+ self .opFlags = 0
101
+ if self .getParameterValue (self .TOUCHES ):
102
+ self .opFlags |= self .operators ['TOUCHES' ]
103
+ if self .getParameterValue (self .OVERLAPS ):
104
+ self .opFlags |= self .operators ['OVERLAPS' ]
105
+ if self .getParameterValue (self .WITHIN ):
106
+ self .opFlags |= self .operators ['WITHIN' ]
107
+
108
+ sp_operator = _sp_operator ()
58
109
59
110
output = self .getOutputFromName (self .OUTPUT )
60
111
writer = output .getVectorWriter (layer .pendingFields (),
@@ -73,7 +124,7 @@ def processAlgorithm(self, progress):
73
124
request = QgsFeatureRequest ().setFilterFid (i )
74
125
feat = layer .getFeatures (request ).next ()
75
126
tmpGeom = QgsGeometry (feat .geometry ())
76
- if geom . intersects ( tmpGeom ):
127
+ if sp_operator ( geom , tmpGeom ):
77
128
selectedSet .append (feat .id ())
78
129
progress .setPercentage (int (current * total ))
79
130
0 commit comments