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
This commit is contained in:
cfarmer 2010-12-21 01:52:42 +00:00
parent a58b6a889b
commit 5e0752fc6e
2 changed files with 168 additions and 122 deletions

View File

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

View File

@ -1,81 +1,82 @@
<ui version="4.0" >
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Dialog</class>
<widget class="QDialog" name="Dialog" >
<property name="windowModality" >
<widget class="QDialog" name="Dialog">
<property name="windowModality">
<enum>Qt::NonModal</enum>
</property>
<property name="geometry" >
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>377</width>
<width>439</width>
<height>466</height>
</rect>
</property>
<property name="windowTitle" >
<property name="windowTitle">
<string>Spatial Join</string>
</property>
<property name="sizeGripEnabled" >
<property name="sizeGripEnabled">
<bool>true</bool>
</property>
<layout class="QGridLayout" name="gridLayout" >
<item row="0" column="0" colspan="3" >
<layout class="QVBoxLayout" >
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0" colspan="3">
<layout class="QVBoxLayout">
<item>
<widget class="QLabel" name="label_3" >
<property name="text" >
<widget class="QLabel" name="label_3">
<property name="text">
<string>Target vector layer</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="inShape" />
<widget class="QComboBox" name="inShape"/>
</item>
</layout>
</item>
<item row="1" column="0" colspan="3" >
<layout class="QVBoxLayout" >
<item row="1" column="0" colspan="3">
<layout class="QVBoxLayout">
<item>
<widget class="QLabel" name="label" >
<property name="text" >
<widget class="QLabel" name="label">
<property name="text">
<string>Join vector layer</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="joinShape" />
<widget class="QComboBox" name="joinShape"/>
</item>
</layout>
</item>
<item row="2" column="0" colspan="3" >
<widget class="QGroupBox" name="groupBox_2" >
<property name="title" >
<item row="2" column="0" colspan="3">
<widget class="QGroupBox" name="groupBox_2">
<property name="title">
<string>Attribute Summary</string>
</property>
<layout class="QGridLayout" >
<item row="0" column="0" colspan="6" >
<widget class="QRadioButton" name="rdoFirst" >
<property name="text" >
<layout class="QGridLayout">
<item row="0" column="0" colspan="7">
<widget class="QRadioButton" name="rdoFirst">
<property name="text">
<string>Take attributes of first located feature</string>
</property>
<property name="checked" >
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item row="1" column="0" colspan="6" >
<widget class="QRadioButton" name="rdoSummary" >
<property name="text" >
<item row="1" column="0" colspan="7">
<widget class="QRadioButton" name="rdoSummary">
<property name="text">
<string>Take summary of intersecting features</string>
</property>
</widget>
</item>
<item row="2" column="0" >
<item row="2" column="0">
<spacer>
<property name="orientation" >
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0" >
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
@ -83,55 +84,55 @@
</property>
</spacer>
</item>
<item row="2" column="1" >
<widget class="QCheckBox" name="chkMean" >
<property name="enabled" >
<item row="2" column="1">
<widget class="QCheckBox" name="chkMean">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text" >
<property name="text">
<string>Mean</string>
</property>
<property name="checked" >
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item row="2" column="2" >
<widget class="QCheckBox" name="chkMin" >
<property name="enabled" >
<item row="2" column="2">
<widget class="QCheckBox" name="chkMin">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text" >
<property name="text">
<string>Min</string>
</property>
</widget>
</item>
<item row="2" column="3" >
<widget class="QCheckBox" name="chkMax" >
<property name="enabled" >
<item row="2" column="3">
<widget class="QCheckBox" name="chkMax">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text" >
<property name="text">
<string>Max</string>
</property>
</widget>
</item>
<item row="2" column="4" >
<widget class="QCheckBox" name="chkSum" >
<property name="enabled" >
<item row="2" column="4">
<widget class="QCheckBox" name="chkSum">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text" >
<property name="text">
<string>Sum</string>
</property>
</widget>
</item>
<item row="2" column="5" >
<item row="2" column="6">
<spacer>
<property name="orientation" >
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0" >
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
@ -139,30 +140,40 @@
</property>
</spacer>
</item>
<item row="2" column="5">
<widget class="QCheckBox" name="chkMedian">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>Median</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item row="3" column="0" colspan="3" >
<layout class="QVBoxLayout" >
<item row="3" column="0" colspan="3">
<layout class="QVBoxLayout">
<item>
<widget class="QLabel" name="label_2" >
<property name="text" >
<widget class="QLabel" name="label_2">
<property name="text">
<string>Output Shapefile</string>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" >
<layout class="QHBoxLayout">
<item>
<widget class="QLineEdit" name="outShape" >
<property name="readOnly" >
<widget class="QLineEdit" name="outShape">
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="toolOut" >
<property name="text" >
<widget class="QToolButton" name="toolOut">
<property name="text">
<string>Browse</string>
</property>
</widget>
@ -171,38 +182,38 @@
</item>
</layout>
</item>
<item row="4" column="0" colspan="3" >
<widget class="QGroupBox" name="groupBox_3" >
<property name="title" >
<item row="4" column="0" colspan="3">
<widget class="QGroupBox" name="groupBox_3">
<property name="title">
<string>Output table</string>
</property>
<layout class="QGridLayout" >
<item row="0" column="0" >
<widget class="QRadioButton" name="rdoMatch" >
<property name="text" >
<layout class="QGridLayout">
<item row="0" column="0">
<widget class="QRadioButton" name="rdoMatch">
<property name="text">
<string>Only keep matching records</string>
</property>
<property name="checked" >
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item row="1" column="0" >
<widget class="QRadioButton" name="rdoKeep" >
<property name="text" >
<string>Keep all records (includeing non-matching target records)</string>
<item row="1" column="0">
<widget class="QRadioButton" name="rdoKeep">
<property name="text">
<string>Keep all records (including non-matching target records)</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item rowspan="3" row="5" column="0" colspan="3" >
<item row="5" column="0" rowspan="3" colspan="3">
<spacer>
<property name="orientation" >
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0" >
<property name="sizeHint" stdset="0">
<size>
<width>359</width>
<height>16</height>
@ -210,25 +221,25 @@
</property>
</spacer>
</item>
<item row="7" column="1" >
<widget class="QProgressBar" name="progressBar" >
<property name="value" >
<item row="7" column="1">
<widget class="QProgressBar" name="progressBar">
<property name="value">
<number>24</number>
</property>
<property name="alignment" >
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
<property name="textVisible" >
<property name="textVisible">
<bool>true</bool>
</property>
</widget>
</item>
<item rowspan="2" row="6" column="2" >
<widget class="QDialogButtonBox" name="buttonBox_2" >
<property name="orientation" >
<item row="6" column="2" rowspan="2">
<widget class="QDialogButtonBox" name="buttonBox_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons" >
<property name="standardButtons">
<set>QDialogButtonBox::Close|QDialogButtonBox::Ok</set>
</property>
</widget>
@ -243,11 +254,11 @@
<receiver>Dialog</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel" >
<x>142</x>
<y>359</y>
<hint type="sourcelabel">
<x>367</x>
<y>450</y>
</hint>
<hint type="destinationlabel" >
<hint type="destinationlabel">
<x>215</x>
<y>290</y>
</hint>
@ -259,11 +270,11 @@
<receiver>Dialog</receiver>
<slot>close()</slot>
<hints>
<hint type="sourcelabel" >
<x>68</x>
<y>359</y>
<hint type="sourcelabel">
<x>293</x>
<y>450</y>
</hint>
<hint type="destinationlabel" >
<hint type="destinationlabel">
<x>132</x>
<y>239</y>
</hint>
@ -275,13 +286,13 @@
<receiver>chkMin</receiver>
<slot>setEnabled(bool)</slot>
<hints>
<hint type="sourcelabel" >
<x>154</x>
<y>183</y>
<hint type="sourcelabel">
<x>180</x>
<y>207</y>
</hint>
<hint type="destinationlabel" >
<x>173</x>
<y>212</y>
<hint type="destinationlabel">
<x>179</x>
<y>235</y>
</hint>
</hints>
</connection>
@ -291,13 +302,13 @@
<receiver>chkMax</receiver>
<slot>setEnabled(bool)</slot>
<hints>
<hint type="sourcelabel" >
<hint type="sourcelabel">
<x>211</x>
<y>190</y>
</hint>
<hint type="destinationlabel" >
<x>219</x>
<y>213</y>
<hint type="destinationlabel">
<x>240</x>
<y>235</y>
</hint>
</hints>
</connection>
@ -307,11 +318,11 @@
<receiver>chkSum</receiver>
<slot>setEnabled(bool)</slot>
<hints>
<hint type="sourcelabel" >
<hint type="sourcelabel">
<x>162</x>
<y>187</y>
</hint>
<hint type="destinationlabel" >
<hint type="destinationlabel">
<x>283</x>
<y>214</y>
</hint>
@ -323,13 +334,29 @@
<receiver>chkMean</receiver>
<slot>setEnabled(bool)</slot>
<hints>
<hint type="sourcelabel" >
<x>69</x>
<y>182</y>
<hint type="sourcelabel">
<x>95</x>
<y>207</y>
</hint>
<hint type="destinationlabel" >
<x>93</x>
<y>210</y>
<hint type="destinationlabel">
<x>123</x>
<y>235</y>
</hint>
</hints>
</connection>
<connection>
<sender>rdoSummary</sender>
<signal>toggled(bool)</signal>
<receiver>chkMedian</receiver>
<slot>setEnabled(bool)</slot>
<hints>
<hint type="sourcelabel">
<x>85</x>
<y>199</y>
</hint>
<hint type="destinationlabel">
<x>372</x>
<y>226</y>
</hint>
</hints>
</connection>