turf icon indicating copy to clipboard operation
turf copied to clipboard

Include current bearing at point returned from @turf/along

Open stevage opened this issue 5 years ago • 3 comments

I find that often when I call turf.along I also need the angle of the given point along the line (for instance, if animating an icon along a line, you want it to rotate to face the direction of travel).

There's no convenient way to compute that angle: you can use turf.bearing, but you have to compute a point slightly forward and slightly behind the point (two more calls to turf.along), and handle the cases where you're very close to the end of the line.

Either of these would be great:

  • turf.along could provide that bearing as a property on the feature (maybe an optional flag, so the computation doesn't happen if it's not needed)
  • a convenient turf.bearingAt method, taking a point and a line and returning the angle
  • or maybe adding an extra property to the return value of turf.nearestPointOnLine, to include the angle of the line at that nearest point.

Thoughts?

stevage avatar Dec 15 '20 23:12 stevage

Hi @stevage. Are you still interested in this feature? About to do some work in along, so now might be a good time. Would like some more info to help understand the use case first though.

Would a single call to bearing with the point returned from along to the end of the original line give you the bearing you need? And if you've reached the end, a call to bearing with the original line and the final param set to true?

Thanks!

smallsaucepan avatar Dec 20 '23 23:12 smallsaucepan

I don't currently need this feature.

I don't think the workaround you mention would work. The use case was moving an arrow icon along the line. At each point, you want to rotate the icon so it faces along the current line segment. So computing an angle between that point and the end of the whole LineString would not be correct.

stevage avatar Dec 21 '23 01:12 stevage

Ok, gotcha. I wasn't considering a line string with multiple segments, potentially running different directions. Should be possible. Sounds like too late for your needs, though hopefully useful to someone down the track.

Thanks for taking the time to help me understand.

smallsaucepan avatar Dec 21 '23 01:12 smallsaucepan