[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

12/12/2007 at 09:35AM PST, ID: 23018885
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

8.6

Initial Bearing Calculation error in PHP

Asked by mdougan in PHP Scripting Language, Algorithms

Tags: bearing, calculate, heading, php

I had some navigation code written in VB that I tried to convert to PHP.  It seemed fine until I started seeing errors when using Lat/Lons that appeared in the Eastern or Southern hemispheres.  

The output of the code sample below produces these results:

heading east in the northern/western hemisphere: 90
heading west in the northern/western hemisphere: 270
heading north in the northern/western hemisphere: 339
heading south in the northern/western hemisphere: nan

heading east in the northern/eastern hemisphere: 270
heading west in the northern/eastern hemisphere: 90
heading north in the northern/eastern hemisphere: 339
heading south in the northern/eastern hemisphere: nan

heading east in the southern/western hemisphere: 90
heading west in the southern/western hemisphere: 270
heading north in the southern/western hemisphere: nan
heading south in the southern/western hemisphere: 204

heading east in the southern/eastern hemisphere: 270
heading west in the southern/eastern hemisphere: 90
heading north in the southern/eastern hemisphere: nan
heading south in the southern/eastern hemisphere: 204

It appears my East/West calculations get reversed in the Eastern Hemisphere.

In all cases, I'd want to see either 0 or 360 for the North and 180 for the South bearings, I don't understand the readings that I'm getting here at all... and not sure whether "nan" is another way of saying 0?

I tried looking up alternative ways to calculate the initial bearing, and found this website, that describes a method using JavaScript.  

http://www.movable-type.co.uk/scripts/latlong.html

However, I tried converting that to PHP (found in the HeadingNew function in the code sample) and it gives me this output:

heading east in the northern/western hemisphere: 90
heading west in the northern/western hemisphere: -90
heading north in the northern/western hemisphere: 0
heading south in the northern/western hemisphere: 0

heading east in the northern/eastern hemisphere: 90
heading west in the northern/eastern hemisphere: -90
heading north in the northern/eastern hemisphere: 0
heading south in the northern/eastern hemisphere: 0

heading east in the southern/western hemisphere: 90
heading west in the southern/western hemisphere: -90
heading north in the southern/western hemisphere: 0
heading south in the southern/western hemisphere: 0

heading east in the southern/eastern hemisphere: 90
heading west in the southern/eastern hemisphere: -90
heading north in the southern/eastern hemisphere: 0
heading south in the southern/eastern hemisphere: 0

That is almost correct, except for the South calculation (and, I'm not sure if the North calculation is actually working or not).  If I used the new heading function, I'd have to find a way to convert the results to a compass bearing too.

Can anyone help me get either routine working, or do you have a sample of a completely different routine that will work in PHP?    

Thanks!
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
<?php
 
	    function HeadingNew($lat1, $long1, $lat2, $long2) {
	       // Takes lat/longs as expressions in Degrees
	       $rLat1 = 0.0;
	       $rLong1 = 0.0;
	       $rLat2 = 0.0;
	       $rLong2 = 0.0;
	       $d = 0.0;
	       $h = 0.0;
	       $result = 0.0;
 
	       $rLat1 = deg2rad($lat1);
	       $rLong1 = deg2rad($long1);
 
	       $rLat2 = deg2rad($lat2);
	       $rLong2 = deg2rad($long2);
 
 
                $d = deg2rad($long2 - $long1);
                $y = sin($d) * cos($rlat2);
                $x = cos($rlat1) * sin($rlat2) - sin($rlat1) * cos($rlat2) * cos($d);
                $h = atan2($y, $x);
                //echo "D = ".$d." X = ".$x." Y = ".$y." H = ".$h."<br>";
 
 	        $result = rad2deg($h);
 
 
	       return($result);
	    }
 
	    function Heading($lat1, $long1, $lat2, $long2) {
	       // Takes lat/longs as expressions in Degrees
	       $pi = 3.14159265358979;
	       $rLat1 = 0.0;
	       $rLong1 = 0.0;
	       $rLat2 = 0.0;
	       $rLong2 = 0.0;
	       $d = 0.0;
	       $h = 0.0;
	       $result = 0.0;
 
	       $rLat1 = deg2rad($lat1);
	       $rLong1 = deg2rad($long1);
 
	       $rLat2 = deg2rad($lat2);
	       $rLong2 = deg2rad($long2);
 
 
	       // Distance in Meters Using Radians
	       // Less subject to rounding errors for small distances
	        $d = ( 2 * asin(sqrt(pow(sin(($rLat1 - $rLat2) / 2), 2) + cos($rLat1) * cos($rLat2) * pow(sin(($rLong1 - $rLong2) / 2),2))));
 
	       if (sin(ABS($rLong2) - ABS($rLong1)) < 0) {
                  //echo "top calc <br>";
	          $h = acos(((sin($rLat2) - sin($rLat1)) * cos($d)) / (sin($d) * cos($rLat1)));
	          }
	       else {
                  //echo "bottom calc <br>";
	          $h = 2 * $pi - acos(((sin($rLat2) - sin($rLat1)) * cos($d)) / (sin($d) * cos($rLat1)));
	          }
 
	       $result = rad2deg($h);
 
 
	       return($result);
	    }
 
 
	 $dHeading = number_format(Heading(30, -30, 30, -20));
         echo "heading east in the northern/western hemisphere: ".$dHeading."<br>";
	 $dHeading = number_format(Heading(30, -20, 30, -30));
         echo "heading west in the northern/western hemisphere: ".$dHeading."<br>";
	 $dHeading = number_format(Heading(30, -30, 40, -30));
         echo "heading north in the northern/western hemisphere: ".$dHeading."<br>";
	 $dHeading = number_format(Heading(40, -30, 30, -30));
         echo "heading south in the northern/western hemisphere: ".$dHeading."<p>";
 
 
	 $dHeading = number_format(Heading(30, 30, 30, 40));
         echo "heading east in the northern/eastern hemisphere: ".$dHeading."<br>";
	 $dHeading = number_format(Heading(30, 40, 30, 30));
         echo "heading west in the northern/eastern hemisphere: ".$dHeading."<br>";
	 $dHeading = number_format(Heading(30, 30, 40, 30));
         echo "heading north in the northern/eastern hemisphere: ".$dHeading."<br>";
	 $dHeading = number_format(Heading(40, 30, 30, 30));
         echo "heading south in the northern/eastern hemisphere: ".$dHeading."<p>";
 
 
	 $dHeading = number_format(Heading(-50, -50, -50, -40));
         echo "heading east in the southern/western hemisphere: ".$dHeading."<br>";
	 $dHeading = number_format(Heading(-50, -40, -50, -50));
         echo "heading west in the southern/western hemisphere: ".$dHeading."<br>";
	 $dHeading = number_format(Heading(-50, -50, -40, -50));
         echo "heading north in the southern/western hemisphere: ".$dHeading."<br>";
	 $dHeading = number_format(Heading(-40, -50, -50, -50));
         echo "heading south in the southern/western hemisphere: ".$dHeading."<p>";
 
 
	 $dHeading = number_format(Heading(-50, 50, -50, 60));
         echo "heading east in the southern/eastern hemisphere: ".$dHeading."<br>";
	 $dHeading = number_format(Heading(-50, 60, -50, 50));
         echo "heading west in the southern/eastern hemisphere: ".$dHeading."<br>";
	 $dHeading = number_format(Heading(-50, 50, -40, 50));
         echo "heading north in the southern/eastern hemisphere: ".$dHeading."<br>";
	 $dHeading = number_format(Heading(-40, 50, -50, 50));
         echo "heading south in the southern/eastern hemisphere: ".$dHeading."<br>";
 
 
     ?>
[+][-]12/12/07 05:07 PM, ID: 20461584

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]12/12/07 08:22 PM, ID: 20462348

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]12/13/07 02:06 PM, ID: 20468161

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: PHP Scripting Language, Algorithms
Tags: bearing, calculate, heading, php
Sign Up Now!
Solution Provided By: AlexanderR
Participating Experts: 1
Solution Grade: A
 
 
[+][-]12/13/07 02:08 PM, ID: 20468185

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]12/14/07 07:51 AM, ID: 20472260

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]12/14/07 09:31 AM, ID: 20472988

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091111-EE-VQP-91 / EE_QW_2_20070628