Advertisement
Guest User

Untitled

a guest
Apr 20th, 2014
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. geocode <- mapreduce(input = "/apps/hive/warehouse/addr.db/addr/",input.format="pig.hive",
  2. output = "/output/georesult", output.format = "pig.hive",
  3. map = function(k,v){
  4.  
  5. require("RCurl")
  6. require("RJSONIO")
  7.  
  8. v = as.matrix(v)
  9. construct.geocode.url <- function(address, return.call = "json", sensor = "false") {
  10. root <- "http://www.datasciencetoolkit.org/maps/api/geocode/"
  11. u <- paste(root, return.call, "?address=", address, "&sensor=", sensor, sep = "")
  12. return(URLencode(u))
  13. }
  14.  
  15. # Goes onto web and gets latitude and longitude
  16. gGeoCode <- function(address,verbose=FALSE) {
  17. georesult = matrix(NA,nrow(address),3)
  18. georesult[,1]=address
  19. for (i in 1:nrow(address)){
  20. if(verbose) cat(address[i,],"n")
  21. u <- construct.geocode.url(address[i,])
  22. doc <- getURL(u)
  23. x <- fromJSON(doc,simplify = FALSE)
  24. if(x$status=="OK") {
  25. georesult[i,2] <- x$results[[1]]$geometry$location$lat
  26. georesult[i,3] <- x$results[[1]]$geometry$location$lng
  27.  
  28. # return(c(address[i],lat[i], lng[i]))
  29. }
  30. else {
  31. georesult[i,2] <- NA
  32. georesult[i,3] <- NA
  33.  
  34. # return(c(address[i],NA,NA))
  35. }
  36. }
  37. return(georesult)
  38. }
  39.  
  40. l = gGeoCode(v)
  41.  
  42. keyval(k,l)
  43.  
  44. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement