Maps, Geocoding, and the R User Conference 2010

July 14, 2010
By

(This article was first published on R-Chart, and kindly contributed to R-bloggers)



The R User Conference 2010 is scheduled for July 20-23, 2010.  Wanna know where?

Although there are more sophisticated methods of mapping with R the maps package makes mapping activities straightforward.  A bit of XML and Google Geocoding is enough to highlight the location of this years conference (or any other location of interest to you).  If you prefer to see this in living color - you can view it at github.


getDocNodeVal=function(doc, path)
{
   sapply(getNodeSet(doc, path), function(el) xmlValue(el))
}


gGeoCode=function(str)
{
  library(XML)
  u=paste('http://maps.google.com/maps/api/geocode/xml?sensor=false&address=',str)
  doc = xmlTreeParse(u, useInternal=TRUE)
  str=gsub(' ','%20',str)
  lng=getDocNodeVal(doc, "/GeocodeResponse/result/geometry/location/lat")
  lat=getDocNodeVal(doc, "/GeocodeResponse/result/geometry/location/lng")
  c(lat,lng)
}


bullseyeEtc=function(str)
{
  for (i in 1:10){points(loc[1],loc[2],col=heat.colors(10)[i],cex=i)}
  for (i in 1:10){points(loc[1],loc[2],col=heat.colors(10)[i],cex=i*.5)}
  title(main='The R User Conference 2010', sub='July 20-23, 2010')
  mtext("NIST: Gaithersburg, Maryland, USA")
  mtext("http://user2010.org",4)
}


loc=gGeoCode('100 Bureau Drive Gaithersburg, MD, 20899, USA')


# World
map('world', plot=TRUE, fill=TRUE);bullseyeEtc()


# USA
map('usa', plot=TRUE, fill=TRUE);bullseyeEtc()




# State
map('state','Maryland', plot=TRUE, fill=TRUE);bullseyeEtc()

UPDATE:  Thanks for the feedback in the comments below - there are a number of improvements suggested:
1)  Use URL Encode rather than just substituting out spaces.
2)  I ran this code without any problem on two machines - using R version 2.10.1 (2009-12-14) for Windows and XML version 2.8-1.  Other folks ran into problems with URL escape characters - but were able to replicate the functionality using the JSON package.  For more information...see the post at stackoverflow.

To leave a comment for the author, please follow the link and comment on his blog: R-Chart.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Tags: , ,

Comments are closed.