As shown in the figure, I have a 3D shapely Linestring (line
) and a Point (point
).
I am trying to calculate the distance between them using the command line.distance(point)
. But I am getting 0.0
as the output. It is not supposed to be zero, as shown in the figure.
line='LINESTRING Z (7760.332870937675 -2204.1529076701922 13310.4921875, 5275.867565893014 -2204.1529076701922 13421.5302734375)'
point='POINT Z (6677.34068980373 -2204.1529076701922 12820.9072265625)'
How to solve this? What I am doing wrong here? Help please.
Shapely is 2d geometry library. The third dimension is 'bolted on', sometimes it's called '2.5' dimensional. Basically you can assume methods only work in the 2d sense.
In your case, your linestring consist of only 2 points.Therefore I will give you a solution that works for just linestring of length 2.
import numpy as np
from numpy.linalg import norm
from shapely import LineString, Point
p1 = 7760.332870937675, -2204.1529076701922, 13310.4921875
p2 = 5275.867565893014, -2204.1529076701922, 13421.5302734375
point = 6677.34068980373, -2204.1529076701922, 12820.9072265625
# line = LineString([p1, p2])
p1 = np.array(p1) # or np.array(line.coords[0])
p2 = np.array(p2) # or np.array(line.coords[1])
point = np.array(point)
d = norm(np.cross(p2-p1, p1-point) / norm(p2-p1))
print(d)
Which prints:
537.4505771322102