javascript - Projecting a point onto a path -


suppose have ordered array containing points (lat, lon) describing path, , have point (lat, lon) describing current location.

how can project point onto path (and place point in appropriate place in array)?

what tried searching nearest 2 points , assume it's in middle of them. it's guess, fails.

what way of doing this?

i see this:

line segment overview

  • p0,p1 path line segment endpoints
  • p position
  • q' closest point on line in 3d cartessian
  • q q' corrected spherical projection

so:

  1. convert points 3d cartessian coordinates
  2. compute perpendicular distance point , line

    • q'=p0+(dot(p-p0,p1-p0)*(p1-p0)/(|p-p0|*|p1-p0|))
    • perpendicular_distance = |p-q'|
  3. find segment smallest perpendicular_distance

    and use rest of bullets

  4. compute q

    if use sphere instead of ellipsoid know radius if not either compute radius algebraically or use average:

    r=0.5*(|p0-(0,0,0)|+|p1-(0,0,0)|) 

    assuming (0,0,0) earth's center. can more precise if weight position:

    w=|q'-p0|/|p1-p0| r=(1-w)*|p0-(0,0,0)|+w*|p1-(0,0,0)| 

    now correct position of q'

    q=q'*r/|q'| 

    set vector q' q size r if not obvious enough. |p0-(0,0,0)|=|p0| wanted sure how got ...

  5. convert q cartesian spherical coordinates

[notes]

  • |a| size of vector a done like: |a|=sqrt(ax*ax+ay*ay+az*az)
  • dot(a,b) dot product of vectors a,b done like: dot(a,b)=(a.b)=ax*bx+ay*by+az*bz

    if path not complex shaped can use binary search find closest segment. distance comparison not need sqrt ...


Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -