1

I am trying to perform a spatial join based on nearest neighbor ("CLOSEST") spatial relationship in QGIS, and by summing the results of the join where multiple matches occur.

In ArcMap there is a way to do this using the spatial join tool, selecting the "CLOSEST" Match Option, and specifying a "SUM" merge rule for the joined attributes. enter image description here I would like to find a QGIS equivalent of this operation. I have already found a plugin which can do a "nearest neighbor join" called NNJoin. I am reasonably satisfied with the results of this plugin. Please note my input shapes are all MultiLineStrings (both target and join feature).

Now I would like to join this resulting table from NNJoin back to my target feature, but it has to sum things for multiple matches. I started fiddling around with the Virtual Layer option in QGIS in order to come up with some SQL code that can do this for me, but I have the impression that the Virtual Layer options are still defective in QGIS (see Virtual layer functionality does not seem to support memory layers).

Is there any reasonably straighforward way to achieve this using QGIS?

2
  • doing things in DB-manager is far more easy then using add virtual layer. I wrote an answer for your previous question: gis.stackexchange.com/a/319329/7849 – PieterB Apr 19 '19 at 11:02
  • I'm guessing we just have to join by location without summing the duplicates and then use some kind of aggregate function in the field calculator to get sums. – Sam Friedman Apr 8 '20 at 19:09
1

Leaving aside the use of SQL or Python, I don't believe there is a one-step method in the Processing Toolbox options right now to do a k-nearest neighbour join and aggregate joined feature attributes.

However, it shouldn't be so hard using two steps. I posted an answer to a similar question at Join/Summary Nearest Neighbors QGIS which involves only Processing Toolbox options. (One advantage of using the Join Attributes by Nearest tool over NNJoin is that it's in-built, and you can specify how many nearest points you want to look for and set a distance limit.)

Initially I used Statistics by Category in that answer but I have now found that the Aggregate processing tool is more flexible if you wanted to aggregate values for multiple fields all at once.

That answer involved joining data back to the source layer, grouping by an unique ID from the source layer - but you could just as group by an ID field from the target layer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.