2

I need to select the closest (and only the closest) lines to a point in QGIS. The lines may belong to different layers. The idea is to be able to combine the selected lines to form the smallest polygon that contains the point used to select the lines. This is an example of my situation: enter image description here

The yellow point is the one i should use to make the selection; the lines can be blue, red, yellow, green or black, and they may belong to different layers. I tried the NNJoin plugin, but it doesn't work for me. Also the Nearest Neighbor tool doesn't work (it returns a List Index Out of Range error).

Does anybody have a suggestion?

2
  • If I'm understanding correctly, you ultimately need the most compact bounding polygon, made up of sides from diverse line layers, right? If so, wouldn't it make sense to dissolve all the lines into polygons, test for which polygon your point falls into, and then find by spatial query all the original (road?) lines that intersect that polygon? Commented Nov 21, 2017 at 6:53
  • Hi Paulo, unfortunately the screenshot above represents just a small portion of my data. I have many points and lines to process, and I can't manually dissolve all the lines into polygons.
    – Luca
    Commented Nov 21, 2017 at 18:57

1 Answer 1

1

In QGIS there is the DB Manager where you can execute SQL code. I am assuming you are using shapefiles, which can have SQL applied to them in the DB Manager and can be found in the 'Virtual Layers' data list.

So in the SQL Window, use the following code below, except replace 'linelayer' and 'pointlayer' with the datasets that contain your line and point data. Then the point_id is the field name for what ever uniquely identifies the point you want to run the query on.

 SELECT  linelayer.*
 FROM linelayer,
      (SELECT geometry FROM pointlayer WHERE point_id = <POINTID>) as point
 ORDER BY ST_Distance(point.geometry,linelayer.geometry)
 LIMIT 1

Hit execute and you should get one feature returned that is the closest to your point. You can then create a temporary layer by selecting the 'Load as new layer' check box right at the bottom of the window.

2
  • Hi Freighty, I like your approach, but perhaps I wasn't clear in my explanation. I need to select ALL the lines that are the closest to my point (actually many points in different areas; this is just a test) to create a closed polygon. The presentation at gis.stackexchange.com/questions/81613/… is probably the closest solution I've found for my issue, with the difference that in my case the lines completely enclose the point.
    – Luca
    Commented Nov 21, 2017 at 19:03
  • This code would work for me, but it doesn't work. It returns an error: "Query execution error on CREATE TEMP VIEW _tview AS SELECT linelayer.* FROM linelayer, (SELECT geometry FROM pointlayer WHERE objectid = <POINTID>) as point ORDER BY ST_Distance(point.geometry,linelayer.geometry) LIMIT 1: 1 - near "<": syntax error"
    – Eric Lino
    Commented Feb 1, 2023 at 23:33

Your Answer

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

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