Skip to content

Commit 98a7242

Browse files
committed
Integration of the modified fTools plugin into master
1 parent 49cf93d commit 98a7242

File tree

4 files changed

+258
-64
lines changed

4 files changed

+258
-64
lines changed

python/plugins/fTools/tools/doPointsInPolygon.py

+76-8
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@
3434
from qgis.core import *
3535
from ui_frmPointsInPolygon import Ui_Dialog
3636

37+
38+
typeInt = 1
39+
typeDouble = 2
40+
3741
class Dialog(QDialog, Ui_Dialog):
3842

3943
def __init__(self, iface):
@@ -46,6 +50,9 @@ def __init__(self, iface):
4650
self.btnClose = self.buttonBox.button( QDialogButtonBox.Close )
4751

4852
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+
4956
self.progressBar.setValue(0)
5057
self.populateLayers()
5158

@@ -57,6 +64,26 @@ def populateLayers( self ):
5764
self.inPoint.clear()
5865
layers = ftools_utils.getLayerNames([QGis.Point])
5966
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)
6087

6188
def outFile(self):
6289
self.outShape.clear()
@@ -84,13 +111,14 @@ def accept(self):
84111

85112
polyProvider = inPoly.dataProvider()
86113
pointProvider = inPnts.dataProvider()
87-
if polyProvider.crs() <> pointProvider.crs():
114+
if polyProvider.crs() != pointProvider.crs():
88115
QMessageBox.warning(self, self.tr("CRS warning!"),
89116
self.tr("Warning: Input layers have non-matching CRS.\nThis may cause unexpected results."))
90117

91118
self.btnOk.setEnabled(False)
92119

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)
94122

95123
QObject.connect(self.workThread, SIGNAL("rangeChanged(int)"), self.setProgressRange)
96124
QObject.connect(self.workThread, SIGNAL("updateProgress()"), self.updateProgress)
@@ -141,7 +169,7 @@ def restoreGui(self):
141169
self.btnOk.setEnabled(True)
142170

143171
class PointsInPolygonThread(QThread):
144-
def __init__( self, inPoly, inPoints, fieldName, outPath, encoding ):
172+
def __init__( self, widget, inPoly, inPoints, fieldName, outPath, encoding, attributeList, statisticSelector):
145173
QThread.__init__( self, QThread.currentThread() )
146174
self.mutex = QMutex()
147175
self.stopMe = 0
@@ -152,6 +180,9 @@ def __init__( self, inPoly, inPoints, fieldName, outPath, encoding ):
152180
self.fieldName = fieldName
153181
self.outPath = outPath
154182
self.encoding = encoding
183+
self.attributeList = attributeList
184+
self.statistics = statisticSelector.currentText()
185+
self.widget = widget
155186

156187
def run(self):
157188
self.mutex.lock()
@@ -163,12 +194,24 @@ def run(self):
163194
polyProvider = self.layerPoly.dataProvider()
164195
pointProvider = self.layerPoints.dataProvider()
165196

166-
fieldList = ftools_utils.getFieldList(self.layerPoly)
197+
fieldList = ftools_utils.getFieldList(self.layerPoly)
167198
index = polyProvider.fieldNameIndex(unicode(self.fieldName))
168199
if index == -1:
169200
index = polyProvider.fields().count()
170201
fieldList.append( QgsField(unicode(self.fieldName), QVariant.Int, "int", 10, 0, self.tr("point count field")) )
171202

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+
172215
sRs = polyProvider.crs()
173216
if QFile(self.outPath).exists():
174217
if not QgsVectorFileWriter.deleteShapeFile(self.outPath):
@@ -189,7 +232,7 @@ def run(self):
189232
while polyFit.nextFeature(polyFeat):
190233
inGeom = polyFeat.geometry()
191234
atMap = polyFeat.attributes()
192-
outFeat.setAttributes(atMap)
235+
outFeat.setAttributes(atMap)
193236
outFeat.setGeometry(inGeom)
194237

195238
count = 0
@@ -202,23 +245,45 @@ def run(self):
202245
hasIntersection = False
203246

204247
if hasIntersection:
248+
valueList = {}
249+
for item in selectedItems:
250+
valueList[item.text()] = []
205251
for p in pointList:
206252
pointProvider.getFeatures( QgsFeatureRequest().setFilterFid( p ) ).nextFeature( pntFeat )
207253
tmpGeom = QgsGeometry(pntFeat.geometry())
208254
if inGeom.intersects(tmpGeom):
209255
count += 1
210-
256+
for item in selectedItems:
257+
valueList[item.text()].append(pntFeat.attribute(item.text()))
258+
211259
self.mutex.lock()
212260
s = self.stopMe
213261
self.mutex.unlock()
214262
if s == 1:
215263
interrupted = True
216264
break
217265

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+
219284
outFeat.setAttributes(atMap)
220285
writer.addFeature(outFeat)
221-
286+
222287
self.emit( SIGNAL( "updateProgress()" ) )
223288

224289
self.mutex.lock()
@@ -241,3 +306,6 @@ def stop(self):
241306
self.mutex.unlock()
242307

243308
QThread.wait( self )
309+
310+
def myAdder(x,y):
311+
return x+y

python/plugins/fTools/tools/doVectorGrid.py

+57
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,9 @@ def accept(self):
159159
def compute( self, bound, xOffset, yOffset, polygon ):
160160
crs = None
161161
layer = ftools_utils.getMapLayerByName(unicode(self.inShape.currentText()))
162+
163+
if self.angle.value() != 0.0:
164+
bound = self.initRotation(bound)
162165

163166
if layer is None:
164167
crs = self.iface.mapCanvas().mapRenderer().destinationCrs()
@@ -204,6 +207,11 @@ def compute( self, bound, xOffset, yOffset, polygon ):
204207
while y >= bound.yMinimum():
205208
pt1 = QgsPoint(bound.xMinimum(), y)
206209
pt2 = QgsPoint(bound.xMaximum(), y)
210+
211+
if self.angle.value() != 0.0:
212+
self.rotatePoint(pt1)
213+
self.rotatePoint(pt2)
214+
207215
line = [pt1, pt2]
208216
outFeat.setGeometry(outGeom.fromPolyline(line))
209217
outFeat.setAttribute(0, idVar)
@@ -224,6 +232,11 @@ def compute( self, bound, xOffset, yOffset, polygon ):
224232
while x <= bound.xMaximum():
225233
pt1 = QgsPoint(x, bound.yMaximum())
226234
pt2 = QgsPoint(x, bound.yMinimum())
235+
236+
if self.angle.value() != 0.0:
237+
self.rotatePoint(pt1)
238+
self.rotatePoint(pt2)
239+
227240
line = [pt1, pt2]
228241
outFeat.setGeometry(outGeom.fromPolyline(line))
229242
outFeat.setAttribute(0, idVar)
@@ -244,11 +257,21 @@ def compute( self, bound, xOffset, yOffset, polygon ):
244257
while y >= bound.yMinimum():
245258
x = bound.xMinimum()
246259
while x <= bound.xMaximum():
260+
247261
pt1 = QgsPoint(x, y)
248262
pt2 = QgsPoint(x + xOffset, y)
249263
pt3 = QgsPoint(x + xOffset, y - yOffset)
250264
pt4 = QgsPoint(x, y - yOffset)
251265
pt5 = QgsPoint(x, y)
266+
267+
print self.angle.value()
268+
if self.angle.value() != 0.0:
269+
self.rotatePoint(pt1)
270+
self.rotatePoint(pt2)
271+
self.rotatePoint(pt3)
272+
self.rotatePoint(pt4)
273+
self.rotatePoint(pt5)
274+
252275
polygon = [[pt1, pt2, pt3, pt4, pt5]]
253276
outFeat.setGeometry(outGeom.fromPolygon(polygon))
254277
outFeat.setAttribute(0, idVar)
@@ -267,6 +290,40 @@ def compute( self, bound, xOffset, yOffset, polygon ):
267290
self.progressBar.setValue( 100 )
268291
del writer
269292

293+
def initRotation(self, boundBox):
294+
# calculate rotation parameters..interpreted from affine transformation plugin
295+
296+
anchorPoint = boundBox.center()
297+
# We convert the angle from degree to radiant
298+
rad = self.angle.value() * math.pi / 180.0
299+
300+
a = math.cos( rad );
301+
b = -1 * math.sin( rad );
302+
c = anchorPoint.x() - math.cos( rad ) * anchorPoint.x() + math.sin( rad ) * anchorPoint.y();
303+
d = math.sin( rad );
304+
e = math.cos( rad );
305+
f = anchorPoint.y() - math.sin( rad ) * anchorPoint.x() - math.cos( rad ) * anchorPoint.y();
306+
307+
self.rotationParams = (a,b,c,d,e,f)
308+
309+
# Rotate the bounding box to set a new extent
310+
ptMin = QgsPoint(boundBox.xMinimum(), boundBox.yMinimum())
311+
ptMax = QgsPoint(boundBox.xMaximum(), boundBox.yMaximum())
312+
313+
self.rotatePoint(ptMin)
314+
self.rotatePoint(ptMax)
315+
316+
newBoundBox = QgsRectangle(ptMin, ptMax)
317+
newBoundBox.combineExtentWith(boundBox)
318+
319+
return newBoundBox
320+
321+
def rotatePoint(self, point):
322+
x = self.rotationParams[0] * point.x() + self.rotationParams[1] * point.y() + self.rotationParams[2];
323+
y = self.rotationParams[3] * point.x() + self.rotationParams[4] * point.y() + self.rotationParams[5];
324+
point.setX(x)
325+
point.setY(y)
326+
270327
def outFile(self):
271328
self.outShape.clear()
272329
( self.shapefileName, self.encoding ) = ftools_utils.saveDialog( self )

0 commit comments

Comments
 (0)