<?php
/**
This file has two parts:

* a heavily stripped-down version of the "gPoint.php class"
  written by Brenor Brophy  http://www.brenorbrophy.com
  (original found at http://www.phpclasses.org)

* a program to connect to www.emaps.bg, converting geographic coordinates
  to UTM coordinates, made for the purposes of bg.wikipedia.org
  written by Petko Yotov http://5ko.free.fr/


This file may be copied and modified under the terms of the
*** GNU Ceneral Public License version 2 or newer ***

*/





/*------------------------------------------------------------------------------
** PHP class to convert Latitude & Longitude coordinates into
**                UTM & Lambert Conic Conformal Northing/Easting coordinates.
** Version:        1.0
** Author:        Brenor Brophy
** Email & URL:    brenor@sbcglobal.net www.brenorbrophy.com
**
** Code for datum and UTM conversion was converted from C++ code written by
** Chuck Gantz (chuck.gantz@globalstar.com) from  http://www.gpsy.com/gpsinfo/geotoutm/
** This URL has many other references to useful information concerning conversion
** of coordinates.
*/
define ("meter2nm", (1/1852));
define ("nm2meter"1852);

/*------------------------------------------------------------------------------
** class gPoint        ... for Geographic Point
**
** This class encapsulates the methods for representing a geographic point on the
** earth in three different coordinate systema. Lat/Long, UTM and Lambert Conic
** Conformal.
*/
class gPoint
{
/* Reference ellipsoids derived from Peter H. Dana's website-
** http://www.utexas.edu/depts/grg/gcraft/notes/datum/elist.html
** Department of Geography, University of Texas at Austin
** Internet: pdana@mail.utexas.edu 3/22/95
**
** Source:
** Defense Mapping Agency. 1987b. DMA Technical Report: Supplement to Department
** of Defense World Geodetic System 1984 Technical Report. Part I and II.
** Washington, DC: Defense Mapping Agency
*/
    
var $ellipsoid = array(//Ellipsoid name, Equatorial Radius, square of eccentricity
    
"Airy"                    =>array (63775630.00667054),
    
"Australian National"    =>array    (63781600.006694542),
    
"Bessel 1841"            =>array    (63773970.006674372),
    
"Bessel 1841 Nambia"    =>array    (63774840.006674372),
    
"Clarke 1866"            =>array    (63782060.006768658),
    
"Clarke 1880"            =>array    (63782490.006803511),
    
"Everest"                =>array    (63772760.006637847),
    
"Fischer 1960 Mercury"    =>array (63781660.006693422),
    
"Fischer 1968"            =>array (63781500.006693422),
    
"GRS 1967"                =>array    (63781600.006694605),
    
"GRS 1980"                =>array    (63781370.00669438),
    
"Helmert 1906"            =>array    (63782000.006693422),
    
"Hough"                    =>array    (63782700.00672267),
    
"International"            =>array    (63783880.00672267),
    
"Krassovsky"            =>array    (63782450.006693422),
    
"Modified Airy"            =>array    (63773400.00667054),
    
"Modified Everest"        =>array    (63773040.006637847),
    
"Modified Fischer 1960"    =>array    (63781550.006693422),
    
"South American 1969"    =>array    (63781600.006694542),
    
"WGS 60"                =>array (63781650.006693422),
    
"WGS 66"                =>array (63781450.006694542),
    
"WGS 72"                =>array (63781350.006694318),
    
"WGS 84"                =>array (63781370.00669438));

    
// Properties
    
var $a;                                        // Equatorial Radius
    
var    $e2;                                    // Square of eccentricity
    
var    $datum;                                    // Selected datum
    
var    $Xp$Yp;                                // X,Y pixel location
    
var $lat$long;                            // Latitude & Longitude of the point
    
var $utmNorthing$utmEasting$utmZone;    // UTM Coordinates of the point
    
var    $lccNorthing$lccEasting;                // Lambert coordinates of the point
    
var $falseNorthing$falseEasting;            // Origin coordinates for Lambert Projection
    
var $latOfOrigin;                            // For Lambert Projection
    
var $longOfOrigin;                            // For Lambert Projection
    
var $firstStdParallel;                        // For lambert Projection
    
var $secondStdParallel;                        // For lambert Projection

    // constructor
    
function gPoint($datum='WGS 84')            // Default datum is WGS 84
    
{
        
$this->$this->ellipsoid[$datum][0];        // Set datum Equatorial Radius
        
$this->e2 $this->ellipsoid[$datum][1];    // Set datum Square of eccentricity
        
$this->datum $datum;                        // Save the datum
    
}

    
//
    // Set/Get/Output Longitude & Latitude of the point
    //
    
function setLongLat($long$lat)
    {
        
$this->long $long$this->lat $lat;
    }
    function 
convertLLtoTM($LongOrigin='')
    {
        
$k0 0.9996;
        
$falseEasting 500000.0;

        
//Make sure the longitude is between -180.00 .. 179.9
        
$LongTemp = ($this->long+180)-(integer)(($this->long+180)/360)*360-180// -180.00 .. 179.9;
        
$LatRad deg2rad($this->lat);
        
$LongRad deg2rad($LongTemp);

        
$ZoneNumber=35// Added by Petko Yotov
        
$LongOrigin = ($ZoneNumber 1)*180 3;  //+3 puts origin in middle of zone

        
$LongOriginRad deg2rad($LongOrigin);

        
$eccPrimeSquared = ($this->e2)/(1-$this->e2);

        
$N $this->a/sqrt(1-$this->e2*sin($LatRad)*sin($LatRad));
        
$T tan($LatRad)*tan($LatRad);
        
$C $eccPrimeSquared*cos($LatRad)*cos($LatRad);
        
$A cos($LatRad)*($LongRad-$LongOriginRad);

        
$M $this->a*((1    $this->e2/4        3*$this->e2*$this->e2/64    5*$this->e2*$this->e2*$this->e2/256)*$LatRad
                            
- (3*$this->e2/8    3*$this->e2*$this->e2/32    45*$this->e2*$this->e2*$this->e2/1024)*sin(2*$LatRad)
                                                + (
15*$this->e2*$this->e2/256 45*$this->e2*$this->e2*$this->e2/1024)*sin(4*$LatRad)
                                                - (
35*$this->e2*$this->e2*$this->e2/3072)*sin(6*$LatRad));

        
$this->utmEasting = ($k0*$N*($A+(1-$T+$C)*$A*$A*$A/6
                        
+ (5-18*$T+$T*$T+72*$C-58*$eccPrimeSquared)*$A*$A*$A*$A*$A/120)
                        + 
$falseEasting);

        
$this->utmNorthing = ($k0*($M+$N*tan($LatRad)*($A*$A/2+(5-$T+9*$C+4*$C*$C)*$A*$A*$A*$A/24
                     
+ (61-58*$T+$T*$T+600*$C-330*$eccPrimeSquared)*$A*$A*$A*$A*$A*$A/720)));
        if(
$this->lat 0)
            
$this->utmNorthing += 10000000.0//10000000 meter offset for southern hemisphere
    
}
// end of class gPoint




// Next code by Petko Yotov
function create_link($lat$lon$ell)
{
    
$gpoint =& new gPoint($ell);
    
$gpoint->setLongLat($lon$lat);
    
$gpoint->convertLLtoTM('');
    
$mapX round($gpoint->utmEasting);
    
$mapY round($gpoint->utmNorthing);
    
$minx $mapX-14425;
    
$maxx $mapX+14425;
    
$miny $mapY-10000;
    
$maxy $mapY+10000;

    
$base_url "http://www.emaps.bg/emaps/content.asp?mapSize=1&cboMaps=0";

    
$url "$base_url&minx=$minx&maxx=$maxx&miny=$miny&maxy=$maxy";
    return 
$url;
}

function 
message($en$bg)
{
    
header("Content-type: text/html; charset=UTF-8");

    echo <<<EOF
<html><title>Error... Грешка...</title><body>
<h1>Error... Грешка...</h1>

<strong><big>$en</big></strong><br/>

You can:
<ul>
<li><a href='javascript:history.go(-1);'>Go back</a>.</li>
<li>Go to <a href='http://www.emaps.bg/'>www.emaps.bg</a>.</li>
<li>Go to <a href='http://bg.wikipedia.org/'>bg.wikipedia.org</a>.</li>
<li>View the <a href='
{$_SERVER["PHP_SELF"]}?q=source'>source code of this script</a>.</li>
</ul>
<br/>
<br/>
<strong><big>$bg</big></strong><br/>

Можете:
<ul>
<li><a href='javascript:history.go(-1);'>Да се върнете</a>.</li>
<li>Да посетите <a href='http://www.emaps.bg/'>www.emaps.bg</a>.</li>
<li>Да посетите <a href='http://bg.wikipedia.org/'>bg.wikipedia.org</a>.</li>
<li>Да прегледате <a href='
{$_SERVER["PHP_SELF"]}?q=source'>изходния код на този скрипт</a>.</li>
</ul>
<br/>
<br/>
<hr/>
<address>
<small>Script by <a href="http://5ko.free.fr/">Petko Yotov</a>.</small>
</address>
</body></html>

EOF;
    exit;


}


if(
strtolower($_GET['q']) == "source")
{
    
header("Content-type: text/html; charset=UTF-8");
    
highlight_file(__FILE__);
    exit;
}

if(!isset(
$_GET['q']) || preg_match("/[^0-9,\.]/"trim($_GET['q']) ) )
{
    
message("No coordinates requested or illegal character""Не са посочени координати или е въведен неразрешен символ");
}





list(
$lat$lon) = explode(","$_GET['q'], 2);

$lat floatval($lat); // 41..45
$lon floatval($lon); // 22..29

if($lat<41 || $lat>45 || $lon<22 || $lon>29 )
{
    
message("Coordinates are out of range. Lattitude '$lat' (must be 41..45), Longitude '$lon' (must be 22..29)""Координатите са извън картата. Ширина: '$lat' (трябва да е 41..45), дължина: '$lon' (трябва да е 22..29)");
}


$url create_link($lat$lon"WGS 84");
header("Location: $url");
exit;
?>