Friday 13 January 2017

geolocation - Calculating the distance between a point and a virtual line of two lat/lngs


Please refer to the example and corresponding image.


I would like to achieve the following: provide two locations (lat/lng), which are shown below as A and B. From this, a virtual line would be drawn and then the distance between this line and C would be calculated (in any measurement).


drawing


I have achieved this currently in Google Maps API v3 but would want to also be able to perform this behind the scenes in my language of choice. Any tips/ideas would be greatly appreciated!



Answer



def get_perp( X1, Y1, X2, Y2, X3, Y3):
"""************************************************************************************************
Purpose - X1,Y1,X2,Y2 = Two points representing the ends of the line segment
X3,Y3 = The offset point

'Returns - X4,Y4 = Returns the Point on the line perpendicular to the offset or None if no such
point exists
'************************************************************************************************ """
XX = X2 - X1
YY = Y2 - Y1
ShortestLength = ((XX * (X3 - X1)) + (YY * (Y3 - Y1))) / ((XX * XX) + (YY * YY))
X4 = X1 + XX * ShortestLength
Y4 = Y1 + YY * ShortestLength
if X4 < X2 and X4 > X1 and Y4 < Y2 and Y4 > Y1:
return X4,Y4

return None

The shortest length is the distance you require, unless I am mistaken?


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