Location: PHPKode > scripts > Geocoder > geocoder/WorldGeocoder.inc
<?php
/**
 * Worldkit Geocoder for World Cities
 *
 * @author Jan Bruyndonckx
 * @copyright Copyright (c) 2008 Banzora   http://software.banzora.com
 * @package main
 * @subpackage lib
 *
*/
require_once "Geocoder.inc";

/**
		Class to geocode a Worldwide location.
		The location is specified using the zip code, the cityname and the state
		We use the Worldkit geocoder described at:
		http://brainoff.com/geocoder/

        Please specify 'city', [ ex. http://brainoff.com/geocoder/rest?city=Brighton,UK ]
        For US cities, include state [ ex. http://brainoff.com/geocoder/rest?city=Santa+Cruz,CA,US ]
        For US zipcodes, specify 'zip', [ ex. http://brainoff.com/geocoder/rest?zip=94114 ]
        For UK postcodes, specify 3/4 digit 'post', [ ex. http://brainoff.com/geocoder/rest?post=SW11 ]

        Country code definitions in [ http://brainoff.com/geocoder/countryselect.php ]
        3 letter ISO codes also accepted, definitions in [ http://brainoff.com/geocoder/iso2 ]
	*/

class WorldGeocoder extends Geocoder
{

    static public function geoCode ($country, $zip, $city) {

        $geo = new WorldGeocoder ($country, $zip, $city) ;
        $result = $geo->submit() ;
        if (isset($result['error']) &&
        ($country == 'US')) { // often we encode the state in the city
            $geo = new WorldGeocoder ('US', NULL, $city) ;
            $result = $geo->submit() ;
        }
        return $result ;
    }

    const SERVICE_URL = "http://brainoff.com/geocoder/rest/?" ;

    protected function createRequest () {
        $url = WorldGeocoder::SERVICE_URL ;

        if ($this->country == 'US') {
            // if we know the zip code, then use it, else use city/state
            if ($this->zipcode != NULL) {
                // only first 5 char zipcodes are used
                $url .= "zip=" . substr($this->zipcode, 0, 5) ;
            } elseif (($this->city != NULL) && ($this->state != NULL)) {
                $url .= "city=" . $this->city . "," . $this->state .",US" ;
            } else {
                return NULL ;
            }
        } else if ($this->country == 'CA') {
            if (strpos ($this->city, '%2C') !== false) {  // %2C is ',' urlencoded...
                $p = explode ('%2C', $this->city) ;   // remove state from city
                $this->city = $p[0] ;
            }
            $url .= "city=" . $this->city . "," . $this->country ;
        }
        else {
            if (($this->city == NULL) || ($this->country == NULL)) {
                return NULL ;
            }
            $url .= "city=" . $this->city . "," . $this->country ;
        }
        //echo("url=$url<br>");
        return $url ;
    }

    /**--------------------------------------------------------------------
    Parses a result that looks like:
    <rdf:RDF>
    <geo:Point>
    <geo:long>-0.15</geo:long>
    <geo:lat>50.8333333</geo:lat>
    </geo:Point>
    </rdf:RDF>
    */
    public function parseResult ($result)
    {
        //echo htmlentities($result) ;
        //echo "<br><BR>" ;

        $result = trim ($result) ;

        // string must start with a '<' in order to be valid xml
        if (stripos ($result, 'Dang') !== false) {
            return $this->createResult (NULL, NULL, "no_geocode") ;
        }
        // couldn't geocode this
        if (stripos ($result, "Please specify 'city'") !== false) {
            return $this->createResult (NULL, NULL, "no_geocode") ;
        }

        // be lazy: replace namespace by '_'
        $result = str_replace (":", "_", $result) ;

        $dom = new DOMDocument() ;
        $dom->preserveWhiteSpace = false ;
        $result = @$dom->loadXML ($result) ;

        if ($result == false)
        {
            return $this->createResult (NULL, NULL, "no_geocode") ;
        }

        $latitudelist = $dom->getElementsByTagName ("geo_lat") ;
        if ($latitudelist->length != 1)
        {
            return $this->createResult (NULL, NULL, "no_geocode") ;
        }
        $longitudelist = $dom->getElementsByTagName ("geo_long") ;
        if ($longitudelist->length != 1)
        {
            return $this->createResult (NULL, NULL, "no_geocode") ;
        }

        return $this->createResult ($longitudelist->item(0)->textContent,
        $latitudelist->item(0)->textContent) ;
    }

}
// end class WorldGeocoder

?>
Return current item: Geocoder