P is for Practical PerlMonks

### Perl and Solving Trig/Converting GPS to x,y Cords

by Pizentios (Scribe)
 on Jan 08, 2013 at 19:36 UTC Need Help??
Pizentios has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks!

I have a problem that i know can be solved by using trig. However i am at a loss on how to go about it in perl.

Bit of backstory:
At work we have a map that uses x,y cords to place objects on it. However we need to "convert" gps lat and long cords to fit on this map. I have two reference points on the map already that will be constant for every caculation. Using Math::Trig i have figured out the angles (read bearings) of the two reference points as well as the bearings from the reference points to my test point using the gps lat and long values.

Here's the code i've used:

```use strict;
use warnings;

+e_direction);

my \$low_right_lat ="49.033312";
my \$low_right_long ="-95.261125";
my \$upper_left_lat ="51.361154";
my \$upper_left_long ="-101.353263";

my \$user_lat ="49.752852";
my \$user_long ="-100.119849";

my @A = NESW(\$low_right_long, \$low_right_lat);
my @B = NESW(\$upper_left_long, \$upper_left_lat);
my @USER = NESW(\$user_long, \$user_lat);

my \$angle_a_to_user = great_circle_direction(@A, @USER); #in rads.
my \$angle_b_to_user = great_circle_direction(@B, @USER); #in rads.
my \$angle_a_to_b = great_circle_direction(@A, @B); #in rads.
my \$angle_b_to_a = great_circle_direction(@B, @A); #in rads.

print "Angle From A to User is: \$angle_a_to_user_deg\n";
print "Angle From B to User is: \$angle_b_to_user_deg\n";
print "Angle From A to B is: \$angle_a_to_b_deg\n";
print "Angle From B to A is: \$angle_b_to_a_deg\n";

This ofcourse returns angles to the lines from 0 degrees (strait north)

Using this information i am able to use the law of sines and the law of cosines (and some addition/subtraction to get the angle from line AB to line AC and line BC) to figure out the length of the sides that are made to my test point on the x,y graph (on paper but it should translate to perl code fairly easy).

I am however at a loss on how to proceed further....

I have constants on a x,y grid that are as follows:
Point A: x=1019,y=1698
Point B: x=79,y=1138
Distance between Point A and Point B is: 1094

I am trying to caculate the x,y cords for a point that i would like to map. My thoughts are to find the intersection of two circles, as i know the radius of each circle (from my math on paper, one centered on Point A and the other centered on Point B).

Circle at Point A's radius: 781.5815
Circle at Point B's radius: 431.2683009

Can somebody please give me some tips or suggestions on how to proceed to solve this problem in perl? Code examples would be greatly appreciated....This is the first time i've had to do any kind of higher level math in perl.

It doesn't have to be solved using intersection of two circles if sombody out there has a better method of figuring it out...and i must admit my trig skills are very rusty as i haven't used em much since high school.

-Pizentios

Replies are listed 'Best First'.
Re: Perl and Solving Trig/Converting GPS to x,y Cords
by davido (Archbishop) on Jan 09, 2013 at 07:50 UTC

This problem is more complex than simple trigonometry. The earth is approximately round, but not precisely round, and that's one of the lesser (but more memorable for me) complexities involved. GPS systems have to choose a projection: WGS84, for example. And navigators of old spent a good deal of time learning the math behind great circles, and various chart projections. ...ok, maybe the rest is trigonometry, but not entirely simple.

It's been eighteen years since I read it, and I've forgotten most of what I learned. But I highly recommend Dutton's Navigation and Piloting (or the more recent Dutton's Nautical Navigation). Pick up a used copy via Amazon or eBay. You're really just after the mathematical formulas and a good explanation of the theory, and for that an older version is probably just as good as a newer one. I think I have the 1961 edition handed down from a friend of my parents.

From what I understand, at least at one time this one and Chapman's were among the books that the USCG used in training its sailors.

Dave

Re: Perl and Solving Trig/Converting GPS to x,y Cords
by BrowserUk (Pope) on Jan 08, 2013 at 22:11 UTC

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Perl and Solving Trig/Converting GPS to x,y Cords
by roboticus (Chancellor) on Jan 08, 2013 at 20:39 UTC

It may not be quite that simple. Flat maps of spheres always have distortion. (Look up "spherical conformal mapping".) So circles on your map won't be the same as circles on the sphere. In addition, the type of projection used will affect the conversion of your planar coordinates to your spherical coordinates.

I'd suggest you find out what your error tolerances are, the largest distance you're going to be interested in, and what sorts of maps you'll be using.

...roboticus

When your only tool is a hammer, all problems look like your thumb.

The Earth is round, but very big.

Between +70 and -70 degrees and distances under about 20 kilometers, bog standard 2D trig will produce results with errors less than 0.2%.

Between +-50 degs, it is less that 0.1%

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

That may be true for all the practical cases for the OP, but it's not generally true. A trivial example being several of the polar maps. In those cases, the equator would give terrible results, and the center pole would give similarly good results.

For anyone interested, some really interesting stuff is at http://www.progonos.com/furuti/MapProj/Normal/TOC/cartTOC.html, especially the "Mathematics of Cartography" section.

...roboticus

When your only tool is a hammer, all problems look like your thumb.

Re: Perl and Solving Trig/Converting GPS to x,y Cords
by Pizentios (Scribe) on Jan 09, 2013 at 14:33 UTC

Thanks for the replies. I should have specified the range of the 2d map is relativly small when compaired to the surface of the earth. The area that i will be working within is about 649,947 km^2. From the 49th parallel to the 60th parallel (all of the gps coordinates that i have worked with so far haven't been above the 54th parallel). The actual working area will likly be smaller (more like half or less) than those numbers, but the numbers above are for sure the max area that i will be working with.

In reality a 0.3% error tolerance for my purposes will likely be fine...and from what i've read/understand, the smaller the area the lower the errors will be. Thanks so far for the help!

-Pizentios

Create A New User
Node Status?
node history
Node Type: perlquestion [id://1012321]
Approved by ww
Front-paged by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2018-08-21 03:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Asked to put a square peg in a round hole, I would:

Results (197 votes). Check out past polls.

Notices?