<?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: test.php,v 1.5 2004/11/24 19:18:44 migurski Exp $
    /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

    
header('Content-Type: text/plain');
    require_once(
'PHPUnit.php');
    
    
chdir(dirname(__FILE__).'/..');
    require_once(
'Linear.php');
    require_once(
'Albers_Conical_EqualArea.php');
    require_once(
'Lambert_Conformal_Conic.php');
    require_once(
'Lambert_Azimuthal_EqualArea.php');
    require_once(
'Mercator.php');
    
    class 
Map_TestCase extends PHPUnit_TestCase {
        var 
$projection;
        var 
$points;
        
        function 
Map_TestCase($name) {
            
$this->PHPUnit_TestCase($name);
        }
        
        function 
define_points()
        {
            
// example points, located in Connecticut (United States)
            
$this->points = array('center' => array('lat' => deg2rad(41.79), 'lon' => deg2rad(-72.72)),
                                  
'top' =>    array('lat' => deg2rad(42.79), 'lon' => deg2rad(-72.72)),
                                  
'right' =>  array('lat' => deg2rad(41.79), 'lon' => deg2rad(-71.72)),
                                  
'bottom' => array('lat' => deg2rad(40.79), 'lon' => deg2rad(-72.72)),
                                  
'left' =>   array('lat' => deg2rad(41.79), 'lon' => deg2rad(-73.72)));
        }
        
        function 
setUp() {
            
$this->projection = new Linear_Projection();
            
$this->define_points();

            
$this->projection->addTransform('rotate'15);
            
$this->projection->addTransform('scale'2);
            
$this->projection->addTransform('translate', -1, -1);
            
$this->projection->addTransform('rotate', -15);
        }
        
        function 
point_accuracy($original$name)
        {
            
$projected $original;
            
$this->projection->project($projected);

            
$unprojected $projected;
            
$this->projection->unproject($unprojected);
            
            
$this->assertEquals(round($unprojected['lat'], 5), round($original['lat'], 5), "original latitude and processed latitude of '{$name}' should be within 0.00001");
            
$this->assertEquals(round($unprojected['lon'], 5), round($original['lon'], 5), "original longitude and processed longitude of '{$name}' should be within 0.00001");
        }
        
        function 
test_projection_accuracy()
        {
            
$this->point_accuracy($this->points['center'], 'center');
            
$this->point_accuracy($this->points['top'],    'top');
            
$this->point_accuracy($this->points['right'],  'right');
            
$this->point_accuracy($this->points['bottom'], 'bottom');
            
$this->point_accuracy($this->points['left'],   'left');
        }

        function 
test_relative_positions()
        {
            
$projected $this->points;
            
//echo "first:\n"; print_r($projected);
            
$this->projection->project($projected);
            
//echo "then:\n"; print_r($projected);

            
$this->assertTrue(   ($projected['top']['y'] > $projected['center']['y']), 'top should be above center');
            
$this->assertTrue(($projected['center']['y'] > $projected['bottom']['y']), 'center should be above bottom');
            
$this->assertTrue( ($projected['right']['x'] > $projected['center']['x']), 'right should be to the right of center');
            
$this->assertTrue(($projected['center']['x'] > $projected['left']['x']),   'center should be to the right of left');
        }
    }

    class 
Albers_Conical_EqualArea_TestCase extends Map_TestCase {
        function 
Albers_Conical_EqualArea_TestCase($name) {
            
$this->PHPUnit_TestCase($name);
        }
        
        function 
setUp() {
            
// example projection, centered on Connecticut (United States)
            
$this->projection = new Albers_Conical_EqualArea_Projection(deg2rad(40.8333), deg2rad(-72.25), deg2rad(41.2), deg2rad(41.8666));
            
$this->define_points();

            
$this->projection->addTransform('rotate'15);
            
$this->projection->addTransform('translate', -1, -1);
            
$this->projection->addTransform('rotate', -15);
            
$this->projection->addTransform('scale'2);
        }
    }

    class 
Lambert_Conformal_Conic_TestCase extends Map_TestCase {
        function 
Lambert_Conformal_Conic_TestCase($name) {
            
$this->PHPUnit_TestCase($name);
        }
        
        function 
setUp() {
            
// example projection, centered on Connecticut (United States)
            
$this->projection = new Lambert_Conformal_Conic_Projection(deg2rad(40.8333), deg2rad(-72.25), deg2rad(41.2), deg2rad(41.8666));
            
$this->define_points();

            
$this->projection->addTransform('translate', -1, -1);
            
$this->projection->addTransform('rotate'15);
            
$this->projection->addTransform('scale'2);
            
$this->projection->addTransform('rotate', -15);
        }
    }

    class 
Lambert_Azimuthal_EqualArea_TestCase extends Map_TestCase {
        function 
Lambert_Azimuthal_EqualArea_TestCase($name) {
            
$this->PHPUnit_TestCase($name);
        }
        
        function 
setUp() {
            
// example projection, centered on continental United States
            
$this->projection = new Lambert_Azimuthal_EqualArea_Projection(deg2rad(45), deg2rad(-100));
            
$this->define_points();

            
$this->projection->addTransform('rotate', -15);
            
$this->projection->addTransform('translate', -1, -1);
            
$this->projection->addTransform('rotate'15);
            
$this->projection->addTransform('scale'2);
        }
    }

    class 
Mercator_TestCase extends Map_TestCase {
        function 
Mercator_TestCase($name) {
            
$this->PHPUnit_TestCase($name);
        }
        
        function 
setUp() {
            
$this->projection = new Mercator_Projection();
            
$this->define_points();

            
$this->projection->addTransform('translate', -1, -1);
            
$this->projection->addTransform('rotate', -15);
            
$this->projection->addTransform('scale'2);
            
$this->projection->addTransform('rotate'15);
        }
    }

    
$suite  = new PHPUnit_TestSuite('Map_TestCase');
    
$result PHPUnit::run($suite);
    echo 
$result->toString();
    
    
$suite  = new PHPUnit_TestSuite('Albers_Conical_EqualArea_TestCase');
    
$result PHPUnit::run($suite);
    echo 
$result->toString();

    
$suite  = new PHPUnit_TestSuite('Lambert_Conformal_Conic_TestCase');
    
$result PHPUnit::run($suite);
    echo 
$result->toString();

    
$suite  = new PHPUnit_TestSuite('Lambert_Azimuthal_EqualArea_TestCase');
    
$result PHPUnit::run($suite);
    echo 
$result->toString();

    
$suite  = new PHPUnit_TestSuite('Mercator_TestCase');
    
$result PHPUnit::run($suite);
    echo 
$result->toString();
    

?>