3

I have a point feature and I want to perform a spatial join with the nearest point of another point feature (maybe even within a certain buffer distance). I know QGIS has a nice spatial join tool "Join Attributes by Location" and I figured out what the geometric predicates do from Documentation "Join Attributes by Location" QGIS 2.18.

However, there isn't an option nearest, although the QGIS API Documentation for the QgsGeometry class suggests that there is a class nearestPoint().

Example

Does anyone know how to perform this kind of analysis in QGIS?

3 Answers 3

4

One possibility is to use QGIS virtual layers with the following statement:

WITH closest_candidates AS (
     SELECT a.*, b.*, st_distance(a.geometry, b.geometry) as dist
     FROM a,b
     WHERE st_intersects(a.geometry, st_buffer(b.geometry,100))
)

SELECT *
FROM closest_candidates
WHERE dist < 50

this will perform a spatial join on the layers a and b within a buffer of 100 meters around b and then select the objects where the distance is lower than 50 meters. Be aware that virtual layers will be slow if you have a lot of items in your layer.

Result

4
  • Btw, do you know up to which number of features extend Virtual Layers work well ?
    – Taras
    May 16, 2019 at 13:45
  • I worked with up to 5000 within one query, which worked. but sometimes it is better to save the virtuallayer to a real layer and continue with that layer...
    – eurojam
    May 16, 2019 at 13:55
  • I did as you suggested, but the virtual layer won't show any features on the map. The attribute table lets me know that the join worked (merged the column names), but the fields are empty. Btw I'll have probably a million point features...I need to check how virtual layers work, I have never worked with those before tbh.
    – GeoEki
    May 16, 2019 at 14:23
  • @GeoEki, then your solution is probably PyGIS
    – Taras
    May 16, 2019 at 16:00
2

Qgis 3.8 has an inbuilt algorithm to join to nearest features (you could download a nightly prerelease to use this before the final 3.8 release next month)

1
  • you're right, this is exactly what I need, thank you. I'll give it a try this weekend and report back here if it works.
    – GeoEki
    May 17, 2019 at 9:31
0

If you have not upgraded to QGIS 3.8, you can use the NNJoin Plugin (https://plugins.qgis.org/plugins/NNJoin/ http://arken.nmbu.no/~havatv/gis/qgisplugins/NNJoin/). It is available through the official QGIS plugin repo.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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