[GIS] How to calculate orientation of line segments using open source GIS

open-source-gisvector

I'd like to calculate the orientation of line segments relative to north direction using open source tools. Which tools or functions would you recommend?

Best Answer

To get an angle from a line you just need to find the angle of the normalized direction. The Atan2 function is available on every computing platform I have used, even calculators. The basic idea is to get a normalized vector for the direction of that line then get the angle.

var normal = line.Direction.GetNormalized();

For your case since you need it to be north (+y hopefully) relative and possibly clockwise you could reverse the inputs to Atan2 like so:

var radians = Atan2(normal.x,normal.y);

And if you need counter-clockwise negate the result of Atan2. For degrees just multiply by 180 then divide by PI. Also note that when the result is negative you can add 2*PI.

if(radians < 0) { radians += 6.28... }

Edited: to correct an error for counter-clockwise.

Note: only works if North is always up.

Related Question