How to get the longitude and latitude coordinates from a city name and country in R?
Asked Answered
C

3

16

I have a long list of city names and countries and I would like to plot them on a map. In order to do this I need the longitude and latitude information of each of the cities.

My table is called test and has the following structure:

Cityname  CountryCode
New York  US
Hamburg   DE
Amsterdam NL
Capriole answered 16/12, 2012 at 20:16 Comment(0)
C
18

With the following code I have successfully solved the problem.

library(RJSONIO)
nrow <- nrow(test)
counter <- 1
test$lon[counter] <- 0
test$lat[counter] <- 0
while (counter <= nrow){
  CityName <- gsub(' ','%20',test$CityLong[counter]) #remove space for URLs
  CountryCode <- test$Country[counter]
  url <- paste(
    "http://nominatim.openstreetmap.org/search?city="
    , CityName
    , "&countrycodes="
    , CountryCode
    , "&limit=9&format=json"
    , sep="")
  x <- fromJSON(url)
  if(is.vector(x)){
    test$lon[counter] <- x[[1]]$lon
    test$lat[counter] <- x[[1]]$lat    
  }
  counter <- counter + 1
}

As this is calling an external service (openstreetmaps.org) it can take a while for larger datasets. However, you probably only do this once in a while when new cities have been added to the list.

Capriole answered 16/12, 2012 at 20:16 Comment(2)
You can also do this with GNsearch from my geonames package - it calls the geonames.org web service which shares a lot of its data with OpenStreetMap's Nominatim service.Deliberative
@Capriole How do i do the reverse of this? I have latitude and longitude and I need to find city and country names using open street through json format like you did above?Perlite
L
16

A few other options for you.

ggmaps

ggmaps has a function geocode which uses Google Maps to geocode. This limits you to 2,500 per day.

taRifx.geo

taRifx.geo's latest version has a geocode function which uses either Google or Bing Maps to geocode. The Bing version requires you to use a (free) Bing account, but in return you can geocode way more entries. Features in this version:

  • Service choice (Bing and Google Maps both supported)
  • Log-in support (particularly for Bing, which requires an account key but in exchange allows for an order of magnitude more daily requests)
  • Geocode a whole data.frame at a time, including some time-savers like ignoring any rows which have already been geocoded
  • Robust batch geocoding (so that any error does not cause the whole data.frame's worth of geocoding to be lost, for bigger jobs)
  • Route finding (travel times from point A to point B)
Leila answered 16/12, 2012 at 20:34 Comment(0)
D
10

Try this I think it will better solution for this problem

> library(ggmap) 
Loading required package: ggplot2
Google Maps API Terms of Service: http://developers.google.com/maps/terms.
Please cite ggmap if you use it: see citation('ggmap') for details.

#Now you can give city name or country name individually

> geocode("hamburg")
Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=hamburg&sensor=false
       lon      lat
1 9.993682 53.55108

geocode("amsterdam")
Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=amsterdam&sensor=false
       lon      lat
1 4.895168 52.37022

> geocode("new york")
Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=new+york&sensor=false
        lon      lat
1 -74.00594 40.71278
Death answered 25/3, 2015 at 9:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.