# Is there any way to find the shortest distance from a point to a line?

I´m working with QGIS 1.8.0. I have a map divided into many features (thousands of small rectangles) and I have a point at the center of each of those features. I also have a map of the transmission lines of the country. Is there any method to measure (not one by one) the shortest distance from the points to the transmission lines? Does the maps have to be projected into any specific CRS?

Regards • if your point is at the center of your feature (centroid) then why not take the grid cells edge length divided by 2 ? Or do they have unequal sizes? Aug 21, 2013 at 18:27
• No idea how you do it in QGIS, but hey , you can load your data into postgis and use st_closestpoint(). Aug 21, 2013 at 18:51
• This may help gis.stackexchange.com/questions/52208/… Aug 22, 2013 at 3:06
• Thanks all for the help. Do you know what should I give when asking for the SRID? Aug 22, 2013 at 16:14
• Could you not utilize Distance to nearest Hub or Distance Matrix? I realize that this uses the centroid point of a line but otherwise you could always convert the transmission line to points. Worth a look... Aug 23, 2013 at 8:00

Continuing with simplexio's suggestion, if you don't have a PostGIS server at hand, you can do the job quickly and simply with SpatiaLite: Import both the transmission_lines layer and the center_points layer in a new spatialite DB. Then two SQL commands will give you the shortest distance from each point to the transmission line:

First add a column to the centerpoints for the distance value:

``````ALTER TABLE center_lines ADD COLUMN dist real;
``````

Now calculate the distance:

``````UPDATE center_points SET dist=(
SELECT MIN( ST_Distance(cp.geometry, tl.geometry) )
FROM center_points AS cp, transmission_lines AS tl
WHERE cp.pk_uid = center_points.pk_uid);
``````

Regarding projection: the ST_Distance function returns values in the units of the projection, so if you want meters, then you must have both layers in a meters based CRS. If your layers are in geographic (Long/Lat) coordinates, you will get the distance in degrees (not very usefull...), so reproject into an appropriate CRS first.

• ok, thansk for your answer, I suppose that is also possible on spatialite-gui? I mean the same codes, etc... :) Aug 22, 2013 at 15:26
• Yes, of course. Aug 22, 2013 at 17:51
• Micha, I tried to do it, but it appears that the function does not exist, any tips? I attach a picture of the error. Thanks again! Aug 23, 2013 at 14:45
• I don't know why you got that error. There most certainly is a function ST_Distance(). But I did notice one important oversight in my answer. You need to wrap the Distance function in MIN() to get the minimum distance for each point. Corrected above. Aug 23, 2013 at 21:35