<?

if(isset($_GET['source'])) {
    
highlight_file(__FILE__);
    exit;
}

$EARTH_RADIUS_EQUATOR 6378140.0;
$RADIAN 180 pi();



if (
$_POST) {
    
$heading $_REQUEST['heading'];
    
settype($heading,'double');
    if (
$heading 180)
        
$heading -= 360.0;
        
    list(
$long,$lat) = calcLatLong($_REQUEST['long'], $_REQUEST['lat'], $_REQUEST['range'], $heading);

    
$bearing 360.0 $heading;
    if (
$bearing 0)
        
$bearing += 360.0;
    if (
$bearing 360)
        
$bearing -= 360.0;
    
$tilt 80;
    
$range $_REQUEST['range'];
    
    if (
$_REQUEST['debug']) {
        print 
"<b>Input:</b>";
        print 
"<PRE>";
        
print_r($_REQUEST);
        print 
"</PRE>";

        print 
"<b>Output:</b>";
        print 
"<PRE>";
        print 
"LONG: $long\n";
        print 
"LAT: $lat\n";
        print 
"RANGE: $range\n";
        print 
"TILT: $tilt\n";
        print 
"HEADING: $bearing [".heading_string($bearing)."]\n";
        print 
"</PRE>";

    } else {
        
#LOOKAT=[lookatLon],[lookatLat],[lookatRange],[lookatTilt],[lookatHeading],[horizFov],[vertFov]
        
header("Location: /googleEarth.kml.php?LOOKAT=$long,$lat,$range,$tilt,$bearing&p=".heading_string($bearing));
        
//http://www.nearby.org.uk
    
}
    exit;
} else {
    if (empty(
$_REQUEST['lat']))
        
$_REQUEST['lat'] = 53.5;
    if (empty(
$_REQUEST['long']))
        
$_REQUEST['long'] = -2.1;
    if (empty(
$_REQUEST['heading']))
        
$_REQUEST['heading'] = 90;
    if (empty(
$_REQUEST['range']))
        
$_REQUEST['range'] = 10000;
    
}

/**
* Calculate the unknown coordinates at bearing and distance from known coordinates

* Developed by TJ (http://tjworld.net/software/kml/) for Google Earth applications
*
* @param    float    long        Longitude of known point (decimal degreees)
* @param    float    lat        Latitude of known point (decimal degrees)
* @param    float    distance    Distance to unknown point (meters)
* @param    float    bearing        Bearing (angle in degrees) to unknown point.
*                    Measured from North=0, -ve going to west (-90), +ve going to east (90)
* @returns    array            Associative array containing unknown 'longitude' and 'latitude'
*/

function calcLatLong($long$lat$distance$bearing) {
 global 
$EARTH_RADIUS_EQUATOR$RADIAN;
 
$b $bearing $RADIAN;
 
$long $long $RADIAN;
 
$lat $lat $RADIAN;
 
$f 1/298.257;
 
$e 0.08181922;
    
 
$R $EARTH_RADIUS_EQUATOR * ($e $e) / pow( ($e*$e pow(sin($lat),2)), 1.5);    
 
$psi $distance/$R;
 
$phi pi()/$lat;
 
$arccos cos($psi) * cos($phi) + sin($psi) * sin($phi) * cos($b);
 
$latA = (pi()/acos($arccos)) * $RADIAN;

 
$arcsin sin($b) * sin($psi) / sin($phi);
 
$longA = ($long asin($arcsin)) * $RADIAN;
 return array(
$longA,$latA);
}

//source: geograph.org.uk source
function heading_string($deg) {
    
$dirs = array('north','east','south','west'); 
    
$rounded round($deg 22.5) % 16
    if (
$rounded 0)
        
$rounded += 16;
    if ((
$rounded 4) == 0) { 
        
$s $dirs[$rounded/4]; 
    } else { 
        
$s $dirs[intval(((intval($rounded 4) + 1) % 4) / 2)]; 
        
$s .= $dirs[intval($rounded 8)]; 
        if (
$rounded == 1) { 
            
$s $dirs[round($rounded/4) % 4] . '-' $s;
        } 
    } 
    return 
$s


?>

<title>LookFrom for Google Earth :: nearby.org.uk</title>

<body style="font-family:arial">
<p align=right>Brought to you by <a href="http://www.nearby.org.uk/">nearby.org.uk</a>.</p>

<h2>LookFrom for Google Earth</h2>




<form action="project-kml.php?submitted=1" method="POST" style="background-color:#dddddd; border:1px solid black; padding:10px; width:600px" name="form1">

   
   
   <b>You want to stand at:</b> <!--small>(you can get here via the <a href="http://www.nearby.org.uk/coord.cgi">coordinate converter</a>)</small--><br/>
   Longitude: <input name="long" type="text" size="10" value="<?= $_REQUEST['long']; ?>"/><br/>
   Latitude: <input name="lat" type="text" size="10" value="<?= $_REQUEST['lat']; ?>"/> <small>decimal degrees</small><br/>
   Elevation: 0 meters <small>(dont have the maths to offer otherwise)</small><br/><br/>
   
   <b>You want to look:</b><br/>
   Heading: <input name="heading" type="text" size="5" value="<?= $_REQUEST['heading']; ?>"/>degrees <small>(North=0, -ve going to west (-90), +ve going to east (90))</small><br/>
   Range: <input name="range" type="text" size="5" value="<?= $_REQUEST['range']; ?>"/>meters<br/>
   Altitude: 0 degrees <small>(horizontal, <a href="http://www.heavens-above.com/gloss.asp?term=azimuth">expanation</a>)</small><br/><br/>
   
    <input type="submit" value="Go" /><br/><br/>
    <input name="debug" type="checkbox"/>Debug (output the values rather than KML)
</form>

<ul>
<li>if there is interest could add 'Lookat' point, so could use that instead of heading and range...</li>

<li>Currently sets a fixed tilt of 80degrees, using 90 sometimes caused strange jumps due to hitting terrain</li>

<li>Thanks to the Maths found in TJ1's <a href="http://bbs.keyhole.com/ubb/showflat.php?Cat=&Board=SupportKML&Number=166379&Searchpage=1&Main=166379&Words=calculate+TJ1&topic=&Search=true#Post166379">postings</a> in the GEC. <a href="?source=1">View Source</a></li>

<li style="color:red"><small>*** <b>It works for <A HREF="mailto:nearby_REMOVE_@barryhunter.co.uk">me</a> but your milage may vary! Use at your own risk, no garentee is offered.</b> ***</small></li>
</ul>