R-Forge Logo

Welcome to geonames project!

An R interface to the geographic query web services described at http://www.geonames.org/


With the geonames package you can get easy access to the geographic names query service on GeoNames.


All you need is a modern R and the rjson package, which is available from CRAN. You also need a network connection, and you need to make sure R can access web sites. Local network policy and firewalls may affect this.


Functions defined here closely follow the names and parameters of WebServices defined in GeoNames' WebServices Overview. The aim is to implement all JSON-enabled web services defined there.

Select the web service you want to call, and check its documentation. To translate into an R geonames package function, just prefix 'GN' to the name, remove the 'JSON' part, and put named parameters in the function call.

For example, let's find the top-level administrative areas of Italy. First you need the geoname id code for Italy. The Web Service Overview gives us the countryInfo service. The description of that service is given as:

Country Info (Bounding Box, Capital, Area in square km, Population)
Webservice Type : REST
Url : ws.geonames.org/countryInfo?
Parameters : country (default = all countries)
lang : ISO-639-1 language code (en,de,fr,it,es,...) (default = english)
Result : Country information : Capital, Population, Area in square km, Bounding Box of mainland
Example : http://ws.geonames.org/countryInfo?

We can then call this from R with GNcountryInfo(country="IT"). Note that the function name has GN prefixed to the web service name, and that the parameter is named the same as the parameter description above.

> GNcountryInfo(country="IT")
  countryName bBoxWest currencyCode fipsCode countryCode isoNumeric capital areaInSqKm            languages
1       Italy 6.614888          EUR       IT          IT        380    Rome   301230.0 it-IT,de-IT,fr-IT,sl
  bBoxEast isoAlpha3 continent bBoxNorth geonameId bBoxSouth population
1 18.51345       ITA        EU   47.0952   3175395  36.65277   58145000

This gives us the geonameId of 3175395 for Italy. We want to find the administrative areas, and this is done with the children web service:


Returns the children for a given geonameId. The children are the
administrative divisions within an other administrative division. Like
the counties (ADM2) in a state (ADM1) or also the countries in a

Webservice Type : XML or JSON
Url : ws.geonames.org/children?
Parameters :
geonameId : the geonameId of the parent
Result : returns a list of GeoName records

Example, regions of Italy:

So in R this becomes itadm1=GNchildren(geonameId=3175395). Now we can investigate the returned value:

> itadm1$name
 [1] "Abruzzo"                "Aosta Valley"           "Apulia"                 "Basilicate"            
 [5] "Calabria"               "Campania"               "Friuli"                 "Latium"                
 [9] "Liguria"                "Lombardy"               "Piedmont"               "Regione Emilia-Romagna"
[13] "Regione Molise"         "Sardinia"               "Sicily"                 "The Marches"           
[17] "Trentino-Alto Adige"    "Tuscany"                "Umbria"                 "Veneto"                

We can also get these names in Italian, by adding lang="IT" to the call. Note that this isn't documented on the geonames website - I tried it and it worked.

> GNchildren(geonameId=3175396,lang="IT")$name
 [1] "Regione Abruzzo"                        "Regione Autonoma Friuli Venezia Giulia"
 [3] "Regione Autonoma Siciliana"             "Regione Autonoma Trentino-Alto Adige"  
 [5] "Regione Autonoma Valle d'Aosta"         "Regione Autonoma della Sardegna"       
 [7] "Regione Basilicata"                     "Regione Calabria"                      
 [9] "Regione Campania"                       "Regione Emilia-Romagna"                
[11] "Regione Lazio"                          "Regione Liguria"                       
[13] "Regione Lombardia"                      "Regione Marche"                        
[15] "Regione Molise"                         "Regione Piemonte"                      
[17] "Regione Puglia"                         "Regione Umbria"                        
[19] "Regione del Veneto"                     "Tuscany"             


Parameter Names

Note that for the simplest functions there are defaults and named formal parameters. For more complex web service calls the function just has a ... parameter and the user has to supply a set of name=value pairs that make sense for the web service. I recommend you always use named parameters when you call these functions, even when calling something like GNtimezone which is defined with names so you can at the moment do GNtimezone(54,23). I might get rid of this and enforce calls to be of the form GNtimezone(lat=54,lng=23) in the future.

Further examples

The following examples are included in the file testing.R in the tests subdirectory. These tests are not run during install or check.

> file.show(file.path(system.file(package="geonames"),"tests","testing.R"))














Returned Values

The functions generally return data frames with a row for each entry and columns named as in the web service specification. Sometimes each returned value in a query has a different set of properties, and in this case the data frame will have all the property names for columns, but NA values in the inappropriate entries.


If the web service call fails then an error message is returned and reported. The text for these messages is coded into the package. If the Geonames people change or extend these codes then you will probably be better off looking them up on their web site.

Sometimes the geonames people will change the hostname. This can be changed in the package with the R "options" function:


The project summary page you can find here.