# Find closest point from origin in QGIS and display measure line in composer

I have a location point. there are several constraints areas surrounding the point. Is there a way to calculate the closest point of this constraint from my point and to display the distances as a measurement in print composer.

Further to the comments below see the above image.

The red point is the subject site. The hatched areas are natural heritage constraints areas such as Natura 2000 site and local designated sites. For the purposes of reports it is necessary to state (for example) that site X is 7Km from the nearest point of the SPA, 9Km from the nearest point of an AONB, 15Km from the nearest point....etc. It would be ideal if this could be plotted rather than using a layer and snapping if it was possible to use an "autoCAD type function" where by measurements are displayed as an attribute to the line rather than a lable plotted in the print composer.

• Perhaps someone would need to create a plugin which in layer A is input that it produces measurement lines (lines with lenghts in the attribute table which can be displayed as lables) to the nearest node on layer b's polygon. Also consider the interestion of the line with the polygon if the intersection is closer to the point than the node. – Ger Apr 30 '13 at 14:10
• What do you mean by 'constraint area'? Could you share a sketch of the output you're looking for? – Simbamangu May 2 '13 at 18:40
• Thanks for the sketch. Do you mean that you want to have a line automatically drawn between point and area, or is it enough to just calculate the nearest distances to each polygon and display that data on the composer? – Simbamangu May 3 '13 at 13:53

I would write a script to run across a list of X,Ys on your constraint and compare distances. This may be slow if you have a large number of constraint points. I am not super experienced with scripting in QGIS, but my initial thought would be something like this (python):

``````import math
sub=[1003017.999854, 211253.000116] #X and Y coordinates of the subject area
con = [[1003275.999994,211219.999854], [1003292.99996, 211327.999966], [1003533.000136, 211179.999934]] # list of X and Y coordinates of the constraint areas
shortist = 0

def distance (subX, subY, conX,conY):
return math.sqrt((conX-subX)**2+ (conY-subY)**2)

for i in con:
if distance(sub[0], sub[1],i[0],i[1])<shortist or shortist ==0:
shortist_point= i[0],i[1], distance(sub[0], sub[1],i[0],i[1])
shortist = distance(sub[0], sub[1],i[0],i[1])

print shortist_point
``````

this will give you the X,Y and distance of the closest point. You would have to change the sub and con variables to be read into your script to have it work for you, but it should give you the answer. If you need the distance to all X,Ys you could change it to be a dictionary of the X,Ys to distance.

if you need the line simply have GIS draw the line from your subject coordinates to the shortest X,Ys. hope this helps.

• Not entirely what i have been looking for but close. it is just not creating the results i need and to run this script evertime is tooo time consuming. thanks and much appriciated for the input. – Ger May 8 '13 at 12:54
• you should be able to combine with this to automate the line creation gis.stackexchange.com/questions/898/convert-xy-points-to-a-line – Hotpepper May 8 '13 at 13:29
• You may want to consider adding an iterative select by location query to the script in the beginning gradually expanding out from the subject area until it selects at least one point of the constraint area to limit the number of points you need to check. – Hotpepper May 10 '13 at 1:08