7

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?

underdark
  • 84,148
  • 21
  • 231
  • 413
  • Just to note unlike your question about line lengths this can be done mathematically really without the direct need of a GIS system (especially if the coordinates are projected). The line lengths is more difficult mainly because the longest line is not necessarily from one vertex to another vertex. – Andy W Sep 12 '10 at 16:48
  • @Andy W.: Yes, I figured that a GIS wouldn't be necessary, but it would be nice to know if it's already implemented somewhere :) I'm feeling lazy. – underdark Sep 12 '10 at 19:47
  • 1
    agreed, no need to do more any more work than absolutely necessary – Andy W Sep 12 '10 at 21:50

3 Answers3

11

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.

Dandy
  • 939
  • 7
  • 14
  • 1
    Open source version: http://gist.github.com/604912 – Dandy Sep 30 '10 at 16:58
  • Hey, I just found out I could really benefit from this code in my task. One thing, I have no idea which is the programming language it is written, could you please tell me so that I can use it? Is it java, javascript, c? thanks in advance! – umbe1987 May 30 '14 at 07:07
  • @umbe1987 That code is JavaScript but it should be easy to translate to any language as it is a simple function. – Dandy Jun 10 '14 at 17:54
4

To muddy the water a bit: what projection are your line segments in? The answer depends! If you're it's a Mercator projection, Dandy's answer works. (In a Mercator, lines are rhumb -- by definition, holding the same compass direction for their length.)

In general, however, a line on a map will not correspond to the same compass direction (azimuth) along its entire length -- so your question doesn't always have an answer.

It might be acceptable for you to just assume your lines are rhumb, or to calculate two directions -- one at each endpoint, or to compromise and calculate the direction at the midpoint . . . .

Dan S.
  • 3,549
  • 17
  • 20
1

GRASS GIS (http://grass.osgeo.org/) offers native directed graphs (i.e. vector lines). See "Vector network" screenshots here. Furthermore, there is the m.cogo tool included.

markusN
  • 12,976
  • 31
  • 48
  • So m.cogo adds the orientation (and distance) to attribute table or will it just output the same format it needs for the reverse operation (bearing + distance to coordinates)? – underdark Sep 18 '10 at 08:25