34
34
from qgis .core import *
35
35
from ui_frmPointsInPolygon import Ui_Dialog
36
36
37
+
38
+ typeInt = 1
39
+ typeDouble = 2
40
+
37
41
class Dialog (QDialog , Ui_Dialog ):
38
42
39
43
def __init__ (self , iface ):
@@ -46,6 +50,9 @@ def __init__(self, iface):
46
50
self .btnClose = self .buttonBox .button ( QDialogButtonBox .Close )
47
51
48
52
QObject .connect (self .toolOut , SIGNAL ("clicked()" ), self .outFile )
53
+ QObject .connect (self .inPoint , SIGNAL ("currentIndexChanged(QString)" ), self .listPointFields )
54
+ QObject .connect (self .inPoint , SIGNAL ("activated(QString)" ), self .listPointFields )
55
+
49
56
self .progressBar .setValue (0 )
50
57
self .populateLayers ()
51
58
@@ -57,6 +64,26 @@ def populateLayers( self ):
57
64
self .inPoint .clear ()
58
65
layers = ftools_utils .getLayerNames ([QGis .Point ])
59
66
self .inPoint .addItems (layers )
67
+
68
+ def listPointFields (self ):
69
+ if self .inPoint .currentText () == "" :
70
+ pass
71
+
72
+ inPnts = ftools_utils .getVectorLayerByName (self .inPoint .currentText ())
73
+ if inPnts :
74
+ pointFieldList = ftools_utils .getFieldList (inPnts )
75
+
76
+ self .attributeList .clear ()
77
+ for field in pointFieldList :
78
+ if field .type () == QVariant .Int or field .type () == QVariant .Double :
79
+ if field .type () == QVariant .Int :
80
+ global typeInt
81
+ item = QListWidgetItem (str (field .name ()), None , typeInt )
82
+ else :
83
+ global typeDouble
84
+ item = QListWidgetItem (str (field .name ()), None , typeDouble )
85
+ item .setToolTip ("Attribute <%s> of type %s" % (field .name (), field .typeName ()))
86
+ self .attributeList .addItem (item )
60
87
61
88
def outFile (self ):
62
89
self .outShape .clear ()
@@ -84,13 +111,14 @@ def accept(self):
84
111
85
112
polyProvider = inPoly .dataProvider ()
86
113
pointProvider = inPnts .dataProvider ()
87
- if polyProvider .crs () <> pointProvider .crs ():
114
+ if polyProvider .crs () != pointProvider .crs ():
88
115
QMessageBox .warning (self , self .tr ("CRS warning!" ),
89
116
self .tr ("Warning: Input layers have non-matching CRS.\n This may cause unexpected results." ))
90
117
91
118
self .btnOk .setEnabled (False )
92
119
93
- self .workThread = PointsInPolygonThread (inPoly , inPnts , self .lnField .text (), self .outShape .text (), self .encoding )
120
+ self .workThread = PointsInPolygonThread (self , inPoly , inPnts , self .lnField .text (), self .outShape .text (), self .encoding ,
121
+ self .attributeList , self .statisticSelector )
94
122
95
123
QObject .connect (self .workThread , SIGNAL ("rangeChanged(int)" ), self .setProgressRange )
96
124
QObject .connect (self .workThread , SIGNAL ("updateProgress()" ), self .updateProgress )
@@ -141,7 +169,7 @@ def restoreGui(self):
141
169
self .btnOk .setEnabled (True )
142
170
143
171
class PointsInPolygonThread (QThread ):
144
- def __init__ ( self , inPoly , inPoints , fieldName , outPath , encoding ):
172
+ def __init__ ( self , widget , inPoly , inPoints , fieldName , outPath , encoding , attributeList , statisticSelector ):
145
173
QThread .__init__ ( self , QThread .currentThread () )
146
174
self .mutex = QMutex ()
147
175
self .stopMe = 0
@@ -152,6 +180,9 @@ def __init__( self, inPoly, inPoints, fieldName, outPath, encoding ):
152
180
self .fieldName = fieldName
153
181
self .outPath = outPath
154
182
self .encoding = encoding
183
+ self .attributeList = attributeList
184
+ self .statistics = statisticSelector .currentText ()
185
+ self .widget = widget
155
186
156
187
def run (self ):
157
188
self .mutex .lock ()
@@ -163,12 +194,24 @@ def run(self):
163
194
polyProvider = self .layerPoly .dataProvider ()
164
195
pointProvider = self .layerPoints .dataProvider ()
165
196
166
- fieldList = ftools_utils .getFieldList (self .layerPoly )
197
+ fieldList = ftools_utils .getFieldList (self .layerPoly )
167
198
index = polyProvider .fieldNameIndex (unicode (self .fieldName ))
168
199
if index == - 1 :
169
200
index = polyProvider .fields ().count ()
170
201
fieldList .append ( QgsField (unicode (self .fieldName ), QVariant .Int , "int" , 10 , 0 , self .tr ("point count field" )) )
171
202
203
+ # Add the selected vector fields to the output polygon vector layer
204
+ selectedItems = self .attributeList .selectedItems ()
205
+ for item in selectedItems :
206
+ global typeDouble
207
+ columnName = unicode (item .text () + "_" + self .statistics )
208
+ index = polyProvider .fieldNameIndex (unicode (columnName ))
209
+ if index == - 1 :
210
+ if item .type () == typeDouble or self .statistics == "mean" :
211
+ fieldList .append ( QgsField (columnName , QVariant .Double , "double" , 10 , 2 , "Value" ) )
212
+ else :
213
+ fieldList .append ( QgsField (columnName , QVariant .Int , "int" , 10 , 0 , "Value" ) )
214
+
172
215
sRs = polyProvider .crs ()
173
216
if QFile (self .outPath ).exists ():
174
217
if not QgsVectorFileWriter .deleteShapeFile (self .outPath ):
@@ -189,7 +232,7 @@ def run(self):
189
232
while polyFit .nextFeature (polyFeat ):
190
233
inGeom = polyFeat .geometry ()
191
234
atMap = polyFeat .attributes ()
192
- outFeat .setAttributes (atMap )
235
+ outFeat .setAttributes (atMap )
193
236
outFeat .setGeometry (inGeom )
194
237
195
238
count = 0
@@ -202,23 +245,45 @@ def run(self):
202
245
hasIntersection = False
203
246
204
247
if hasIntersection :
248
+ valueList = {}
249
+ for item in selectedItems :
250
+ valueList [item .text ()] = []
205
251
for p in pointList :
206
252
pointProvider .getFeatures ( QgsFeatureRequest ().setFilterFid ( p ) ).nextFeature ( pntFeat )
207
253
tmpGeom = QgsGeometry (pntFeat .geometry ())
208
254
if inGeom .intersects (tmpGeom ):
209
255
count += 1
210
-
256
+ for item in selectedItems :
257
+ valueList [item .text ()].append (pntFeat .attribute (item .text ()))
258
+
211
259
self .mutex .lock ()
212
260
s = self .stopMe
213
261
self .mutex .unlock ()
214
262
if s == 1 :
215
263
interrupted = True
216
264
break
217
265
218
- atMap .append (count )
266
+ atMap .append (count )
267
+
268
+ # Compute the statistical values for selected vector attributes
269
+ for item in selectedItems :
270
+ values = valueList [item .text ()]
271
+ if values and len (values ) > 0 :
272
+ if self .statistics == "sum" :
273
+ value = reduce (myAdder , values )
274
+ elif self .statistics == "mean" :
275
+ value = reduce (myAdder , values ) / float (len (values ))
276
+ elif self .statistics == "min" :
277
+ values .sort ()
278
+ value = values [0 ]
279
+ elif self .statistics == "max" :
280
+ values .sort ()
281
+ value = values [- 1 ]
282
+ atMap .append (value )
283
+
219
284
outFeat .setAttributes (atMap )
220
285
writer .addFeature (outFeat )
221
-
286
+
222
287
self .emit ( SIGNAL ( "updateProgress()" ) )
223
288
224
289
self .mutex .lock ()
@@ -241,3 +306,6 @@ def stop(self):
241
306
self .mutex .unlock ()
242
307
243
308
QThread .wait ( self )
309
+
310
+ def myAdder (x ,y ):
311
+ return x + y
0 commit comments