3

I have a polygon shapefile and a line shapefile with several thousand features each. I want to get:

The nearest distance between a polygon and a line that have the same attribute (e.g. ID). Example here (I want to get the green line as an output).

Desired output: Nearest distance between line and polygon with the same id (green line)

I have played around with various combinations of all distance tools in QGIS, like Distance to nearest hub, Join attributes by nearest, v.distance.

v.distance SHOULD be able to do what I want with 'upload' = 'to_attr', but I am getting no output.

  • Do you want an actual line geometry, or do you only need the distance value? – csk Aug 19 at 16:23
  • Just the distance value would be enough, though the actual line geometry would have the bonus of making it easier to check visually whether the results are correct. The answer from @snaileater generates the distance, which is sufficient for me. – wowbag Aug 20 at 8:46
1

I don't know about the tools you mention but you can try a query like this one :

select id, st_distance(poly.geometry, line.geometry) from poly, line
where poly.id=line.id

That should do the job ...

You input the query in QGIS DB Manager : You choose Database / Database Manager / Database Manager then Virtual Layers / Qgis Layers.

Adapt the query to your data sructure if needed ...

  • Beautiful, it works! I had to remove 'id' from the query though, it gave me an error message about being ambiguous. So: select st_distance(poly.geometry, line.geometry) from poly, line where poly.id=line.id – wowbag Aug 20 at 8:37
  • yep you have to mention either table ... so poly.id or line.id ... in that case they will be equal anyway ... – snaileater Aug 20 at 8:42

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.