Location: PHPKode > projects > jjfmapper > jjfmapper/lib/coordial.php
<?php #-*-Mode: php; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
/*
    coordial, a class for parsing latitude longitude strings for PHP 4.
    Copyright (C) 2004  John J Foerch

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

//a class for parsing several notations of latitude and longitude.
//written and maintained by John J Foerch.
//this source code is released to the public domain.

class coordial {
    
    var $latitude;
    var $longitude;
    var $terms=array();
    var $degchar;
    
    //////////////////////////////////////////////////////////
    //  As it is necessary, programmatically, to unambiguously name
    //  the different formats of notation for latitude and longitude
    //  coordinates, I have devised the following system.
    //
    //  t = direction marker (N, S, E, W)
    //  d = degrees number
    //  m = minutes number
    //  s = seconds number
    //  c = decimal number
    //
    //////////////////////////////////////////////////////////
    
    function coordial () {
        if (func_num_args() == 0)
        {
            $this->latitude=0;
            $this->longitude=0;
            return;
        } else {
            $working_string = strtoupper(func_get_arg(0));
        }
        if (func_num_args() > 1)
        {
            $this->degchar = func_get_arg(1);
        } else {
            $this->degchar = '°';
        }
        $working_string = str_replace("\r",'',$working_string);
        //convert whitespace to 0x20
        $working_string = str_replace("\n",' ',$working_string);
        $working_string = str_replace("\t",' ',$working_string);
        //add a space after each term marker
        $working_string = str_replace('*','',$working_string);
        $working_string = str_replace('°','',$working_string);
        $working_string = str_replace('\'','',$working_string);
        $working_string = str_replace('\\','',$working_string);
        $working_string = str_replace('"','',$working_string);
        //pad directional marker
        $working_string = str_replace('N',' N ',$working_string);
        $working_string = str_replace('S',' S ',$working_string);
        $working_string = str_replace('E',' E',$working_string);
        $working_string = str_replace('W',' W ',$working_string);
        //collapse spaces
        $working_string = preg_replace('/\s+/',' ',$working_string);
        $working_string = trim($working_string);
        
        $this->terms = explode(' ',$working_string);
        
        
        $latidx = array_search('N',$this->terms);
        if ($latidx === false) $latidx = array_search('S',$this->terms);
        $lonidx = array_search('E',$this->terms);
        if ($lonidx === false) $lonidx = array_search('W',$this->terms);
        
        if ($latidx === false && $lonidx === false &&
            count ($this->terms) == 2)
        {
            $this->latitude = $this->terms[0];
            $this->longitude = $this->terms[1];
        }
        if ($this->terms[0] == 'N' || $this->terms[0] == 'S')
        {
            $latnumterms = $lonidx - 1;
            $lonnumterms = count ($this->terms) - ($lonidx + 1);
            if ($latnumterms == 1)
            {
                $this->latitude =
                    $this->terms[1] * ($this->terms[0] == 'N'? 1 : -1);
            } elseif ($latnumterms == 2) {
                $this->latitude =
                    ($this->terms[1] + $this->terms[2] / 60) *
                    ($this->terms[0] == 'N'?1:-1);
            } elseif ($latnumterms == 3) {
                $this->latitude =
                    ($this->terms[1] + $this->terms[2] / 60 +
                     $this->terms[3] / 3600) * ($this->terms[0] == 'N'?1:-1);
            }
            if ($lonnumterms == 1) {
                $this->longitude =
                    $this->terms[$lonidx + 1] *
                    ($this->terms[$lonidx] == 'E'? 1 : -1);
            } elseif ($lonnumterms == 2) {
                $this->longitude =
                    ($this->terms[$lonidx + 1] +
                     $this->terms[$lonidx + 2] / 60) *
                    ($this->terms[$lonidx] == 'E'?1:-1);
            } elseif ($lonnumterms == 3) {
                $this->longitude =
                    ($this->terms[$lonidx + 1] +
                     $this->terms[$lonidx + 2] / 60 +
                     $this->terms[$lonidx + 3] / 3600) *
                    ($this->terms[$lonidx] == 'E'?1:-1);
            }
        }
        if ($this->terms[count($this->terms)-1] == 'E' ||
            $this->terms[count($this->terms)-1] == 'W')
        {
            $latnumterms = $latidx;
            $lonnumterms = $lonidx - ($latidx + 1);
            if ($latnumterms == 1)
            {
                $this->latitude =
                    $this->terms[0] * ($this->terms[1] == 'N'? 1 : -1);
            } elseif ($latnumterms == 2) {
                $this->latitude =
                    ($this->terms[0] + $this->terms[1] / 60) *
                    ($this->terms[2] == 'N'?1:-1);
            } elseif ($latnumterms == 3) {
                $this->latitude =
                    ($this->terms[0] + $this->terms[1] / 60 +
                     $this->terms[2] / 3600) * ($this->terms[3] == 'N'?1:-1);
            } if ($lonnumterms == 1) {
                $this->longitude =
                    $this->terms[$lonidx - 1] *
                    ($this->terms[$lonidx] == 'E'? 1 : -1);
            } elseif ($lonnumterms == 2) {
                $this->longitude =
                    ($this->terms[$lonidx - 2] +
                     $this->terms[$lonidx - 1] / 60) *
                    ($this->terms[$lonidx] == 'E'?1:-1);
            } elseif ($lonnumterms == 3) {
                $this->longitude =
                    ($this->terms[$lonidx - 3] +
                     $this->terms[$lonidx - 2] / 60 +
                     $this->terms[$lonidx - 1] / 3600) *
                    ($this->terms[$lonidx] == 'E'?1:-1); }
        }
        
    }
    
    function latitude_dc() {
        $a = $this->latitude;
        if (func_num_args() > 0)
        {
            $sd = func_get_arg(0);
        } else {
            $sd = 5;
        }
        $a = round ($a, $sd);
        return $a;
    }
    function longitude_dc() {
        $a = $this->longitude;
        if (func_num_args() > 0)
        {
            $sd = func_get_arg(0);
        } else {
            $sd = 5;
        }
        $a = round ($a, $sd);
        return $a;
    }
    function latitude_tdmc() {
        $a = (abs($this->latitude) - floor(abs($this->latitude))) * 60;
        if (func_num_args() > 0)
        {
            $sd = func_get_arg(0);
        } else {
            $sd = 3;
        }
        $a = round ($a, $sd);
        return (($this->latitude >= 0?'N ':'S ') .
                floor(abs($this->latitude)) . $this->degchar ." $a'");
    }
    function longitude_tdmc() {
        $a = (abs($this->longitude) - floor(abs($this->longitude))) * 60;
        if (func_num_args() > 0)
        {
            $sd = func_get_arg(0);
        } else {
            $sd = 3;
        }
        $a = round ($a, $sd);
        return (($this->longitude >= 0?'E ':'W ') .
                floor(abs($this->longitude)) . $this->degchar . " $a'");
    }
    function latitude_dmct() {
        $a = (abs($this->latitude) - floor(abs($this->latitude))) * 60;
        if (func_num_args() > 0)
        {
            $sd = func_get_arg(0);
        } else {
            $sd = 3;
        }
        $a = round ($a, $sd);
        return (floor(abs($this->latitude)) .
                $this->degchar . " $a' " . ($this->latitude >= 0?'N ':'S '));
    }
    function longitude_dmct() {
        $a = (abs($this->longitude) - floor(abs($this->longitude))) * 60;
        if (func_num_args() > 0)
        {
            $sd = func_get_arg(0);
        } else {
            $sd = 3;
        }
        $a = round ($a, $sd);
        return (floor(abs($this->longitude)) .
                $this->degchar . " $a' " . ($this->longitude >= 0?'E ':'W '));
    }
    function latitude_tdms() {
        $a = (abs($this->latitude) - floor(abs($this->latitude))) * 60;
        $b = $a - floor($a);
        $a -= $b;
        $b *= 60;
        if (func_num_args() > 0)
        {
            $sd = func_get_arg(0);
        } else {
            $sd = 0;
        }
        $b = round ($b, $sd);
        return (($this->latitude >= 0?'N ':'S ') .
                floor(abs($this->latitude)) . $this->degchar . " $a' $b\"");
    }
    function longitude_tdms() {
        $a = (abs($this->longitude) - floor(abs($this->longitude))) * 60;
        $b = $a - floor($a);
        $a -= $b;
        $b *= 60;
        if (func_num_args() > 0)
        {
            $sd = func_get_arg(0);
        } else {
            $sd = 0;
        }
        $b = round ($b, $sd);
        return (($this->longitude >= 0?'E ':'W ') .
                floor(abs($this->longitude)) . $this->degchar . " $a' $b\"");
    }
    function latitude_dmst() {
        $a = (abs($this->latitude) - floor(abs($this->latitude))) * 60;
        $b = $a - floor($a);
        $a -= $b;
        $b *= 60;
        if (func_num_args() > 0)
        {
            $sd = func_get_arg(0);
        } else {
            $sd = 0;
        }
        $b = round ($b, $sd);
        return (floor(abs($this->latitude)) .$this->degchar .
                " $a' $b\" " . ($this->latitude >= 0?'N ':'S '));
    }
    function longitude_dmst() {
        $a = (abs($this->longitude) - floor(abs($this->longitude))) * 60;
        $b = $a - floor($a);
        $a -= $b;
        $b *= 60;
        if (func_num_args() > 0)
        {
            $sd = func_get_arg(0);
        } else {
            $sd = 0;
        }
        $b = round ($b, $sd);
        return (floor(abs($this->longitude)) . $this->degchar .
                " $a' $b\" " . ($this->longitude >= 0?'E ':'W '));  
    }
    function islegal() {
        if ($this->latitude >= -90 && $this->latitude <= 90 &&
            $this->longitude >= -180 && $this->longitude <= 180)
        {
            return true;
        }
        return false;
    }
    
    
}//class coordial

?>
Return current item: jjfmapper