Tweetegy On the edge of chaos with Ruby, Rails, JavaScript and AngularJS.

| About | Search | Archive | Github | RSS |

How to get a semantic list of all countries and locations in the world

I am in the process of building a semantic web application. Since the application I am building will be used globally, I wanted to start with including a semantic collection of all countries in the world. It turns out to be rather simple thanks to the GeoNames API. I am a big fan of JSON so all the examples I give here will return the results as JSON.

Complete list of all Countries

Get a complete list of all countries is as simple as making the following request (note in all the examples the username is demo. In a real application you would need to create your own account with GeoNames):

Get data for a single Country

If you want data for a single country then you must include the 2 letter ISO_639 code in your request. In the following example, the request returns data for Cambodia (iso_639 code KH):

By the way, you will notice that the GeoNames API contains a rich set of data.

Locations within a Country

GeoNames API has the ability to drill down into various administrative levels (aka populated places) within the country via the GeoNames Children API. To get all the children of a particular country you need to first get the geonameId from GeoNames Country Info API (discussed above already), and pass that as a parameter to the Children API. The following example gets the first level of children for Cambodia (which happens to have a geonameId of 1831722)

To drill down into the next level of children you recursively pass in the geonameId of each child until the API returns an empty result. If you try the GeoNames API with UK data then the first level of children are England, Scotland, Wales etc then if you pass the geonameId of England you will get all the counties of England like Sussex, Kent, Essex, Nottinghamshire etc. Continue to pass in the geonameId until there are no more children and you will have all the locations in the UK (at least according to GeoNames).

I hope you find this useful. In my next post I will discuss how to get language data from dbpedia using their RESTful API or a custom SPARQL query.