Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- geocode <- mapreduce(input = "/apps/hive/warehouse/addr.db/addr/",input.format="pig.hive",
- output = "/output/georesult", output.format = "pig.hive",
- map = function(k,v){
- require("RCurl")
- require("RJSONIO")
- v = as.matrix(v)
- construct.geocode.url <- function(address, return.call = "json", sensor = "false") {
- root <- "http://www.datasciencetoolkit.org/maps/api/geocode/"
- u <- paste(root, return.call, "?address=", address, "&sensor=", sensor, sep = "")
- return(URLencode(u))
- }
- # Goes onto web and gets latitude and longitude
- gGeoCode <- function(address,verbose=FALSE) {
- georesult = matrix(NA,nrow(address),3)
- georesult[,1]=address
- for (i in 1:nrow(address)){
- if(verbose) cat(address[i,],"n")
- u <- construct.geocode.url(address[i,])
- doc <- getURL(u)
- x <- fromJSON(doc,simplify = FALSE)
- if(x$status=="OK") {
- georesult[i,2] <- x$results[[1]]$geometry$location$lat
- georesult[i,3] <- x$results[[1]]$geometry$location$lng
- # return(c(address[i],lat[i], lng[i]))
- }
- else {
- georesult[i,2] <- NA
- georesult[i,3] <- NA
- # return(c(address[i],NA,NA))
- }
- }
- return(georesult)
- }
- l = gGeoCode(v)
- keyval(k,l)
- })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement