위도,경도를 주소로 변환 ggmap package를 이용한 (geocoding and revgeocode in R)


GGMAP package

geocode

geocode함수는 latitude와 longitude를 가지고 location으로 변환 하는것을 말한다.
이것은 Google Map을 이용하게 된다.

Usage

geocode(location, output = c("latlon", "latlona", "more", "all"),
source = c("google", "dsk"), messaging = FALSE, force = ifelse(source ==
"dsk", FALSE, TRUE), sensor = FALSE, override_limit = FALSE,
client = "", signature = "", nameType = c("long", "short"), data)
geocodeQueryCheck(userType = "free")

예제

library(ggmap)

gc <- as.numeric(geocode('Baylor University'))
revgeocode(gc, output = 'more')
revgeocode(gc, output = 'all')

as.numeric(dfLocation[3,])
revgeocode(as.numeric(dfLocation[2:3,]),output = 'more')
revgeocode(latlng,output = 'more')
revgeocode(latlng, ics = 'WGS-84', api = 'google', output = 'address')

data.frame에 기록된 longitude와 latitude를 이용해서 Place로 변환하는 코드이다.

place <- adply(dfLocation, 1,function(x){
    revgeocode(as.numeric(x),output = 'more')
    }
)

test code

Using Google Maps API and R

library(RCurl)
library(RJSONIO)
library(plyr)

url <- function(address, return.call = "json", sensor = "false") {
    root <- "http://maps.google.com/maps/api/geocode/"
    u <- paste(root, return.call, "?address=", address, "&sensor=", sensor, sep = "")
    return(URLencode(u))
}

geoCode <- function(address,verbose=FALSE) {
    if(verbose) cat(address,"\n")
    u <- url(address)
    doc <- getURL(u)
    x <- fromJSON(doc,simplify = FALSE)
    if(x$status=="OK") {
        lat <- x$results[[1]]$geometry$location$lat
        lng <- x$results[[1]]$geometry$location$lng
        location_type  <- x$results[[1]]$geometry$location_type
        formatted_address  <- x$results[[1]]$formatted_address
        return(c(lat, lng, location_type, formatted_address))
        Sys.sleep(0.5)
    } else {
        return(c(NA,NA,NA, NA))
    }
}
address <- geoCode("The White House, Washington, DC")


+ Recent posts