Geoname data for country/state/city list generation

 

 

Geonames.org is one of the renowned service. Few days ago i need to make a tool for one of my project. The purpose is as follows:

  • Select a country from a country dropdown.
  • load state/province/Division for that country in a dropdown.
  • load city/location information based on the country and state that selected earlier.

To do so, i search several service and found this service very effective. They provide 50,000 credits for free service. each request though their web service cost 4 credits. Again they provide their whole database for free, so that you can download and install in your database. They update their database every 3/4 months and you can get the update from their website. but its very difficult to find a early solution from their Web service documentation. So i am going to give you a early review what i have done in a simple way.

Step 1: Create a select box with list of countries and country related ISO_Code.  i have use the following country table structure.

   1: CREATE TABLE `countries` (

   2:   `id` int(10) unsigned NOT NULL auto_increment,

   3:   `code` varchar(100) collate latin1_general_ci default NULL,

   4:   `name` varchar(100) collate latin1_general_ci default NULL,

   5:   `printable_name` varchar(100) collate latin1_general_ci default NULL,

   6:   `iso3` char(3) collate latin1_general_ci default NULL,

   7:   `numcode` smallint(6) default NULL,

   8:   PRIMARY KEY  (`id`)

   9: ) ENGINE=MyISAM

You can have the list of countries data from the following link:

Step2: Pass the selected country code(US for USA, BD for Bangladesh) with geoname web service url as a parameter. You will get all the information including “geonameid” for that country. “geonameid”  is the most important thing of geoname service. They provide a unique geoname id to each individual location of the world.

   1:  

   2: $countryCode = $_GET['country_code'];

   3: $webserviceUrl = "http://ws.geonames.org/countryInfo?country={$countryCode}";

   4:  

Step3: Get the information using a Curl from URL.

   1: //call a function getResponse.

   2: $response = getResponse( $webserviceUrl);

   3:  

   4: /**

   5:  * Get response from geonames service

   6:  * 

   7:  * @param   string $url webservice api url with country 

   8:  * @return  XML

   9:  */

  10: function getResponse( $url ) {

  11:     $ch = curl_init();

  12:     curl_setopt($ch, CURLOPT_URL, $url);

  13:     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

  14:     curl_setopt($ch, CURLOPT_GET, true);

  15:     $response = curl_exec($ch);

  16:     curl_close($ch);

  17:     return $response;

  18: }

Step4: prepare the XML response to an array

   1: $countryData = prepareCountryData($response);

   2:  

   3:  

   4: function prepareCountryData( $response ) {

   5:     $root = simplexml_load_string($response);

   6:     $data = get_object_vars($root);

   7:     if ( $data  ) {

   8:         foreach($data as $key => $val ){

   9:             $temp = get_object_vars($val);

  10:         }

  11:         return $temp;

  12:     }

  13:     return false;

  14: }

step5: Get the Country related geonameid and get the state/providence list with that id.

   1: $geoId = $countryData['geonameId'];

   2: $stateList = getStateList($geoId);

   3:  

   4: function getStateList( $geonameId ) {

   5:     $url = "http://ws.geonames.org/children?geonameId=" . $geonameId;

   6:     $response = getResponse( $url );

   7:     $listData = prepareStateList( $response );

   8:     return $listData;

   9: }

  10:  

  11: function prepareStateList( $response ) {

  12:     $root = simplexml_load_string($response);

  13:     $data = get_object_vars($root);

  14:     if ( $data['geoname']  ) {

  15:         foreach($data['geoname'] as $key => $val ){

  16:             $obj = get_object_vars($val);

  17:             $temp[$obj['geonameId']] = $obj['name'];

  18:         }

  19:         return $temp;

  20:     }

  21:     return false;

  22: }

Step6: Now you have all the state list of that particular country. if you look at the list, you will see again we have the state list with geonamid.  Now if you send the geonameid to http://ws.geonames.org/children?geonameId=1337175 , you will get all the cities for that state/province. You can easily make a Autocomple select box with jQuery Autocomple plugin.

Hope you like this and it works for you. thanks.

Reference:  Geoname Website(http://www.geonames.org)

Next article: How to create your own REST based webservice using geoname data from your own database.



1 Comment »

  1. abcphp.com says:

    Geoname data for country/state/city list generation…

    Geonames.org is one of the renowned service. Few days ago i need to make a tool for one of my project. The purpose is as follows:
    - Select a country from a country dropdown.
    - load state/province/Division for that country in a dropdown.
    - load city/l…

RSS feed for comments on this post. TrackBack URL

Leave a comment

viagra patent expire Viagra Sale viagra anxiety