Showing posts with label Maps. Show all posts
Showing posts with label Maps. Show all posts

Tuesday, January 24, 2012

Geocode your data using, R, JSON and Google Maps' Geocoding API

First and foremost, I absolutely love the topic of Location Analytics (Geo-Spatial Analysis) and see tremendous business potential in not so distant future.  I would go out on a limb to predict that the Location Analytics will soon go viral in the enterprise space because it has the capability to WOW us. Look no further than your iPhone or an Android phone and count how many location aware apps you have. We all have at lease one app - Google Maps.  Mobile is one of the strongest catalyst for enterprise adoption of Location aware apps. All right, enough of business talk, let's get dirty with the code.


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")

> geoCodes
           Lat            Lng 
  "37.4418834" "-122.1430195" 


You can run this on the entire column of a data frame or a data table:

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:

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



Happy Demoing and Coding!

Thursday, July 28, 2011

Google Maps - SAP - The press is humming...

Thanks to my friends in marketing...


SAP Now Allows Businesses To Layer Big Data With Google Maps And Earth

Leena Rao, TechCrunch
July 27, 2011
Also seen in: The Washington Post, The Wall Street Journal, NewsFactor, The Business Insider, 21st Century Networker, Maine Social Networking, Data Centers Canada

(385 tweets)


SAP Tying Analytics to Google Maps, Earth

Chris Kanaracus, IDG News
July 27, 2011
Also seen in: PCWorld, CIO, Network World, Wall Street Journal, Computerworld, PC Advisor, InfoWorld, Albuquerque Express, CFO World, ARNnet, ITworld, Good Gear Guide, Techworld, Computerworld Australia, Computerworld UK, ITBusiness.ca
(114 tweets)

Google Deal Lets SAP Customers Map Data
Doug Henschen, InformationWeek
July 27, 2011
(28 tweets)

 

Google and SAP Team-Up to Help You Visualize Big Data

Klint Finley, ReadWriteWeb
July 27, 2011
(128 tweets)

SAP, Google expand collaboration partnership, eye enterprise mashups
Larry Dignan, ZDNet
July 27, 2011
(39 tweets)

SAP and Google Maps Team Up to Visualize Corporate Data

Todd R. Weiss, CIO blog: Deciphering Enterprise Apps

July 27, 2011

(3 tweets)

 

SAP Geo-tagging Business Analytics Data with Google Maps

Clint Boulton, eWEEK

July 27, 2011

http://www.eweek.com/c/a/Enterprise-Applications/SAP-Geotagging-Business-Analytics-Data-with-Google-Maps-274375/

Also seen in: eWEEK Europe
(19 tweets)

SAP Invokes Google Maps to Put Data in Context

Mike Vizard, IT Business Edge

July 27, 2011

(2 tweets)

 

Google Plus SAP Deepen Ties, Deliver More Enterprise Integration

Courtney Bjorlin, ASUG News

July 27, 2011

(14 tweets)

Photos: Google Maps gives SAP's business intelligence a new direction
Tim Ferguson, Silicon.com
July 27, 2011
(6 tweets)

SAP and Google Put Business Analytics on the Map

Justin Kern, Information Management

July 27, 2011

http://www.information-management.com/news/business_intelligence_analytics_data_geospatial-10020840-1.html

Also seen in: Insurance Networking News
(10 tweets)

SAP and Google Partner for Geospatial Business Intelligence
Paul Shread, EnterpriseAppsToday
July 27, 2011
(7 tweets)

SAP, Google and geospatial analytics
Todd Morrison, SearchSAP
July 27, 2011
(3 tweets)

 

SAP and Google team up to mash up analytics and maps
Matt Hartley, Financial Post
July 27, 2011

http://business.financialpost.com/2011/07/27/sap-and-google-team-up-to-mash-up-analytics-and-maps/

(4 tweets)

 

The “Where” Dimension of Business Intelligence

Adrian Gonzalez, Logistics Viewpoints
July 27, 2011
(15 tweets)

SAP, Google team up to tie-in business analytics to Google Map

Brad Lemaire, Proactive Investors USA & Canada
July 27, 2011
(4 tweets)

Google and SAP Partner on Geo-Mapping

Chris Crum, WebProNews
July 27, 2011
(23 tweets)

Google and SAP Team Up for Geovisualization of Business Analytics

Matt Ball, Spatial Sustain
July 27, 2011
(5 tweets)

SAP Partnership With Google Allows Data Mapping on Google Maps
TechGadgetsWeb.com
July 27, 2011
(5 tweets)

Street Fight Daily: 07.27.11
David Hirschman, Street Fight Daily: Inside the Business of Hyperlocal
July 27, 2011
(0 tweets)

Media and Blogger Coverage – Headlines & Links Select Global Coverage

EMEA
Germany
Geo-Dienste: SAP verbündet sich mit Google
Sibylle Gassner, Silicon.de
July 27, 2011
(4 tweets)

SAP und Google wollen gemeinsam Geodaten sammeln
Jens Hartmann, Silicon.de
July 27, 2011
(10 tweets)

SAP und Google rücken zusammen
Corrina Visser, Der Tagesspiegel
July 27, 2011
(1 tweet)

Netherlands
SAP, Google team up to pair enterprise apps with LBS tools
Telecompaper
July 27, 2011                 
(2 tweets)

Switzerland
Big Data: SAP married with Google Maps and Google Earth/ Big Data: SAP verheiratet sich mit Google Maps und Google Eart
Inside-it.ch
July 27, 2011
(0 tweets)

UK

SAP's Google partnership adds mapping data to business intelligence

Phil Muncaster, V3.co.uk
July 27, 2011
(0 tweets)

SAP and Google plan ‘big data’ Maps
Tom Brewster, ITPro
July 27, 2011
(13 tweets)

SAP & Google Partner For Enhanced Location-Based Analytics
ITProPortal
July 27, 2011
(5 tweets)

SAP sees double-digit growth in software sales

Computing.co.uk
July 27, 2011
(1 tweets)

APJ 
India
SAP teams-up with Google to manage big data
CIOL India
July 27, 2011
(0 tweets)