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?

3
  • 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, 2010 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, 2010 at 19:47
  • 1
    agreed, no need to do more any more work than absolutely necessary
    – Andy W
    Sep 12, 2010 at 21:50

3 Answers 3

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.

3
  • 1
    Open source version: gist.github.com/604912
    – Dandy
    Sep 30, 2010 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, 2014 at 7: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, 2014 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 . . . .

2
  • Thank you for your comment, you're right. Luckily the lines are in a Mercator projection.
    – underdark
    Sep 17, 2010 at 10:15
  • Should have clarified that a bit better. That would definitely take the easy out of it.
    – Dandy
    Sep 17, 2010 at 19:08
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.

1
  • 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, 2010 at 8:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.