# Computing shortest distance to line segment from set of points using QGIS?

I have a shapefile that describes the urban boundaries of a city and I want to calculate for a set of points the nearest distance to the boundary. Given the number of points is very large (millions) I decided to first test my planned procedure to make sure it is producing the correct output. This is what I did:

1. Created a grid of test points using 'Create grid'
2. Converted the boundary to a set of points using 'Convert lines to points'
3. Identified the shortest path to the boundary using 'Distance to nearest hub (lines to hub)' with meters as the measurement unit

I have encountered a problem in that for a number of test points, particularly those for which the shortest path to the boundary is in a north-south direction, the nearest point on the boundary identified by 'Distance to nearest hub' is incorrect (ie. there are other points on the boundary that are closer).

As an example, for the selected test point, the closest point on the boundary identified by 'Distance to nearest hub' (ie. the northern most point) is 15037.985 meters away, but in fact there is a closer point (the western most point) that is only 14679.774 meters away. I have also checked these distances (and a number of others) against Google Maps and they seem to be fairly accurate (within 10-20 meters).

Not sure what is going on here. I am relatively new to QGIS. I am using QGIS version 3.2.3-Bonn.

## 1 Answer

The data you are using is not projected. In such case, distances are usually computed using the great circle distance (the length of the arc going through two points and the center of the earth), but some tools compute a straight line, Cartesian distance, which is wrong.

You could try projecting your data first, then run the distance to nearest hub tool.

• Thanks for your response. Both the layer and project CRS are set to EPSG:4283. Is this what you mean by 'projecting your data'? Are you able to recommend any other tools or plugins that might produce better results? Nov 8, 2018 at 13:37
• 4283 is a datum, so coordinates are on a round earth. Projecting your data means transforming the coordinates to a flat representation of the earth.
– JGH
Nov 8, 2018 at 13:40
• I would personally put the data in a spatial database, like PostGIS, and use the DB functions (but that's just my way of working, tools are usually perfectly fine)
– JGH
Nov 8, 2018 at 13:41
• Used the v.distance function in GRASS and all is working correctly Nov 8, 2018 at 16:02