2
2
#-----------------------------------------------------------
3
3
#
4
4
# fTools
5
- # Copyright (C) 2008-2011 Carson Farmer
5
+ # Copyright (C) 2008-2011 Carson Farmer, edited and improved by Giovanni Allegri (2014)
6
6
# EMAIL: carson.farmer (at) gmail.com
7
7
# WEB : http://www.ftools.ca/fTools.html
8
8
#
32
32
from PyQt4 .QtGui import *
33
33
import ftools_utils
34
34
from qgis .core import *
35
- from ui_frmPointsInPolygon import Ui_Dialog
35
+ from ui_frmSelectByLocation import Ui_Dialog
36
36
37
37
class Dialog (QDialog , Ui_Dialog ):
38
+ TOUCH = 1
39
+ OVERLAP = 2
40
+ WITHIN = 4
38
41
39
42
def __init__ (self , iface ):
40
43
QDialog .__init__ (self , iface .mainWindow ())
44
+ self .opFlags = 0
41
45
self .iface = iface
42
46
# Set up the user interface from Designer.
43
47
self .setupUi (self )
@@ -48,7 +52,6 @@ def __init__(self, iface):
48
52
layers = ftools_utils .getLayerNames ([QGis .Point , QGis .Line , QGis .Polygon ])
49
53
self .inPolygon .addItems (layers )
50
54
self .inPoint .addItems (layers )
51
- self .updateUI ()
52
55
self .connect (self .inPoint , SIGNAL ("currentIndexChanged(QString)" ), self .updateCheck )
53
56
self .cmbModify .addItems ([self .tr ("creating new selection" ), self .tr ("adding to current selection" ), self .tr ("removing from current selection" )])
54
57
@@ -59,50 +62,63 @@ def updateCheck(self, text):
59
62
else :
60
63
self .chkSelected .setChecked (False )
61
64
62
- def updateUI (self ):
63
- self .label_5 .setVisible (False )
64
- self .lnField .setVisible (False )
65
- self .outShape .setVisible (False )
66
- self .toolOut .setVisible (False )
67
- self .label_2 .setVisible (False )
68
- self .setWindowTitle (self .tr ("Select by location" ))
69
- self .label_3 .setText (self .tr ("Select features in:" ))
70
- self .label_4 .setText (self .tr ("that intersect features in:" ))
71
- self .label_mod = QLabel (self )
72
- self .label_mod .setObjectName ("label_mod" )
73
- self .label_mod .setText (self .tr ("Modify current selection by:" ))
74
- self .cmbModify = QComboBox (self )
75
- self .cmbModify .setObjectName ("cmbModify" )
76
- self .chkSelected = QCheckBox (self .tr ("Use selected features only" ), self )
77
- self .chkSelected .setObjectName ("chkSelected" )
78
- self .gridLayout .addWidget (self .chkSelected ,2 ,0 ,1 ,1 )
79
- self .gridLayout .addWidget (self .label_mod ,3 ,0 ,1 ,1 )
80
- self .gridLayout .addWidget (self .cmbModify ,4 ,0 ,1 ,1 )
81
- self .resize (381 , 100 )
82
-
83
65
def accept (self ):
84
66
self .buttonOk .setEnabled ( False )
85
67
if self .inPolygon .currentText () == "" :
86
68
QMessageBox .information (self , self .tr ("Select by location" ), self .tr ( "Please specify input layer" ))
87
69
elif self .inPoint .currentText () == "" :
88
70
QMessageBox .information (self , self .tr ("Select by location" ), self .tr ("Please specify select layer" ))
89
71
else :
90
- inPoly = self .inPolygon .currentText ()
91
- inPts = self .inPoint .currentText ()
92
- self .compute (inPoly , inPts , self .cmbModify .currentText (), self .chkSelected .isChecked ())
72
+ inLayer = self .inPolygon .currentText ()
73
+ selLayer = self .inPoint .currentText ()
74
+ self .compute (inLayer , selLayer , self .cmbModify .currentText (), self .chkSelected .isChecked ())
93
75
self .progressBar .setValue (0 )
94
76
self .buttonOk .setEnabled ( True )
95
77
96
- def compute (self , inPoly , inPts , modify , selection ):
97
- inputLayer = ftools_utils .getVectorLayerByName (inPoly )
98
- selectLayer = ftools_utils .getVectorLayerByName (inPts )
78
+ def compute (self , inLayer , selLayer , modify , selection ):
79
+ inputLayer = ftools_utils .getVectorLayerByName (inLayer )
80
+ selectLayer = ftools_utils .getVectorLayerByName (selLayer )
99
81
inputProvider = inputLayer .dataProvider ()
100
82
selectProvider = selectLayer .dataProvider ()
101
83
feat = QgsFeature ()
102
84
infeat = QgsFeature ()
103
85
geom = QgsGeometry ()
104
86
selectedSet = []
105
87
index = ftools_utils .createIndex (inputProvider )
88
+
89
+ def _points_op (geomA ,geomB ):
90
+ return geomA .intersects (geomB )
91
+
92
+ def _poly_lines_op (geomA ,geomB ):
93
+ if geomA .disjoint (geomB ):
94
+ return False
95
+ intersects = False
96
+ if self .opFlags & self .TOUCH :
97
+ intersects |= geomA .touches (geomB )
98
+ if not intersects and (self .opFlags & self .OVERLAP ):
99
+ if geomB .type () == QGis .Line or geomA .type () == QGis .Line :
100
+ intersects |= geomA .crosses (geomB )
101
+ else :
102
+ intersects |= geomA .overlaps (geomB )
103
+ if not intersects and (self .opFlags & self .WITHIN ):
104
+ intersects |= geomA .contains (geomB )
105
+ return intersects
106
+
107
+ def _sp_operator ():
108
+ if inputLayer .geometryType () == QGis .Point :
109
+ return _points_op
110
+ else :
111
+ return _poly_lines_op
112
+
113
+ self .opFlags = 0
114
+ if self .chkTouches .checkState () == Qt .Checked :
115
+ self .opFlags |= self .TOUCH
116
+ if self .chkOverlaps .checkState () == Qt .Checked :
117
+ self .opFlags |= self .OVERLAP
118
+ if self .chkContains .checkState () == Qt .Checked :
119
+ self .opFlags |= self .WITHIN
120
+
121
+ sp_operator = _sp_operator ()
106
122
107
123
if selection :
108
124
features = selectLayer .selectedFeatures ()
@@ -113,7 +129,7 @@ def compute(self, inPoly, inPts, modify, selection):
113
129
for id in intersects :
114
130
inputProvider .getFeatures ( QgsFeatureRequest ().setFilterFid ( int (id ) ) ).nextFeature ( infeat )
115
131
tmpGeom = QgsGeometry (infeat .geometry ())
116
- if geom . intersects ( tmpGeom ):
132
+ if sp_operator ( geom , tmpGeom ):
117
133
selectedSet .append (infeat .id ())
118
134
self .progressBar .setValue (self .progressBar .value ()+ 1 )
119
135
else :
@@ -125,7 +141,7 @@ def compute(self, inPoly, inPts, modify, selection):
125
141
for id in intersects :
126
142
inputProvider .getFeatures ( QgsFeatureRequest ().setFilterFid ( int (id ) ) ).nextFeature ( infeat )
127
143
tmpGeom = QgsGeometry ( infeat .geometry () )
128
- if geom . intersects ( tmpGeom ):
144
+ if sp_operator ( geom , tmpGeom ):
129
145
selectedSet .append (infeat .id ())
130
146
self .progressBar .setValue (self .progressBar .value ()+ 1 )
131
147
if modify == self .tr ("adding to current selection" ):
0 commit comments