I would like to find the coordinates of the nearest point to the linear object T around O
(latitude, longitude) on the latitude and longitude coordinate system.
Currently, only the latitude and longitude coordinates (degrees) of O and the latitude and longitude coordinates (degrees) of both ends of a bar-shaped object shown in blue are known.
Examples of information currently known:
O coordinates (34.9207, 39.4168), blue bar range (34.8923, 139.3775-34.8915, 139.3784)
I was able to display the blue bar by entering the coordinates at the end using the code below, but it didn't go well after that.
bmap.plot ([139.3775, 34.8923], [139.3784, 34.8915], 'r-', linewidth=4)
Currently, my idea is to fine-tune the coordinates of the blue bar from the start point to the end point using functions such as linspace, find the distance from point O to each point, and take the nearest coordinates. Is there any other good method?
Could you tell me if there is a function similar to matlab's polyxpoly function?
I look forward to your kind cooperation.
Sample code for finding the shortest distance coordinates on a plane.
The get_nearest
function calculates and returns the shortest distance coordinates for the line segment t
and the coordinates o
.
You can also easily find it by using the nearest_points function in the Shapely package.( pip install shapely
)
In addition, nearest_points(t,o)
returns two sets of tuples, the first one returns a point on t
, and the second one returns a point on o
, which is a point on o
.
sample code
def get_nearest(t,o):
x1,y1 = t[0]
x2,y2 = t[1]
x3,y3 = o
dx,dy=x2-x1,y2-y1
det=dx*dx+dy*dy
a=(dy*(y3-y1)+dx*(x3-x1))/det
return x1+a*dx, y1+a*dy
t = [(2,0),(0,1)]
o = (0,0)
print(get_nearest(t,o))
from shapely.geometry import point, LineString
from shapely.ops import nearest_points
t = LineString ([(2, 0), (0, 1)])
o = Point(0,0)
nt, no=nearest_points(t,o)
print(nt, no)
# (0.3999999999999999, 0.8)
# POINT (0.3999999999999999990.8) POINT(00)
References
Answer How to find the closeest point on a line segment to an arbitral point?
Find coordinate of the closeest point on polygon in Shapely
© 2024 OneMinuteCode. All rights reserved.