diff --git a/python/plugins/fTools/tools/doSpatialJoin.py b/python/plugins/fTools/tools/doSpatialJoin.py index 0845ac840c0..f435478cb52 100755 --- a/python/plugins/fTools/tools/doSpatialJoin.py +++ b/python/plugins/fTools/tools/doSpatialJoin.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- #----------------------------------------------------------- -# +# # Spatial Join # # Performing an attribute join between vector layers based on spatial @@ -14,23 +14,23 @@ # WEB : www.geog.uvic.ca/spar/carson # #----------------------------------------------------------- -# +# # licensed under the terms of GNU GPL 2 -# +# # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. -# +# # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. -# +# # You should have received a copy of the GNU General Public License along # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. -# +# #--------------------------------------------------------------------- from PyQt4.QtCore import * @@ -39,6 +39,23 @@ import ftools_utils from qgis.core import * from ui_frmSpatialJoin import Ui_Dialog +def myself(L): + #median computation + nVal = len(L) + if nVal == 1: + return L[0] + L.sort() + #test for list length + medianVal = 0 + if nVal > 1: + if ( nVal % 2 ) == 0: + #index begin at 0 + #remove 1 to index in standard median computation + medianVal = 0.5 * ( (L[ (nVal) / 2 - 1]) + (L[ (nVal) / 2 ] )) + else: + medianVal = L[ (nVal + 1) / 2 - 1] + return medianVal + class Dialog(QDialog, Ui_Dialog): def __init__(self, iface): @@ -55,7 +72,7 @@ class Dialog(QDialog, Ui_Dialog): layers = ftools_utils.getLayerNames([QGis.Point, QGis.Line, QGis.Polygon]) self.inShape.addItems(layers) self.joinShape.addItems(layers) - + def accept(self): self.buttonOk.setEnabled( False ) if self.inShape.currentText() == "": @@ -64,7 +81,7 @@ class Dialog(QDialog, Ui_Dialog): QMessageBox.information(self, self.tr("Spatial Join"), self.tr("Please specify output shapefile") ) elif self.joinShape.currentText() == "": QMessageBox.information(self, self.tr("Spatial Join"), self.tr("Please specify join vector layer") ) - 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()): + 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()): QMessageBox.information(self, self.tr("Spatial Join"), self.tr("Please specify at least one summary statistic") ) else: inName = self.inShape.currentText() @@ -77,6 +94,7 @@ class Dialog(QDialog, Ui_Dialog): if self.chkMean.isChecked(): sumList.append("MEAN") if self.chkMin.isChecked(): sumList.append("MIN") if self.chkMax.isChecked(): sumList.append("MAX") + if self.chkMedian.isChecked(): sumList.append("MED") else: summary = False sumList = ["all"] @@ -120,7 +138,7 @@ class Dialog(QDialog, Ui_Dialog): provider2.select(allAttrs) fieldList2 = ftools_utils.getFieldList(layer2) fieldList = [] - if provider1.crs() <> provider2.crs(): + if provider1.crs() != provider2.crs(): QMessageBox.warning(self, self.tr("CRS warning!"), self.tr("Warning: Input layers have non-matching CRS.\nThis may cause unexpected results.")) if not summary: fieldList2 = ftools_utils.testForUniqueness(fieldList1, fieldList2.values()) @@ -141,7 +159,7 @@ class Dialog(QDialog, Ui_Dialog): fieldList1.extend(fieldList) seq = range(0, len(fieldList1)) fieldList1 = dict(zip(seq, fieldList1)) - + # check for correct field names longNames = ftools_utils.checkFieldNameLength( fieldList1 ) if not longNames.isEmpty(): @@ -149,7 +167,7 @@ class Dialog(QDialog, Ui_Dialog): self.tr( 'No output will be created.\nFollowing field names are longer than 10 characters:\n%1' ) .arg( longNames.join( '\n' ) ) ) return False - + sRs = provider1.crs() progressBar.setValue(13) check = QFile(self.shapefileName) @@ -215,6 +233,7 @@ class Dialog(QDialog, Ui_Dialog): if k == "SUM": atMap.append(QVariant(sum(numFields[j]))) elif k == "MEAN": atMap.append(QVariant(sum(numFields[j]) / count)) elif k == "MIN": atMap.append(QVariant(min(numFields[j]))) + elif k == "MED": atMap.append(QVariant(myself(numFields[j]))) else: atMap.append(QVariant(max(numFields[j]))) numFields[j] = [] atMap.append(QVariant(count)) diff --git a/python/plugins/fTools/tools/frmSpatialJoin.ui b/python/plugins/fTools/tools/frmSpatialJoin.ui index fd730bf7b90..cd80f70f7f6 100644 --- a/python/plugins/fTools/tools/frmSpatialJoin.ui +++ b/python/plugins/fTools/tools/frmSpatialJoin.ui @@ -1,81 +1,82 @@ - + + Dialog - - + + Qt::NonModal - + 0 0 - 377 + 439 466 - + Spatial Join - + true - - - + + + - - + + Target vector layer - + - - + + - - + + Join vector layer - + - - - + + + Attribute Summary - - - - + + + + Take attributes of first located feature - + true - - - + + + Take summary of intersecting features - + - + Qt::Horizontal - + 40 20 @@ -83,55 +84,55 @@ - - - + + + false - + Mean - + true - - - + + + false - + Min - - - + + + false - + Max - - - + + + false - + Sum - + - + Qt::Horizontal - + 40 20 @@ -139,30 +140,40 @@ + + + + false + + + Median + + + - - + + - - + + Output Shapefile - + - - + + true - - + + Browse @@ -171,38 +182,38 @@ - - - + + + Output table - - - - + + + + Only keep matching records - + true - - - - Keep all records (includeing non-matching target records) + + + + Keep all records (including non-matching target records) - + - + Qt::Vertical - + 359 16 @@ -210,25 +221,25 @@ - - - + + + 24 - + Qt::AlignCenter - + true - - - + + + Qt::Horizontal - + QDialogButtonBox::Close|QDialogButtonBox::Ok @@ -243,11 +254,11 @@ Dialog accept() - - 142 - 359 + + 367 + 450 - + 215 290 @@ -259,11 +270,11 @@ Dialog close() - - 68 - 359 + + 293 + 450 - + 132 239 @@ -275,13 +286,13 @@ chkMin setEnabled(bool) - - 154 - 183 + + 180 + 207 - - 173 - 212 + + 179 + 235 @@ -291,13 +302,13 @@ chkMax setEnabled(bool) - + 211 190 - - 219 - 213 + + 240 + 235 @@ -307,11 +318,11 @@ chkSum setEnabled(bool) - + 162 187 - + 283 214 @@ -323,13 +334,29 @@ chkMean setEnabled(bool) - - 69 - 182 + + 95 + 207 - - 93 - 210 + + 123 + 235 + + + + + rdoSummary + toggled(bool) + chkMedian + setEnabled(bool) + + + 85 + 199 + + + 372 + 226