Nothing new here. Just write a little bit code to do geocoding on a large file of addresses. The R code is as follows.
###########################################
# check and regeocoding NETS address
# using Google Map API
###########################################
setwd("C:/Geocoding")
source("C:/Geocoding/GIS_Google_Map_API.R") # code listed in the previous blog
address <- read.csv("addresses.csv",header=T)
GeoResult <- c()
for(i in 1:dim(address)[1]){
addr_item = address[i,]
Addr <- addr_item$Addr
Addr <- ifelse(is.na(Addr), "", paste(Addr,", ") )
City <- addr_item$City
City <- ifelse(is.na(City), "", paste(City,", ") )
State <- addr_item$State
State <- ifelse(is.na(State), "", paste(State,", ") )
Zip <- addr_item$ZIP
Zip <- ifelse(is.na(Zip), "", Zip )
Zip4 <-addr_item$Plus4
Zip4 <- ifelse(is.na(Zip4), "", paste("-",Zip4, sep="") )
full_addr <- paste(Addr,City,State,Zip,Zip4, sep="")
georesult <- gGeoCode(full_addr)
if(georesult$status == "OK"){
GeoResult_i <- cbind( rep(i,length(georesult$lat)) ,
rep(full_addr,length(georesult$lat)),
georesult$lat,
georesult$lng,
georesult$formatted_address,
georesult$location_type,
rep(georesult$status,length(georesult$lat))
)
}
if(georesult$status != "OK") GeoResult_i <- c(i, full_addr, NA, NA, NA, NA, georesult$status )
GeoResult <- rbind(GeoResult, GeoResult_i)
Sys.sleep(0.5) # we need to pose a little bit or else google will regard it as too-many queries a time.
}
colnames(GeoResult) <- c("item", "full_addr", "lat", "lng", "formatted_address", "location_type", "status")
write.csv(GeoResult,"GeoResult.csv")
No comments:
Post a Comment