1
1
# -*- coding: utf-8 -*-
2
2
#-----------------------------------------------------------
3
- #
3
+ #
4
4
# Spatial Join
5
5
#
6
6
# Performing an attribute join between vector layers based on spatial
14
14
# WEB : www.geog.uvic.ca/spar/carson
15
15
#
16
16
#-----------------------------------------------------------
17
- #
17
+ #
18
18
# licensed under the terms of GNU GPL 2
19
- #
19
+ #
20
20
# This program is free software; you can redistribute it and/or modify
21
21
# it under the terms of the GNU General Public License as published by
22
22
# the Free Software Foundation; either version 2 of the License, or
23
23
# (at your option) any later version.
24
- #
24
+ #
25
25
# This program is distributed in the hope that it will be useful,
26
26
# but WITHOUT ANY WARRANTY; without even the implied warranty of
27
27
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28
28
# GNU General Public License for more details.
29
- #
29
+ #
30
30
# You should have received a copy of the GNU General Public License along
31
31
# with this program; if not, write to the Free Software Foundation, Inc.,
32
32
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
33
- #
33
+ #
34
34
#---------------------------------------------------------------------
35
35
36
36
from PyQt4 .QtCore import *
39
39
from qgis .core import *
40
40
from ui_frmSpatialJoin import Ui_Dialog
41
41
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
+
42
59
class Dialog (QDialog , Ui_Dialog ):
43
60
44
61
def __init__ (self , iface ):
@@ -55,7 +72,7 @@ def __init__(self, iface):
55
72
layers = ftools_utils .getLayerNames ([QGis .Point , QGis .Line , QGis .Polygon ])
56
73
self .inShape .addItems (layers )
57
74
self .joinShape .addItems (layers )
58
-
75
+
59
76
def accept (self ):
60
77
self .buttonOk .setEnabled ( False )
61
78
if self .inShape .currentText () == "" :
@@ -64,7 +81,7 @@ def accept(self):
64
81
QMessageBox .information (self , self .tr ("Spatial Join" ), self .tr ("Please specify output shapefile" ) )
65
82
elif self .joinShape .currentText () == "" :
66
83
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 () ):
68
85
QMessageBox .information (self , self .tr ("Spatial Join" ), self .tr ("Please specify at least one summary statistic" ) )
69
86
else :
70
87
inName = self .inShape .currentText ()
@@ -77,6 +94,7 @@ def accept(self):
77
94
if self .chkMean .isChecked (): sumList .append ("MEAN" )
78
95
if self .chkMin .isChecked (): sumList .append ("MIN" )
79
96
if self .chkMax .isChecked (): sumList .append ("MAX" )
97
+ if self .chkMedian .isChecked (): sumList .append ("MED" )
80
98
else :
81
99
summary = False
82
100
sumList = ["all" ]
@@ -120,7 +138,7 @@ def compute(self, inName, joinName, outName, summary, sumList, keep, progressBar
120
138
provider2 .select (allAttrs )
121
139
fieldList2 = ftools_utils .getFieldList (layer2 )
122
140
fieldList = []
123
- if provider1 .crs () <> provider2 .crs ():
141
+ if provider1 .crs () != provider2 .crs ():
124
142
QMessageBox .warning (self , self .tr ("CRS warning!" ), self .tr ("Warning: Input layers have non-matching CRS.\n This may cause unexpected results." ))
125
143
if not summary :
126
144
fieldList2 = ftools_utils .testForUniqueness (fieldList1 , fieldList2 .values ())
@@ -141,15 +159,15 @@ def compute(self, inName, joinName, outName, summary, sumList, keep, progressBar
141
159
fieldList1 .extend (fieldList )
142
160
seq = range (0 , len (fieldList1 ))
143
161
fieldList1 = dict (zip (seq , fieldList1 ))
144
-
162
+
145
163
# check for correct field names
146
164
longNames = ftools_utils .checkFieldNameLength ( fieldList1 )
147
165
if not longNames .isEmpty ():
148
166
QMessageBox .warning ( self , self .tr ( 'Incorrect field names' ),
149
167
self .tr ( 'No output will be created.\n Following field names are longer than 10 characters:\n %1' )
150
168
.arg ( longNames .join ( '\n ' ) ) )
151
169
return False
152
-
170
+
153
171
sRs = provider1 .crs ()
154
172
progressBar .setValue (13 )
155
173
check = QFile (self .shapefileName )
@@ -215,6 +233,7 @@ def compute(self, inName, joinName, outName, summary, sumList, keep, progressBar
215
233
if k == "SUM" : atMap .append (QVariant (sum (numFields [j ])))
216
234
elif k == "MEAN" : atMap .append (QVariant (sum (numFields [j ]) / count ))
217
235
elif k == "MIN" : atMap .append (QVariant (min (numFields [j ])))
236
+ elif k == "MED" : atMap .append (QVariant (myself (numFields [j ])))
218
237
else : atMap .append (QVariant (max (numFields [j ])))
219
238
numFields [j ] = []
220
239
atMap .append (QVariant (count ))
0 commit comments