16
16
* *
17
17
***************************************************************************
18
18
"""
19
- from builtins import next
20
- from builtins import str
21
- from builtins import range
22
19
23
20
__author__ = 'Victor Olaya'
24
21
__date__ = 'August 2012'
@@ -207,14 +204,15 @@ def linearMatrix(self, parameters, context, source, inField, target_source, targ
207
204
208
205
def regularMatrix (self , parameters , context , source , inField , target_source , targetField ,
209
206
nPoints , feedback ):
210
-
211
- index = QgsSpatialIndex (target_source .getFeatures (QgsFeatureRequest ().setSubsetOfAttributes ([]).setDestinationCrs (source .sourceCrs ())), feedback )
212
- inIdx = source .fields ().lookupField (inField )
213
-
214
207
distArea = QgsDistanceArea ()
215
208
distArea .setSourceCrs (source .sourceCrs ())
216
209
distArea .setEllipsoid (context .project ().ellipsoid ())
217
210
211
+ inIdx = source .fields ().lookupField (inField )
212
+ targetIdx = target_source .fields ().lookupField (targetField )
213
+
214
+ index = QgsSpatialIndex (target_source .getFeatures (QgsFeatureRequest ().setSubsetOfAttributes ([]).setDestinationCrs (source .sourceCrs ())), feedback )
215
+
218
216
first = True
219
217
sink = None
220
218
dest_id = None
@@ -225,27 +223,28 @@ def regularMatrix(self, parameters, context, source, inField, target_source, tar
225
223
break
226
224
227
225
inGeom = inFeat .geometry ()
228
- inID = str (inFeat .attributes ()[inIdx ])
229
- featList = index .nearestNeighbor (inGeom .asPoint (), nPoints )
230
226
if first :
227
+ featList = index .nearestNeighbor (inGeom .asPoint (), nPoints )
231
228
first = False
232
229
fields = QgsFields ()
233
230
input_id_field = source .fields ()[inIdx ]
234
231
input_id_field .setName ('ID' )
235
232
fields .append (input_id_field )
236
- for i in range (len (featList )):
237
- fields .append (QgsField ('DIST_{0}' .format (i + 1 ), QVariant .Double ))
233
+ for f in target_source .getFeatures (QgsFeatureRequest ().setFilterFids (featList ).setSubsetOfAttributes ([targetIdx ]).setDestinationCrs (source .sourceCrs ())):
234
+ fields .append (QgsField (str (f [targetField ]), QVariant .Double ))
235
+
238
236
(sink , dest_id ) = self .parameterAsSink (parameters , self .OUTPUT , context ,
239
237
fields , source .wkbType (), source .sourceCrs ())
240
238
241
- data = [inID ]
239
+ data = [inFeat [ inField ] ]
242
240
for target in target_source .getFeatures (QgsFeatureRequest ().setSubsetOfAttributes ([]).setFilterFids (featList ).setDestinationCrs (source .sourceCrs ())):
243
241
if feedback .isCanceled ():
244
242
break
245
243
outGeom = target .geometry ()
246
244
dist = distArea .measureLine (inGeom .asPoint (),
247
245
outGeom .asPoint ())
248
- data .append (float (dist ))
246
+ data .append (dist )
247
+
249
248
out_feature = QgsFeature ()
250
249
out_feature .setGeometry (inGeom )
251
250
out_feature .setAttributes (data )
0 commit comments