Skip to content

Commit 5e0752f

Browse files
author
cfarmer
committed
Adds median value to summary when joining layers with numeric data
git-svn-id: http://svn.osgeo.org/qgis/trunk@14953 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent a58b6a8 commit 5e0752f

File tree

2 files changed

+168
-122
lines changed

2 files changed

+168
-122
lines changed

python/plugins/fTools/tools/doSpatialJoin.py

+30-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
22
#-----------------------------------------------------------
3-
#
3+
#
44
# Spatial Join
55
#
66
# Performing an attribute join between vector layers based on spatial
@@ -14,23 +14,23 @@
1414
# WEB : www.geog.uvic.ca/spar/carson
1515
#
1616
#-----------------------------------------------------------
17-
#
17+
#
1818
# licensed under the terms of GNU GPL 2
19-
#
19+
#
2020
# This program is free software; you can redistribute it and/or modify
2121
# it under the terms of the GNU General Public License as published by
2222
# the Free Software Foundation; either version 2 of the License, or
2323
# (at your option) any later version.
24-
#
24+
#
2525
# This program is distributed in the hope that it will be useful,
2626
# but WITHOUT ANY WARRANTY; without even the implied warranty of
2727
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2828
# GNU General Public License for more details.
29-
#
29+
#
3030
# You should have received a copy of the GNU General Public License along
3131
# with this program; if not, write to the Free Software Foundation, Inc.,
3232
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
33-
#
33+
#
3434
#---------------------------------------------------------------------
3535

3636
from PyQt4.QtCore import *
@@ -39,6 +39,23 @@
3939
from qgis.core import *
4040
from ui_frmSpatialJoin import Ui_Dialog
4141

42+
def myself(L):
43+
#median computation
44+
nVal = len(L)
45+
if nVal == 1:
46+
return L[0]
47+
L.sort()
48+
#test for list length
49+
medianVal = 0
50+
if nVal > 1:
51+
if ( nVal % 2 ) == 0:
52+
#index begin at 0
53+
#remove 1 to index in standard median computation
54+
medianVal = 0.5 * ( (L[ (nVal) / 2 - 1]) + (L[ (nVal) / 2 ] ))
55+
else:
56+
medianVal = L[ (nVal + 1) / 2 - 1]
57+
return medianVal
58+
4259
class Dialog(QDialog, Ui_Dialog):
4360

4461
def __init__(self, iface):
@@ -55,7 +72,7 @@ def __init__(self, iface):
5572
layers = ftools_utils.getLayerNames([QGis.Point, QGis.Line, QGis.Polygon])
5673
self.inShape.addItems(layers)
5774
self.joinShape.addItems(layers)
58-
75+
5976
def accept(self):
6077
self.buttonOk.setEnabled( False )
6178
if self.inShape.currentText() == "":
@@ -64,7 +81,7 @@ def accept(self):
6481
QMessageBox.information(self, self.tr("Spatial Join"), self.tr("Please specify output shapefile") )
6582
elif self.joinShape.currentText() == "":
6683
QMessageBox.information(self, self.tr("Spatial Join"), self.tr("Please specify join vector layer") )
67-
elif self.rdoSummary.isChecked() and not (self.chkMean.isChecked() or self.chkSum.isChecked() or self.chkMin.isChecked() or self.chkMax.isChecked() or self.chkMean.isChecked()):
84+
elif self.rdoSummary.isChecked() and not (self.chkMean.isChecked() or self.chkSum.isChecked() or self.chkMin.isChecked() or self.chkMax.isChecked() or self.chkMean.isChecked() or self.chkMedian.isChecked()):
6885
QMessageBox.information(self, self.tr("Spatial Join"), self.tr("Please specify at least one summary statistic") )
6986
else:
7087
inName = self.inShape.currentText()
@@ -77,6 +94,7 @@ def accept(self):
7794
if self.chkMean.isChecked(): sumList.append("MEAN")
7895
if self.chkMin.isChecked(): sumList.append("MIN")
7996
if self.chkMax.isChecked(): sumList.append("MAX")
97+
if self.chkMedian.isChecked(): sumList.append("MED")
8098
else:
8199
summary = False
82100
sumList = ["all"]
@@ -120,7 +138,7 @@ def compute(self, inName, joinName, outName, summary, sumList, keep, progressBar
120138
provider2.select(allAttrs)
121139
fieldList2 = ftools_utils.getFieldList(layer2)
122140
fieldList = []
123-
if provider1.crs() <> provider2.crs():
141+
if provider1.crs() != provider2.crs():
124142
QMessageBox.warning(self, self.tr("CRS warning!"), self.tr("Warning: Input layers have non-matching CRS.\nThis may cause unexpected results."))
125143
if not summary:
126144
fieldList2 = ftools_utils.testForUniqueness(fieldList1, fieldList2.values())
@@ -141,15 +159,15 @@ def compute(self, inName, joinName, outName, summary, sumList, keep, progressBar
141159
fieldList1.extend(fieldList)
142160
seq = range(0, len(fieldList1))
143161
fieldList1 = dict(zip(seq, fieldList1))
144-
162+
145163
# check for correct field names
146164
longNames = ftools_utils.checkFieldNameLength( fieldList1 )
147165
if not longNames.isEmpty():
148166
QMessageBox.warning( self, self.tr( 'Incorrect field names' ),
149167
self.tr( 'No output will be created.\nFollowing field names are longer than 10 characters:\n%1' )
150168
.arg( longNames.join( '\n' ) ) )
151169
return False
152-
170+
153171
sRs = provider1.crs()
154172
progressBar.setValue(13)
155173
check = QFile(self.shapefileName)
@@ -215,6 +233,7 @@ def compute(self, inName, joinName, outName, summary, sumList, keep, progressBar
215233
if k == "SUM": atMap.append(QVariant(sum(numFields[j])))
216234
elif k == "MEAN": atMap.append(QVariant(sum(numFields[j]) / count))
217235
elif k == "MIN": atMap.append(QVariant(min(numFields[j])))
236+
elif k == "MED": atMap.append(QVariant(myself(numFields[j])))
218237
else: atMap.append(QVariant(max(numFields[j])))
219238
numFields[j] = []
220239
atMap.append(QVariant(count))

0 commit comments

Comments
 (0)