check <- function(type, value){
if(value==""){
return("")
}
else(
return(paste(type
,"="
,gsub(" ", "+", value)
, "&"
,sep="")
)
)
}
#' currently throwing an ssl error
#' @export
census_geocoder <- function(address,zip,city,state){
addr <- check("street",address)
zip <- check("zip",zip)
city <- check("city",city)
state <- check("state",state)
string <- paste("https://geocoding.geo.census.gov/geocoder/geographies/address?"
,addr
,city
,zip
,state
,"benchmark=4&vintage=4&format=json"
,sep="")
options(RCurlOptions = list(cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl")))
print(paste("Geocoding ",string,sep=""))
json_file <- jsonlite::fromJSON(
RCurl::getURL(string)
)
if(length(json_file$result$addressMatches$coordinates)>0){
if(is.null(json_file$result$addressMatches$coordinates$x[1])==TRUE){
print("no result")
return(data.frame(
address=""
,lat = ""
,lon= ""
,tract = ""
,block = "")
)
} else{
return(data.frame(
address=as.character(data.frame(json_file$result$addressMatches$matchedAddress)[1,])
,lat = as.character(json_file$result$addressMatches$coordinates$y[1])
,lon= as.character(json_file$result$addressMatches$coordinates$x[1])
,tract = data.frame(json_file$result$addressMatches$geographies$`Census Tracts`)$GEOID[1]
,block = data.frame(json_file$result$addressMatches$geographies$`2010 Census Blocks`)[1,c("GEOID")]))
}
}
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.