How to calculate the nearest point on a line to a given point

The problem of the GEOS solution is … it’s not correct because GEOS is a Cartesian library. When working with geographical coordinates you want the point to be along a great circle. In this case the difference is small, but it is no zero. GMT computes it though I find some issues that need to be ironed up. Namely, to compute this one need to save the lines segments in a file first

using GMT

gmtwrite("line.dat", [146.54294815408372 -36.04985956921628; 146.5457684472937 -36.04924988190973; 146.54706548053542 -36.04879871026288; 146.54919201177802 -36.047176909745176; 146.55074543531066 -36.04585993436855])

pt2 = mapproject([146.54376257030356 -36.04616479173944], dist2line="line.dat")

1×5 GMTdataset{Float64, 2}
 Row │   col.1     col.2    col.3    col.4     col.5
─────┼───────────────────────────────────────────────
   1 │ 146.544  -36.0462  376.851  146.545  -36.0495

The the result is in columns 4 & 5 (column 3 is the distance in meters).
Also PrettyTables is not printing enough decimals in this case, so we need to

julia> pt2[4]
146.544839684871

julia> pt2[5]
-36.049450668819865

For reference, the GEOS solution is

julia> on_linestring
POINT (146.54448928065796 -36.04952641048415)

If the line segment was longer, the difference between Cartesian and Geographic would be larger.

6 Likes