I have the following LineString
Line = [(-58.39323087479187, -34.64522852285068), (-58.39493519860744, -34.64593911202303), (-58.39540370387294, -34.64612018963881), (-58.39577715270643, -34.64624297063709), (-58.3961720086682, -34.64636735300267), (-58.39657393244798, -34.64649079664271), (-58.39686900560828, -34.64658786576401), (-58.39768408267521, -34.64688376919202), (-58.39804117638568, -34.64699057960223), (-58.39811749931905, -34.64700849164973), (-58.39828903137601, -34.64704378391358), (-58.39848415993365, -34.64708122410398), (-58.3986547387477, -34.64709449060737), (-58.3988514934597, -34.64709955836587), (-58.39897325079004, -34.6471023653607), (-58.39913887311855, -34.64708744614302), (-58.40078230339798, -34.64694668280777), (-58.4011366548087, -34.64692373544752), (-58.40140137506319, -34.64690660901691), (-58.40169294994597, -34.64689298223946), (-58.40188791399491, -34.64691373562576), (-58.40216794959328, -34.64695369573664), (-58.41441599275006, -34.64898345419851), (-58.41468645364731, -34.64902341675737), (-58.41491789917437, -34.6490522244158), (-58.41504783372419, -34.64906712767555), (-58.41517069354625, -34.64908171429391), (-58.41533660567228, -34.64909137548982), (-58.41610344894117, -34.64910924442286), (-58.41663883920926, -34.64911070715178), (-58.41691994816962, -34.64910623905278), (-58.41712925394172, -34.64908282123896), (-58.41735140092969, -34.64905175937751), (-58.4175040105956, -34.64901464682342), (-58.41778163827102, -34.64893890857992), (-58.41810776345819, -34.64882463807892), (-58.42014859446244, -34.64801074189261), (-58.42109129459963, -34.64768468167974), (-58.42150738131538, -34.64755870865789), (-58.42342555778116, -34.64704856595483), (-58.42380036444368, -34.64696522849173), (-58.42402399498763, -34.64692538456899), (-58.42422994553617, -34.64689682578112), (-58.42442882836995, -34.64688048716456), (-58.42466544132492, -34.64686749516407), (-58.42492895224804, -34.64686969903472), (-58.42519118859626, -34.64689581060387), (-58.42548633816839, -34.64693076251233), (-58.42574834123136, -34.64696690278817), (-58.42600734692162, -34.64703299983701), (-58.42633487929256, -34.64713342399471), (-58.42672363380665, -34.6472903832091), (-58.42701863504796, -34.64744017525714), (-58.42735477118806, -34.64764714102999), (-58.42758954729708, -34.64780944247776), (-58.42780426987807, -34.64798874926387), (-58.42805931284784, -34.64825160891823), (-58.42826384085686, -34.64851328718624), (-58.42836837245316, -34.64865441916345), (-58.42849097552769, -34.64886100909527), (-58.42863862000018, -34.64921981114054), (-58.43039902344795, -34.65411150588781), (-58.43058099211167, -34.65454759092934), (-58.43074440690579, -34.65487960245804), (-58.43091147516467, -34.65519055733962), (-58.43112803380347, -34.65554914847827), (-58.43260609566938, -34.65801519415205), (-58.43278567440816, -34.65827265019357), (-58.43296016110701, -34.65845540315134), (-58.43315011877778, -34.65865061801186), (-58.43335842356506, -34.65885169235345), (-58.43349821535271, -34.65894935913921), (-58.43369231539262, -34.65907745659961), (-58.43391244522258, -34.65921105679707), (-58.4342196854351, -34.65939138210857), (-58.43446159367213, -34.65950262175289), (-58.4354319679709, -34.65989668701516), (-58.44002778106239, -34.66183363815986), (-58.44218397209216, -34.66275027310008), (-58.46163757545956, -34.67090968101208), (-58.46246050330255, -34.67120817614321), (-58.46280547898094, -34.67132107658005), (-58.46307488578911, -34.67139608443851), (-58.46332241010779, -34.67144991252323), (-58.46353300409209, -34.67149541798045), (-58.46859653017688, -34.67249959570204), (-58.46908781436287, -34.67259411624501), (-58.46938034898276, -34.67267684368898), (-58.46981999535013, -34.67282189408634), (-58.47017709131737, -34.67296547046871), (-58.47422872212128, -34.67473003403723), (-58.47458907678709, -34.67490463353656), (-58.47485536868304, -34.67507001487545), (-58.47511721636123, -34.67527809815956), (-58.47561611823835, -34.67573966172805), (-58.47637678148585, -34.67650147966764), (-58.4772898411353, -34.677473083393), (-58.47756878106423, -34.67779366716631), (-58.47927838887487, -34.68008637250686), (-58.4796189824651, -34.68046368453628), (-58.4797836819284, -34.68061589623475), (-58.47997298162954, -34.68077149644261), (-58.48013854950542, -34.68087214089938), (-58.48033951807771, -34.68099482470753), (-58.48077178430249, -34.68117907983302), (-58.4809809942354, -34.68125468946911), (-58.48151812211091, -34.68141121492266), (-58.49074865351113, -34.68364996209967), (-58.49280644305118, -34.68416717487745), (-58.49323181266317, -34.68430677581161), (-58.4939040130434, -34.68450404480966), (-58.494446444632, -34.68469758388901), (-58.50679022633284, -34.68987115611775), (-58.50863040256832, -34.6906581855909), (-58.51021336002367, -34.69130911057548), (-58.51092048468821, -34.69162906732615), (-58.51128148474532, -34.69180116103016), (-58.51164513166684, -34.69197626961216), (-58.51190552938787, -34.6921423269721), (-58.51223224913973, -34.6923465503359), (-58.51266317587315, -34.69263696928233), (-58.51283797439013, -34.69275574911642), (-58.51343928772994, -34.69323891435921), (-58.51613617076313, -34.69565149779803), (-58.51639524277449, -34.6959621229764), (-58.51657880735801, -34.69623621532357), (-58.51668736518741, -34.69648714304233), (-58.51677990482248, -34.69677220564932), (-58.51681912473175, -34.69694815217243), (-58.51683540541565, -34.69728978685934), (-58.51682586767205, -34.69757721116119), (-58.51679226699013, -34.697818097686), (-58.51670205772855, -34.69812198637412), (-58.51657448656194, -34.69837726193614), (-58.51643210417134, -34.69861385564264), (-58.51621815101113, -34.69887195886386), (-58.5160233966457, -34.69905049204797), (-58.51546229204164, -34.69948099921363), (-58.51164101129704, -34.70237114559549)]
and i have this point
Punto = (-58.503015,-34.704488)
If I apply the following code
from shapely.geometry import LineString, Point, LinearRing
p = Point(Punto)
linea=LineString(line)
line1 = list(linea.coords)
print(linea.project(p))
np = linea.interpolate(linea.project(p))
a = p.distance(linea)
print(np)
print(a)
I get that the point that makes up the line closest to the comparison point is (-58.51164101129704, -34.70237114559549) and the distance is 0.008881956060, when what I need is the distance between the point and the line that joins the points.
I add an image for better understanding.
Suppose that the red circles are the points that form the LineString and the black point is the one to which I must measure the distance.
As seen in the image, it is between two points, but I need it to be measured against the line that I paint green, since it is the closest to the black point.
I have painted the line from the data you provide us, and the point in question whose distance to the line you want to know. This is how it looks (the red dot is the one you give with coordinates
(-58.503015,-34.704488)
:As you can see in the figure, the point you have given falls in such a way that the shortest distance to the line almost coincides with the distance to one end of the line. The figure below zooms in on that end, and paints the point you found with your code (the value of the variable
np
) green:The green point falls on the endpoint of the curve, so it is not a good example to determine if you are calculating the distance to a given point, or to the line that interpolates two points.
Let's try instead with the coordinate point
(-58.45,-34.68)
that falls closest to the center of the curve.If, using your same code but making
Punto
the value that I just mentioned equal, we paint the point in red and its projection on the curve in green, we see that what is obtained is what you were looking for, the distance to the line that joins the points (because in factp.distance(linea)
it returns the same result asp.distance(np)
, except floating point rounding error).Look:
Update
This is the code with which I generated the graphics. It is
matplotlib
, executed in a Jupyter Notebook.