@@ -111,6 +111,7 @@ def processAlgorithm(self, parameters, context, feedback):
111
111
source_field = self .parameterAsString (parameters , self .INPUT_FIELD , context )
112
112
target_source = self .parameterAsSource (parameters , self .TARGET , context )
113
113
target_field = self .parameterAsString (parameters , self .TARGET_FIELD , context )
114
+ same_source_and_target = parameters [self .INPUT ] == parameters [self .TARGET ]
114
115
matType = self .parameterAsEnum (parameters , self .MATRIX_TYPE , context )
115
116
nPoints = self .parameterAsInt (parameters , self .NEAREST_POINTS , context )
116
117
@@ -119,19 +120,25 @@ def processAlgorithm(self, parameters, context, feedback):
119
120
120
121
if matType == 0 :
121
122
# Linear distance matrix
122
- return self .linearMatrix (parameters , context , source , source_field , target_source , target_field ,
123
+ return self .linearMatrix (parameters , context , source , source_field , target_source , target_field , same_source_and_target ,
123
124
matType , nPoints , feedback )
124
125
elif matType == 1 :
125
126
# Standard distance matrix
126
127
return self .regularMatrix (parameters , context , source , source_field , target_source , target_field ,
127
128
nPoints , feedback )
128
129
elif matType == 2 :
129
130
# Summary distance matrix
130
- return self .linearMatrix (parameters , context , source , source_field , target_source , target_field ,
131
+ return self .linearMatrix (parameters , context , source , source_field , target_source , target_field , same_source_and_target ,
131
132
matType , nPoints , feedback )
132
133
133
- def linearMatrix (self , parameters , context , source , inField , target_source , targetField ,
134
+ def linearMatrix (self , parameters , context , source , inField , target_source , targetField , same_source_and_target ,
134
135
matType , nPoints , feedback ):
136
+
137
+ if same_source_and_target :
138
+ # need to fetch an extra point from the index, since the closest match will always be the same
139
+ # as the input feature
140
+ nPoints += 1
141
+
135
142
inIdx = source .fields ().lookupField (inField )
136
143
outIdx = target_source .fields ().lookupField (targetField )
137
144
@@ -176,6 +183,9 @@ def linearMatrix(self, parameters, context, source, inField, target_source, targ
176
183
if feedback .isCanceled ():
177
184
break
178
185
186
+ if same_source_and_target and inFeat .id () == outFeat .id ():
187
+ continue
188
+
179
189
outID = outFeat .attributes ()[outIdx ]
180
190
outGeom = outFeat .geometry ()
181
191
dist = distArea .measureLine (inGeom .asPoint (),
@@ -207,6 +217,7 @@ def linearMatrix(self, parameters, context, source, inField, target_source, targ
207
217
208
218
def regularMatrix (self , parameters , context , source , inField , target_source , targetField ,
209
219
nPoints , feedback ):
220
+
210
221
distArea = QgsDistanceArea ()
211
222
distArea .setSourceCrs (source .sourceCrs (), context .transformContext ())
212
223
distArea .setEllipsoid (context .project ().ellipsoid ())
0 commit comments