Call Nucleus Development [Nd] now, 347 688 6441 or contact us


content loading...

Code Repository

The code libraries listed to the right -> are available for public use. You may copy, distribute, and eat this code as you wish. However, the license remains in effect for the life of the software.
Pretense for download and use "[Nd] Libraries":
  • You may not use this code in a commercial setting without prior consent from [Nd].
  • You may not misrepresent yourself as the author of this code.
  • You may make changes to this code, but must make the changes available under this license.
  • These libraries are governed by their respective licenses.



<?php
 
/**
  * Zipcode Lookup Class
  *
  * php 4,5
  *
  * 
  * LICENSE: Use of this library is governed by the Creative Commons License Attribution 2.5. 
  * You can check it out at: http://creativecommons.org/licenses/by/2.5/legalcode
  *
  * - You may copy, distribute, and eat this code as you wish. You must give me credit for
  * writing it. You may not misrepresent yourself as the author of this code.
  * - You may not use this code in a commercial setting without prior consent from me.
  * - If you make changes to this code, you must make the changes available under a license like
  * this one.
  *
  * @category   Geo-Location 
  * @package    ZIPCODE
  * @author     R. Fritz Washabaugh <general@nucleusdevelopment.com>
  * @copyright  2011 Nucleus Development - R. Fritz Washabaugh
  * @license    http://creativecommons.org/licenses/by/2.5/legalcode CC 2.5
  * @link       http://www.nucleusdevelopment.com/code/do/zipcode
  *
  **/
  
/**
  * Zipcode Lookup Class
  *
  * @version    Release: 0.9b
  * @link        http://www.nucleusdevelopment.com/code/do/zipcode
  * @ModDate    2011-04-24
  *
  **/
 
// {{{ ZIPCODE
/** 
  * Zipcode Lookup Class
  *
  * Used for:
  * 1) looking up the zip of a long and lat,
  * 2) looking up the disance between two zipcodes, 
  * 3) finding all the zipcodes in the radius of a zipcode.
  * 
  *  
  * Usage example: 
  * <code>
  *
  * // invoke
  * include_once($DIR_LIBS . 'ZIPCODE.php');
  * $zipcode = new ZIPCODE();
  *
  * // Lookup zipcode by long and lat
  * $zip_r = $zipcode->parseDistanceByGeoLoc( 
  *                    $longitude, //  
  *                    $latitude, 
  *                    0.7, // range, default in miles
  *                    1 // limit result, int, (optional)
  *                    'M' // Units, (Optional)
  *                                              );
  *        Units ['M'=>'miles' default, 'K'=>'Kilometers', 'N'=>'Nautical']
  *
  *
  * if(is_array($zip_r))
  *     echo explode(',', $zip_r);
  * else echo $zip_r;
  *
  * // lookup the distance between any two zipcodes
  * $zipcode->readOrgByZIP( 53211 ); // input users originating zip
  * $zipcode->readDestByZip( 11321 ); // input desination
  * $distance = $zipcode->calcDistance();
  *
  * 
  * // lookup all zipcodes within range
  * $array = parseAllDistancesByZip($zip,$radius,$units=null,$limit=null);
  *
  * </code>
  *
  *
  * Associated Database:
  * http://www.nucleusdevelopment.com/downloads/zipcode.sql
  *
  *
  * Version Changes:
  *
  * v0.9b - 2011/04/24
  * release.
  *
  *
  *
  * @author R. Fritz Washabaugh
  * @package ZIPCODE
  * @access public
  **/
 
class ZIPCODE
{
    
    // (objects)
    var $userZip;
    var $destinationZip;
    var $distance;
    
    var $lat1;
    var $long1;
    var $lat2;
    var $long2;
    
    var $city;
    var $state;
    
    
    // (constructor)
    function ZIPCODE() {
            
    }
    
    
    function quickDistanceLookup($origin, $destination, $unit=NULL) {
        $this->readOrgByZIP($origin); // input users originating zip        
        $this->readDestByZip($destination); // input dest zip
        
        return $this->calcDistance($unit); // return distance
    }
    
    
    function readOrgByZip($zip) {
        $this->userZip = $zip;
        return $this->readOrigin("zzip='".addslashes($zip)."'");
    }
    
    
    function readDestByZip($zip) {
        $this->destinationZip = $zip;
        return $this->readDestination("zzip='".addslashes($zip)."'");
    }
 
 
    // (public)
    function calcDistance($unit=null) { 
        
        // default unit is miles    
        $theta = $this->long1 - $this->long2; 
        
        $dist     = sin(deg2rad($this->lat1)) 
                * sin(deg2rad($this->lat2)) 
                + cos(deg2rad($this->lat1)) 
                * cos(deg2rad($this->lat2)) 
                * cos(deg2rad($theta)); 
        
        $dist = acos($dist); 
        $dist = rad2deg($dist); 
        $distance = $dist * 60 * 1.1515;
        
        $unit = strtoupper($unit);
        
        if($unit != 'M' && $unit != null) 
            $distance = $this->convertFromUnit('M',$unit) * $distance;
        
        $distance = round($distance, 1);
        
        return $distance;
    }
 
    
    function getUnit($unit = 'M') {
        if(empty($unit)) 
            $unit = 'M';
            
        switch ($unit) {
            case 'M': 
                $r = _MILES;
                break;
            case 'N':
                $r =  _NAUTICAL_MILES;
                break;
            case 'K':
                $r = _KILOMETERS;
                break;
        }
        return $r;
    }
 
    
    function setUnit($name) {
        $name = strtolower($name);
        switch ($name) {
            case 'miles': 
                $r = 'M';
                break;
            case 'nautical miles':
                $r = 'N';
                break;
            case 'kilometers':
                $r = 'K';
                break;
        }
        return $r;
    }
 
    
    function convertFromUnit($oldUnit,$newUnit) {
        
        $newUnit = strtoupper($newUnit);
        $oldUnit = strtoupper($oldUnit);
        
        switch($oldUnit) {
            case 'M':
                switch($newUnit) {
                    case 'M':
                        $r = 1;
                        break;
                    case 'N':
                        $r = 0.8690;
                        break;
                    case 'K':
                        $r = 1.609;
                        break;
                }
                break;
            case 'N':
                switch($newUnit) {
                    case 'M':
                        $r = 1.151;
                        break;
                    case 'N':
                        $r = 1;
                        break;
                    case 'K':
                        $r = 1.852;
                        break;
                }
                break;
            case 'K':
                switch($newUnit) {
                    case 'M':
                        $r = 0.6214;
                        break;
                    case 'N':
                        $r = .5400;
                        break;
                    case 'K':
                        $r = 1;
                        break;
                }
                break;
        }
        return $r;
    
    }
    
    
    // New very fast query function    
    function parseAllDistancesByZip($zip,$radius,$units=null,$limit=null) {
            
        if(!empty($units) && $units != 'M') 
            $radius = $this->convertFromUnit('M',$units * $radius);
            
        if(!empty($limit))
            $limit = "LIMIT " . $limit;
 
        $query='SELECT * FROM '.sql_table('zipcode'). ' WHERE zzip="'.$zip.'"';
        $result = mysql_query($query);
 
        if(mysql_num_rows($result) > 0) {
            
            $obj = mysql_fetch_object($result);
            
            $query = 'SELECT zzip FROM '. sql_table('zipcode') .
                ' WHERE (POW((69.1*(zlongitude-"' . 
                $obj->zlongitude . '")*cos(' . $obj->zlatitude . 
                '/57.3)),"2")+POW((69.1*(zlatitude-"' . 
                $obj->zlatitude . '")),"2"))<(' . $radius . 
                '*' . $radius . ') ' .  $limit;
                
            $result = mysql_query($query);
            
            if(mysql_num_rows($result) > 0) {
                while($obj = mysql_fetch_object($result)) {
                    $zipArray[] = $obj->zzip;
                }              
            }
        }        
        
        return $zipArray;  
    }
    
    
    function parseDistanceByGeoLoc($long, $lat, $radius, $limit=null, $units=null) {
        
        if( !empty( $units ) && $units != 'M' ) 
            $radius = $this->convertFromUnit( 'M', $units * $radius );
        
        if( !empty( $limit ) )
            $limit = "LIMIT " . $limit;
            
        $query = 'SELECT zzip FROM ' . sql_table( 'zipcode' ) . 
            ' WHERE (POW((69.1*(zlongitude-"' . 
            $long . '")*cos(' . $long . 
            '/57.3)),"2")+POW((69.1*(zlatitude-"' . 
            $lat . '")),"2"))<(' . $radius . 
            '*' . $radius . ') ' . $limit;
            
          $result = mysql_query( $query );
            
        if( mysql_num_rows( $result ) > 1 ) {
             while( $obj = mysql_fetch_object( $result ) ) {
                  $zipArray[] = $obj->zzip;
             }
             
            return $zipArray;              
          }
          
          $obj = mysql_fetch_object($result);
                    
         return $obj->zzip;   
    }
    
    
    // (private)
    function readOrigin($where) {
        // read info
        $query = 'SELECT * FROM ' . sql_table('zipcode') . ' WHERE ' . $where;
        
        $res = sql_query($query);
        $obj = mysql_fetch_object($res);
        
        $this->setOrgLat($obj->zlatitude);
        $this->setOrgLong($obj->zlongitude);
        $this->setOrgCity($obj->zcity);
        $this->setOrgState($obj->zstate);
        
        return mysql_num_rows($res);
    }
 
 
    function readDestination($where) {
        // read info
        $query =  'SELECT * FROM ' . sql_table('zipcode') . ' WHERE ' . $where;
        
        $res = sql_query($query);
        $obj = mysql_fetch_object($res);
        
        $this->setDestCity($obj->zcity);
        $this->setDestState($obj->zstate);
        $this->setDestLat($obj->zlatitude);
        $this->setDestLong($obj->zlongitude);
        $this->setDestTimeZone($obj->ztimezone);    
            
        return mysql_num_rows($res);
    }
 
 
    function setDestCity( $city ) {
        $this->city = $city;
    }
    
    function setDestState($state) {
        $this->state = $state;
    }
    
    function setOrgCity($city) {
        $this->orgCity = $city;
    }
    
    function setOrgState($state) {
        $this->orgState = $state;
    }
 
    
    function setOrgLat($lat) {
        $this->lat1 = $lat;
    }
    
    function setOrgLong($long) {
        $this->long1 = $long;
    }
    
    function setDestLat($lat) {
        $this->lat2 = $lat;
    }
    
    function setDestLong($long) {
        $this->long2 = $long;
    }
    
    function setDestTimeZone($offset) {
        $this->timezone = $offset;
    }
    
    function getDistance() {
        return $this->distance;
    }
    
    
    function getOrgCity() { return $this->orgCity; }
    
    function getOrgState() { return $this->orgState; }
 
}
 
?>
 

URL Buffer

The following is a change log of URL updates:

©2012. All rights reserved.