Thursday, 10 August 2017

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


enter image description hereI 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.



Answer



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_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.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...