I am currently using the v.distance GRASS tool to calculate distances, but it naturally gives the nearest (shortest) distances. Oddly enough, I would like to find the furthest distance from a pipeline line segment to a road line segment. Both shapefiles are Line (MultiLineString).

Is there any way for v.distance to be augmented to do such a calculation?

image of furthest distance from pipeline to road

  • 2
    Use PostGIS postgis.net/docs/ST_MaxDistance.html or SpatiaLite gaia-gis.it/gaia-sins/spatialite-sql-latest.html (MaxDistance). But in your example the max distance seems to start from the other end of the road. If you are interested in the distance that you draw you must first take the end (or start) point of the road.
    – user30184
    Sep 2, 2020 at 19:50
  • @user30184 l thought the same thing when I saw the picture- that's not the maximum distance. What the OP really wants is the point on the pipe with the largest minimum distance to the road
    – M Bain
    Sep 3, 2020 at 0:26
  • I am very new to SQL expressions, especially the spatial ones, so I have many questions. The examples I have found are very confusing and I need some help with them. Also, the pipeline shapefile(s) have over 33,000 line segments and I read that certain statements are quicker than others when dealing with a large number of features. Any direction for help would be appreciated. What is my first step? I'm overwhelmed :) Both files are projected and the measurements need to be accurate. I think I would like to use a SpatiaLite database. Thanks
    – MJM
    Sep 3, 2020 at 0:44
  • Sorry for a terrible picture to explain what I am really looking for. I am looking to asses the risk of the pipeline based on distance from the road, so I would like to determine a conservative distance value (furthest distance from a location on each pipeline segment to the road). This distance will be the worst case scenario if you have to access that section of pipeline from the road.
    – MJM
    Sep 3, 2020 at 0:51
  • Don't be sorry Matthew, makes sense to me, I kind of guessed it was some sort of farthest, most difficult to access, assessment.
    – M Bain
    Sep 3, 2020 at 3:20

2 Answers 2


An imperfect approach would be to run the Extract Vertices processing algorithm on the pipes layer and then join the resulting Vertices layer to the Roads layer using the Join attributes by nearest algorithm. Then from the Joined Layer you can use Statistics by categories to get the maximum distance for each pipe id.

If you want the location of the furthest point as well as the distance its a bit trickier. I'm thinking you could use the Order by expression algorithm on the Joined Layer, sorting by distance in descending order, to create a new Ordered table, then run Delete duplicates by attribute using the pipe id as the match field, which should leave a single point for each pipe - the first one with the longest distance.

This approach is imperfect because the actual furthest point on a pipe may not coincide with a vertex. Like when you have straight pipe and a bend in the road: enter image description here You could add additional intermediate points at regular intervals using Points along geometry, but that's starting to feel pretty crude...

Maybe this approach will be good enough. You know what your data looks like and the requirements of your risk assessment exercise.

FYI, sometimes when running multiple processing algorithms sequentially, I have started to get weird results, when that happens it often helps to save the temporary table as a real table and continuing processing on the saved table.

  • Thank you for this approach. I've used it and it works. However, when I run the join is states the following from my graphical modelling log: 1. Multiple matching features found at same distance from search feature, found 3 features instead of 1 x 60-70 time; 2. Feature could not be written to C:/Users/Matthew/Box/Modelling Project/Model Data/GIS/Project/Asset Distances Model/Outputs/pipe_road.csv x 11. There are many pipeline segments (33, 000+) and many roads. The output file of the results looks okay, but there are just too many entries to verify. Any suggestions?
    – MJM
    Sep 5, 2020 at 19:55
  • Hmmm. An easy one to try first would be to run Remove duplicate vertices on the pipes layer before extracting the vertices - I don't know if there will be any. Another thought is you said the pipes are MultiLineString, if a single pipe asset branches you will get two vertices at the same location - one for each part. To remove these "duplicates" you would need to Dissolve the vertices using pipe id as the dissolve field.
    – M Bain
    Sep 5, 2020 at 22:46

I am editing my question after messing around with some SQL expressions.

pipeline = pipeline shapefile - Geometry Line (MultiLineString)imported into SpatiaLite database pipeline_road_dist.sqlite. Projected to EPSG: 3400

road_seg_clip = road shapefile - Geometry Line (MultiLineString) imported into SpatiaLite database (pipeline_road_dist.sqlite). Projected to EPSG: 3400


SELECT lic_li_no, roadsegid, MaxDistance(pipeline.geom, road_seg_clip.geom) AS dist FROM pipeline, road_seg_clip

However this gives some pretty large distances (i.e. distance from a pipeline segment to a road of 4.5 km, but there is one 25 meters away).

I also tried the following expression:

SELECT lic_li_no, roadsegid, Distance(pipeline.geom, road_seg_clip.geom) AS dist FROM pipeline, road_seg_clip ORDER BY dist

I also get the same wrong distances, which is likely because I have forgotten to do something regarding the geometry/datum instructions. As you can see 41003 m is not right.

After all this I cannot load the results into my canvas because there is no geometry column recognized. I am missing a step.

Updated image below.

[![DB Manager][1]]2

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.