<?php
    
// +----------------------------------------------------------------------+
    // | PHP version 4                                                        |
    // +----------------------------------------------------------------------+
    // | Copyright (c) 2003-2004 Michal Migurski                              |
    // +----------------------------------------------------------------------+
    // | This source file is subject to version 3.0 of the PHP license,       |
    // | that is bundled with this package in the file LICENSE, and is        |
    // | available through the world-wide-web at the following url:           |
    // | http://www.php.net/license/3_0.txt.                                  |
    // | If you did not receive a copy of the PHP license and are unable to   |
    // | obtain it through the world-wide-web, please send a note to          |
    // | license@php.net so we can mail you a copy immediately.               |
    // +----------------------------------------------------------------------+
    // | Author: Michal Migurski <mike@teczno.com>                            |
    // +----------------------------------------------------------------------+
    //
    // $Id: Mercator.php,v 1.3 2004/11/24 19:18:44 migurski Exp $
    /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

    // theta: latitude (N/S)
    // lambda: longitude (E/W)
    // DON'T FORGET: deg2rad()
    
    
require_once('Linear.php');
    
   
/** Mercator_Projection
    * calculates Mercator Projection
    * http://mathworld.wolfram.com/MercatorProjection.html
    */
    
class Mercator_Projection extends Linear_Projection
    
{
        var 
$transform;
        var 
$inverse;
    
        function 
Mercator_Projection()
        {
            
$this->initializeTransform();
        }
        
       
/** _GPSToMap
        * calculates map x,y coordinate from gps lat/long coordinates
        *
        * @param    lat     float       latitude, in radians
        * @param    lon     float       longitude, in radians
        *
        * @return   array   'x', 'y' elements, map coordinates
        */
        
function _GPSToMap($lat$lon)
        {
            
$y log10(tan((M_PI 4) + ($lat 2)));
            
$x $lon;
            
            
$vector = new Math_Matrix(array(array($x$y1)));
            
$vector->multiply($this->transform);

            return array(
'x' => $vector->getElement(00),
                         
'y' => $vector->getElement(01));
        }
    
       
/** _mapToGPS
        * calculates gps lat/long coordinate from map coordinates.
        *
        * @param    x       float       horizontal map position
        * @param    y       float       vertical map position
        *
        * @return   array   'lat', 'lon' elements, gps coordinates in radians
        */
        
function _mapToGPS($x$y)
        {
            
$vector = new Math_Matrix(array(array($x$y1)));
            
$vector->multiply($this->inverse);
            
            
$x $vector->getElement(00);
            
$y $vector->getElement(01);

            
$lat * (atan(pow(10$y)) - (M_PI 4));
            
$lon $x;
            
            return array(
'lat' => $lat'lon' => $lon);
        }
    }

?>