Over the last year and half, I have faced numerous challenges with geocoding the data that I have used to showcase my passion for location analytics. In 2012, I decided to take thing in my control and turned to R. Here, I am sharing a simple R script that I wrote to geo-code my data whenever I needed it, even BIG Data.
To geocode my data, I use Google's Geocoding service which returns the geocoded data in a JSON. I will recommend that you register with Google Maps API and get a key if you have large amount of data and would do repeated geo coding.
Here is function that can be called repeatedly by other functions:
getGeoCode <- function(gcStr)
{
library("RJSONIO") #Load Library
gcStr <- gsub(' ','%20',gcStr) #Encode URL Parameters
#Open Connection
connectStr <- paste('http://maps.google.com/maps/api/geocode/json?sensor=false&address=',gcStr, sep="")
con <- url(connectStr)
data.json <- fromJSON(paste(readLines(con), collapse=""))
close(con)
#Flatten the received JSON
data.json <- unlist(data.json)
lat <- data.json["results.geometry.location.lat"]
lng <- data.json["results.geometry.location.lng"]
gcodes <- c(lat, lng)
names(gcodes) <- c("Lat", "Lng")
return (gcodes)
}
Let's put this function to test:
geoCodes <- getGeoCode("Palo Alto,California")
Lat Lng
"37.4418834" "-122.1430195"
Here is my sample data frame with three columns - Opposition, Ground.Country and Toss. Two of the columns, you guessed it right, need geocoding.
> head(shortDS,10)
Opposition Ground.Country Toss
1 Pakistan Karachi,Pakistan won
2 Pakistan Faisalabad,Pakistan lost
3 Pakistan Lahore,Pakistan won
4 Pakistan Sialkot,Pakistan lost
5 New Zealand Christchurch,New Zealand lost
6 New Zealand Napier,New Zealand won
7 New Zealand Auckland,New Zealand won
8 England Lord's,England won
9 England Manchester,England lost
10 England The Oval,England won
To geo code this, here is a simple one liner I execute:
> head(shortDS, 10)
Opposition Ground.Country Toss Ground.Lat Ground.Lng
1 Pakistan Karachi,Pakistan won 24.893379 67.028061
2 Pakistan Faisalabad,Pakistan lost 31.408951 73.083458
3 Pakistan Lahore,Pakistan won 31.54505 74.340683
4 Pakistan Sialkot,Pakistan lost 32.4972222 74.5361111
5 New Zealand Christchurch,New Zealand lost -43.5320544 172.6362254
6 New Zealand Napier,New Zealand won -39.4928444 176.9120178
7 New Zealand Auckland,New Zealand won -36.8484597 174.7633315
8 England Lord's,England won 51.5294 -0.1727
9 England Manchester,England lost 53.479251 -2.247926
10 England The Oval,England won 51.369037 -2.378269
Happy Coding!
shortDS <- with(shortDS, data.frame(Opposition, Ground.Country, Toss,
laply(Ground.Country, function(val){getGeoCode(val)} )))
> head(shortDS, 10)
Opposition Ground.Country Toss Ground.Lat Ground.Lng
1 Pakistan Karachi,Pakistan won 24.893379 67.028061
2 Pakistan Faisalabad,Pakistan lost 31.408951 73.083458
3 Pakistan Lahore,Pakistan won 31.54505 74.340683
4 Pakistan Sialkot,Pakistan lost 32.4972222 74.5361111
5 New Zealand Christchurch,New Zealand lost -43.5320544 172.6362254
6 New Zealand Napier,New Zealand won -39.4928444 176.9120178
7 New Zealand Auckland,New Zealand won -36.8484597 174.7633315
8 England Lord's,England won 51.5294 -0.1727
9 England Manchester,England lost 53.479251 -2.247926
10 England The Oval,England won 51.369037 -2.378269
Thanks for this great tool. Any idea why it returns a bunch of (but not all) NAs?
ReplyDeletegoogle only allows 10 requests per second.
DeleteGreat Article Artificial Intelligence Projects
DeleteProject Center in Chennai
JavaScript Training in Chennai
JavaScript Training in Chennai Project Centers in Chennai
Hi Dave, Where can I specify the API key?
ReplyDeleteCan anyone here please explain about geocode and the use of it?
ReplyDeleteHP Printer Technical Support Number
ReplyDeleteHP Printer Technical Support Phone Number
HP Printer Technical Support
HP Tech Support
Hp printer firmware error
Hp printer firmware error
How to fix hp printer paper jam issue
how to fix hp printer paper jam issue
troubleshoot hp printer error code 79
troubleshoot hp printer error code 79
hp printer error code e2
hp printer error code e2
HP Printer Support
canon Printer Support Number USA
ReplyDeletecanon Printer Support
canon Printer Support Phone Number
canon Printer Support Number
canon Printer Tech Support Phone Number
canon Printer Customer Support Number
canon Printer Customer Service Number
canon Printer Tech Support Phone Number
canon Printer Technical Support Phone Number
HP Printer Support Phone Number
ReplyDeleteBrother Printer Support Phone Number
Brother Printer Support Phone Number
Lexmark Printer Support Phone Number
Lexmark Printer Support Phone Number
HP Printer Support phone Number
HP Printer Support Phone Number
Epson printer Support Phone Number
Epson Printer Support Phone Number
HP Printer Installation Help
HP Printer Installation help
HOme PAGE
ReplyDeleteEpson Printer Support Phone Number
Epson Printer Technical Support Number
Epson Printer Technical Support Number
epson printer drivers download
epson printer drivers download
epson printer prints blank pages
epson printer prints blank pages
epson printer error code 0x800706b9
epson printer error code 0x800706b9
Canon
Canon Printer Support Phone Number
Canon Priner Technical Support Number
Canon Priner Technical Support Phone Number
Canon Printer Customer Support Number
Canon Printer Customer Support Number
Epson Printer Helpline Number
Epson Printer Helpline Number
Canon
ReplyDeleteCanon Printer Support
Canon Printer Support Number
Canon Printer Support Number USA
canon printer tech support phone number
Canon Printer technical Support Phone Number
Canon Printer customer Support Number
Canon Printer customer service number
HP
HP Printer Support
HP Printer Support Phone Number USA
HP Printer tech Support Number
HP Printer tech support phone Number
HP Printer technical support
HP Printer Number
HP Printer technical support Number
HP printer helpline number
EPSON
Epson Printer Support Number
Epson Printer Support
Epson Printer Support Phone Number
Epson Printer Support Phone Number USA
hp Printer
ReplyDeletehp printer Support
hp Support
brother printer
brother printer Support
brother Support
canon printer
canon printer Support
canon Support
epson
epson printer Support
epson Support
Dell Printer
dell printer Support
dell Support
Lexmark Printer
Lexmark Support
Lexmark printer Support
Epson Printer Support Phone Number
ReplyDeleteEpson Printer Support Number
Epson Printer Support
Epson Printer Technical Support Number
Epson Printer Technical Support Phone Number
Epson Printer Tech Support Number
Epson Printer Helpline Number
Epson Printer Customer Helpline Number
Epson Printer Customer Care Number
epson workforce wf 3530 support number/
HP Printer Support
ReplyDeleteHP Printer Support Phone Number
HP Printer Support Telephone Number
HP Printer Support Contact Number
HP Printer Support Toll Free Number
HP Printer Support Online
HP Printer Support Wireless
HP Printer Support Telephone
HP Printer Support Help
HP Printer Support Helpline
HP Printer Support Number usa
HP Printer Support Line
HP Printer Support Customer Service Number
HP Printer Support usa
----------------------------------------------------------------
Canon Printer Support Phone Number
Canon Printer Support Number
Canon printer technical Support
Canon Printer Support usa
Canon Printer Support Help
Canon Printer Support Online
Canon printer Support Telephone Number
Canon printer Customer Support Phone Number
--------------------------------------------------------------------
Linksys Support
Linksys Support Number
Linksys Customer Support Number
Linksys Support site
Linksys Support Phone
Linksys Support Chat
Linksys Support Line
Linksys Support Online
Linksys Support website
Linksys Support Number usa
--------------------------------------------------------------------
Pogo Game not loading
Pogo Games Phone Number
Pogo Game Customer toll free Number
Pogo Game won't load
Pogo Game Customer care Number
Pogo Games contact us
Pogo Games Helpline
Pogo Games java problems
Pogo Game Support
Pogo Games Support Phone Number
Pogo Game Support Number
Pogo Games Customer Support
Pogo Game tech Support Phone Number
Pogo Games technical Support
Pogo Games technical Support Phone Number
Delta Skymiles Phone Number For Reservations
ReplyDeleteDelta Airlines Customer Service Number
Cheap Flight Tickets Websites
Jetblue Reservations Phone Number
Delta Airlines Phone Number
Jetblue Airlines Phone Number
United Airlines Reservation Phone Number
United Airlines Phone Number
Delta Reservations Phone Number
Linksys Support
ReplyDeleteLinksys Support Number
Linksys Customer Support Number
Linksys Support site
Linksys Support Phone
Linksys Support Chat
Linksys Support Line
Linksys Support Online
Linksys Support website
Linksys Support Number usa
Linksys Router Support Phone Number
Delta is an American airline major company, headquarters in Atlanta, Georgia and operates over 5400 flights every day. Delta operates with domestic and international flights in 52 countries include 325 destinations. Travelers may choose different cabin option as per their needs and capabilities like Delta One, Premium Select, First Class, Delta Comfort+, Main Cabin, and Basic Economy. Delta Airlines Reservation Phone Number will help you to plan your vacation and trips. You may dial Delta Airlines Phone Number to know your flight status, changes in your flight, upgrade your seats. A team of travel advisor is ready to listen to you and make your fly easy.
ReplyDeleteTechnoTools: Front-end web developers are responsible for how the website looks. They create the layout of the site and integrate graphics, applications (such as retail checkout tools) and other content. They also write web site programs in various types of computer languages, such as HTML or Javascript.
ReplyDeleteHP Printer Support can offer a live professional who can help in a wide range of printer related issues.
ReplyDeleteHP Printer Support Phone Number
HP Printer Support Phone Number now not only allows to work for ride customers, however also enhance their work for fresher, who are no longer able to begin the printer for the first time they buy. As it is said above that the entirety needs to be updated, so is the HP Printer. People normally seem to be for putting in HP Printer. Because it gives an updates reminder of the printer. The consumer can deploy HP printer Support by using getting assist from our helpline wide variety.
ReplyDeleteWithin no time you will get the response from our experts with whom you can discuss the issues to get the solution. Our experts will provide you the user-friendly support on the chat to resolve the issues effectively from its main cause. Problems like installation issues, drivers update, and all other issues can be resolved through the Epson Printer Technical Support Number.
ReplyDeleteWe are giving HP Printer Helpline Number
ReplyDeletea Japan affiliation advancement, has contributed an imperative action by moving best printers over the world. Individuals purchase printers for their position and moreover non-official use. In the event that regardless the strong Printer Support client faces issue while utilizing the printer, similar to the words or the letters are printer lacking or isn't unmistakably conspicuous, and for all intents and purposes indistinguishable issues show up, for this our HP Printer Helpline Number is open without crash and burn
This comment has been removed by the author.
ReplyDelete
ReplyDeleteepson Printer Support
epson Printer Support Number
epson Printer Toll Free Number
epson Printer Support Phone Number
epons printer customer support number
epson printer customer helpline number
epson printer customer care number
epson Printer Technical Support Number
epson Printer Technical Support Phone Number
epson Printer Technical Support
epson Printer Tech Support Number
epson Printer Tech Support
epson Printer Helpline Number
epson Printer Toll Free Number
epson Printer Contact Number
Epson Printer Toll Free Number 1800-436-0509
ReplyDeleteEpson printer drivers download
Informative article. Thanks for sharing such an valuable article.this is best article .
ReplyDeleteCanon Printer Technical Support Number
Canon Printer Support Phone Number
Canon Printer Phone Number
Here for how to connect Epson wireless printer to Wifi? Solution for wireless printer on your WLAN network call Epson support number +1-800-436-0509 toll free, Epson Wireless Printer Setup
ReplyDeleteHow to connect Epson wireless printer to Wifi
Really helpfull geocoding data via json.First copy luxury watrches online
ReplyDelete